ManagedRoute.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MANAGEDROUTE_HPP
  14. #define ZT_MANAGEDROUTE_HPP
  15. #include "../node/AtomicCounter.hpp"
  16. #include "../node/InetAddress.hpp"
  17. #include "../node/SharedPtr.hpp"
  18. #include "../node/Utils.hpp"
  19. #include <map>
  20. #include <stdexcept>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <vector>
  24. namespace ZeroTier {
  25. /**
  26. * A ZT-managed route that used C++ RAII semantics to automatically clean itself up on deallocate
  27. */
  28. class ManagedRoute {
  29. friend class SharedPtr<ManagedRoute>;
  30. public:
  31. ManagedRoute(const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* device);
  32. ~ManagedRoute();
  33. /**
  34. * Set or update currently set route
  35. *
  36. * This must be called periodically for routes that shadow others so that
  37. * shadow routes can be updated. In some cases it has no effect
  38. *
  39. * @return True if route add/update was successful
  40. */
  41. bool sync();
  42. /**
  43. * Remove and clear this ManagedRoute
  44. *
  45. * This does nothing if this ManagedRoute is not set or has already been
  46. * removed. If this is not explicitly called it is called automatically on
  47. * destruct.
  48. */
  49. void remove();
  50. inline const InetAddress& target() const
  51. {
  52. return _target;
  53. }
  54. inline const InetAddress& via() const
  55. {
  56. return _via;
  57. }
  58. inline const InetAddress& src() const
  59. {
  60. return _src;
  61. }
  62. inline const char* device() const
  63. {
  64. return _device;
  65. }
  66. private:
  67. ManagedRoute(const ManagedRoute&)
  68. {
  69. }
  70. inline ManagedRoute& operator=(const ManagedRoute&)
  71. {
  72. return *this;
  73. }
  74. InetAddress _target;
  75. InetAddress _via;
  76. InetAddress _src;
  77. InetAddress _systemVia; // for route overrides
  78. std::map<InetAddress, bool> _applied; // routes currently applied
  79. char _device[128];
  80. char _systemDevice[128]; // for route overrides
  81. AtomicCounter __refCount;
  82. };
  83. } // namespace ZeroTier
  84. #endif