ManagedRoute.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef ZT_MANAGEDROUTE_HPP
  2. #define ZT_MANAGEDROUTE_HPP
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "../node/InetAddress.hpp"
  6. #include "../node/Utils.hpp"
  7. #include <stdexcept>
  8. #include <vector>
  9. namespace ZeroTier {
  10. /**
  11. * A ZT-managed route that used C++ RAII semantics to automatically clean itself up on deallocate
  12. */
  13. class ManagedRoute
  14. {
  15. public:
  16. ManagedRoute()
  17. {
  18. _device[0] = (char)0;
  19. _systemDevice[0] = (char)0;
  20. _applied = false;
  21. }
  22. ~ManagedRoute()
  23. {
  24. this->remove();
  25. }
  26. ManagedRoute(const ManagedRoute &r)
  27. {
  28. _applied = false;
  29. *this = r;
  30. }
  31. inline ManagedRoute &operator=(const ManagedRoute &r)
  32. {
  33. if ((!_applied)&&(!r._applied)) {
  34. memcpy(this,&r,sizeof(ManagedRoute)); // InetAddress is memcpy'able
  35. } else {
  36. fprintf(stderr,"Applied ManagedRoute isn't copyable!\n");
  37. abort();
  38. }
  39. return *this;
  40. }
  41. /**
  42. * Initialize object and set route
  43. *
  44. * Note: on Windows, use the interface NET_LUID in hexadecimal as the
  45. * "device name."
  46. *
  47. * @param target Route target (e.g. 0.0.0.0/0 for default)
  48. * @param via Route next L3 hop or NULL InetAddress if local in which case it will be routed via device
  49. * @param device Name or hex LUID of ZeroTier device (e.g. zt#)
  50. * @return True if route was successfully set
  51. */
  52. inline bool set(const InetAddress &target,const InetAddress &via,const char *device)
  53. {
  54. if ((!via)&&(!device[0]))
  55. return false;
  56. this->remove();
  57. _target = target;
  58. _via = via;
  59. Utils::scopy(_device,sizeof(_device),device);
  60. return this->sync();
  61. }
  62. /**
  63. * Set or update currently set route
  64. *
  65. * This must be called periodically for routes that shadow others so that
  66. * shadow routes can be updated. In some cases it has no effect
  67. *
  68. * @return True if route add/update was successful
  69. */
  70. bool sync();
  71. /**
  72. * Remove and clear this ManagedRoute
  73. *
  74. * This does nothing if this ManagedRoute is not set or has already been
  75. * removed. If this is not explicitly called it is called automatically on
  76. * destruct.
  77. */
  78. void remove();
  79. inline const InetAddress &target() const { return _target; }
  80. inline const InetAddress &via() const { return _via; }
  81. inline const char *device() const { return _device; }
  82. private:
  83. InetAddress _target;
  84. InetAddress _via;
  85. InetAddress _systemVia; // for route overrides
  86. char _device[128];
  87. char _systemDevice[128]; // for route overrides
  88. bool _applied;
  89. };
  90. } // namespace ZeroTier
  91. #endif