RemotePath.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_REMOTEPATH_HPP
  28. #define ZT_REMOTEPATH_HPP
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdexcept>
  32. #include <algorithm>
  33. #include "Path.hpp"
  34. #include "Node.hpp"
  35. #include "AntiRecursion.hpp"
  36. #include "RuntimeEnvironment.hpp"
  37. namespace ZeroTier {
  38. /**
  39. * Path to a remote peer
  40. *
  41. * This extends Path to include status information about path activity.
  42. */
  43. class RemotePath : public Path
  44. {
  45. public:
  46. RemotePath() :
  47. Path(),
  48. _lastSend(0),
  49. _lastReceived(0),
  50. _localAddress(),
  51. _fixed(false) {}
  52. RemotePath(const InetAddress &localAddress,const InetAddress &addr,bool fixed) :
  53. Path(addr,0,TRUST_NORMAL),
  54. _lastSend(0),
  55. _lastReceived(0),
  56. _localAddress(localAddress),
  57. _fixed(fixed) {}
  58. inline const InetAddress &localAddress() const throw() { return _localAddress; }
  59. inline uint64_t lastSend() const throw() { return _lastSend; }
  60. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  61. /**
  62. * @return Is this a fixed path?
  63. */
  64. inline bool fixed() const throw() { return _fixed; }
  65. /**
  66. * @param f New value of fixed flag
  67. */
  68. inline void setFixed(const bool f)
  69. throw()
  70. {
  71. _fixed = f;
  72. }
  73. /**
  74. * Called when a packet is sent to this remote path
  75. *
  76. * This is called automatically by RemotePath::send().
  77. *
  78. * @param t Time of send
  79. */
  80. inline void sent(uint64_t t)
  81. throw()
  82. {
  83. _lastSend = t;
  84. }
  85. /**
  86. * Called when a packet is received from this remote path
  87. *
  88. * @param t Time of receive
  89. */
  90. inline void received(uint64_t t)
  91. throw()
  92. {
  93. _lastReceived = t;
  94. }
  95. /**
  96. * @param now Current time
  97. * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
  98. */
  99. inline bool active(uint64_t now) const
  100. throw()
  101. {
  102. return ( (_fixed) || ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT) );
  103. }
  104. /**
  105. * Send a packet via this path
  106. *
  107. * @param RR Runtime environment
  108. * @param data Packet data
  109. * @param len Packet length
  110. * @param now Current time
  111. * @return True if transport reported success
  112. */
  113. inline bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now)
  114. {
  115. if (RR->node->putPacket(_localAddress,address(),data,len)) {
  116. sent(now);
  117. RR->antiRec->logOutgoingZT(data,len);
  118. return true;
  119. }
  120. return false;
  121. }
  122. private:
  123. uint64_t _lastSend;
  124. uint64_t _lastReceived;
  125. InetAddress _localAddress;
  126. bool _fixed;
  127. };
  128. } // namespace ZeroTier
  129. #endif