ManagedRoute.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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: 2025-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 <stdlib.h>
  16. #include <string.h>
  17. #include "../node/InetAddress.hpp"
  18. #include "../node/Utils.hpp"
  19. #include "../node/SharedPtr.hpp"
  20. #include "../node/AtomicCounter.hpp"
  21. #include <stdexcept>
  22. #include <vector>
  23. #include <map>
  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. {
  30. friend class SharedPtr<ManagedRoute>;
  31. public:
  32. ManagedRoute(const InetAddress &target,const InetAddress &via,const InetAddress &src,const char *device);
  33. ~ManagedRoute();
  34. /**
  35. * Set or update currently set route
  36. *
  37. * This must be called periodically for routes that shadow others so that
  38. * shadow routes can be updated. In some cases it has no effect
  39. *
  40. * @return True if route add/update was successful
  41. */
  42. bool sync();
  43. /**
  44. * Remove and clear this ManagedRoute
  45. *
  46. * This does nothing if this ManagedRoute is not set or has already been
  47. * removed. If this is not explicitly called it is called automatically on
  48. * destruct.
  49. */
  50. void remove();
  51. inline const InetAddress &target() const { return _target; }
  52. inline const InetAddress &via() const { return _via; }
  53. inline const InetAddress &src() const { return _src; }
  54. inline const char *device() const { return _device; }
  55. private:
  56. ManagedRoute(const ManagedRoute &) {}
  57. inline ManagedRoute &operator=(const ManagedRoute &) { return *this; }
  58. InetAddress _target;
  59. InetAddress _via;
  60. InetAddress _src;
  61. InetAddress _systemVia; // for route overrides
  62. std::map<InetAddress,bool> _applied; // routes currently applied
  63. char _device[128];
  64. char _systemDevice[128]; // for route overrides
  65. AtomicCounter __refCount;
  66. };
  67. } // namespace ZeroTier
  68. #endif