ManagedRoute.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "../node/SharedPtr.hpp"
  8. #include "../node/AtomicCounter.hpp"
  9. #include "../node/NonCopyable.hpp"
  10. #include <stdexcept>
  11. #include <vector>
  12. #include <map>
  13. namespace ZeroTier {
  14. /**
  15. * A ZT-managed route that used C++ RAII semantics to automatically clean itself up on deallocate
  16. */
  17. class ManagedRoute : NonCopyable
  18. {
  19. friend class SharedPtr<ManagedRoute>;
  20. public:
  21. ManagedRoute(const InetAddress &target,const InetAddress &via,const char *device)
  22. {
  23. _target = target;
  24. _via = via;
  25. if (via.ss_family == AF_INET)
  26. _via.setPort(32);
  27. else if (via.ss_family == AF_INET6)
  28. _via.setPort(128);
  29. Utils::scopy(_device,sizeof(_device),device);
  30. _systemDevice[0] = (char)0;
  31. }
  32. ~ManagedRoute()
  33. {
  34. this->remove();
  35. }
  36. /**
  37. * Set or update currently set route
  38. *
  39. * This must be called periodically for routes that shadow others so that
  40. * shadow routes can be updated. In some cases it has no effect
  41. *
  42. * @return True if route add/update was successful
  43. */
  44. bool sync();
  45. /**
  46. * Remove and clear this ManagedRoute
  47. *
  48. * This does nothing if this ManagedRoute is not set or has already been
  49. * removed. If this is not explicitly called it is called automatically on
  50. * destruct.
  51. */
  52. void remove();
  53. inline const InetAddress &target() const { return _target; }
  54. inline const InetAddress &via() const { return _via; }
  55. inline const char *device() const { return _device; }
  56. private:
  57. InetAddress _target;
  58. InetAddress _via;
  59. InetAddress _systemVia; // for route overrides
  60. std::map<InetAddress,bool> _applied; // routes currently applied
  61. char _device[128];
  62. char _systemDevice[128]; // for route overrides
  63. AtomicCounter __refCount;
  64. };
  65. } // namespace ZeroTier
  66. #endif