RemotePath.hpp 3.1 KB

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