RemotePath.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. _fixed(false) {}
  51. RemotePath(const InetAddress &addr,bool fixed) :
  52. Path(addr,0,TRUST_NORMAL,false),
  53. _lastSend(0),
  54. _lastReceived(0),
  55. _fixed(fixed) {}
  56. inline uint64_t lastSend() const throw() { return _lastSend; }
  57. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  58. /**
  59. * @return Is this a fixed path?
  60. */
  61. inline bool fixed() const throw() { return _fixed; }
  62. /**
  63. * @param f New value of fixed flag
  64. */
  65. inline void setFixed(const bool f)
  66. throw()
  67. {
  68. _fixed = f;
  69. }
  70. /**
  71. * Called when a packet is sent to this remote path
  72. *
  73. * This is called automatically by RemotePath::send().
  74. *
  75. * @param t Time of send
  76. */
  77. inline void sent(uint64_t t)
  78. throw()
  79. {
  80. _lastSend = t;
  81. }
  82. /**
  83. * Called when a packet is received from this remote path
  84. *
  85. * @param t Time of receive
  86. */
  87. inline void received(uint64_t t)
  88. throw()
  89. {
  90. _lastReceived = t;
  91. }
  92. /**
  93. * @param now Current time
  94. * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
  95. */
  96. inline bool active(uint64_t now) const
  97. throw()
  98. {
  99. return ( (_fixed) || ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT) );
  100. }
  101. /**
  102. * Send a packet via this path
  103. *
  104. * @param RR Runtime environment
  105. * @param data Packet data
  106. * @param len Packet length
  107. * @param now Current time
  108. * @return True if transport reported success
  109. */
  110. inline bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now)
  111. {
  112. if (RR->node->putPacket(_addr,data,len)) {
  113. sent(now);
  114. RR->antiRec->logOutgoingZT(data,len);
  115. return true;
  116. }
  117. return false;
  118. }
  119. private:
  120. uint64_t _lastSend;
  121. uint64_t _lastReceived;
  122. bool _fixed;
  123. };
  124. } // namespace ZeroTier
  125. #endif