ManagedRoute.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: 2023-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. {
  34. _target = target;
  35. _via = via;
  36. _src = src;
  37. if (via.ss_family == AF_INET)
  38. _via.setPort(32);
  39. else if (via.ss_family == AF_INET6)
  40. _via.setPort(128);
  41. if (src.ss_family == AF_INET) {
  42. _src.setPort(32);
  43. } else if (src.ss_family == AF_INET6) {
  44. _src.setPort(128);
  45. }
  46. Utils::scopy(_device,sizeof(_device),device);
  47. _systemDevice[0] = (char)0;
  48. }
  49. ~ManagedRoute()
  50. {
  51. this->remove();
  52. }
  53. /**
  54. * Set or update currently set route
  55. *
  56. * This must be called periodically for routes that shadow others so that
  57. * shadow routes can be updated. In some cases it has no effect
  58. *
  59. * @return True if route add/update was successful
  60. */
  61. bool sync();
  62. /**
  63. * Remove and clear this ManagedRoute
  64. *
  65. * This does nothing if this ManagedRoute is not set or has already been
  66. * removed. If this is not explicitly called it is called automatically on
  67. * destruct.
  68. */
  69. void remove();
  70. inline const InetAddress &target() const { return _target; }
  71. inline const InetAddress &via() const { return _via; }
  72. inline const InetAddress &src() const { return _src; }
  73. inline const char *device() const { return _device; }
  74. private:
  75. ManagedRoute(const ManagedRoute &) {}
  76. inline ManagedRoute &operator=(const ManagedRoute &) { return *this; }
  77. InetAddress _target;
  78. InetAddress _via;
  79. InetAddress _src;
  80. InetAddress _systemVia; // for route overrides
  81. std::map<InetAddress,bool> _applied; // routes currently applied
  82. char _device[128];
  83. char _systemDevice[128]; // for route overrides
  84. AtomicCounter __refCount;
  85. };
  86. } // namespace ZeroTier
  87. #endif