IncomingPacket.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #ifndef ZT_INCOMINGPACKET_HPP
  19. #define ZT_INCOMINGPACKET_HPP
  20. #include <stdexcept>
  21. #include "Packet.hpp"
  22. #include "Path.hpp"
  23. #include "Utils.hpp"
  24. #include "MulticastGroup.hpp"
  25. #include "Peer.hpp"
  26. /*
  27. * The big picture:
  28. *
  29. * tryDecode gets called for a given fully-assembled packet until it returns
  30. * true or the packet's time to live has been exceeded, in which case it is
  31. * discarded as failed decode. Any exception thrown by tryDecode also causes
  32. * the packet to be discarded.
  33. *
  34. * Thus a return of false from tryDecode() indicates that it should be called
  35. * again. Logic is very simple as to when, and it's in doAnythingWaitingForPeer
  36. * in Switch. This might be expanded to be more fine grained in the future.
  37. *
  38. * A return value of true indicates that the packet is done. tryDecode must
  39. * never be called again after that.
  40. */
  41. namespace ZeroTier {
  42. class RuntimeEnvironment;
  43. class Network;
  44. /**
  45. * Subclass of packet that handles the decoding of it
  46. */
  47. class IncomingPacket : public Packet
  48. {
  49. public:
  50. IncomingPacket() :
  51. Packet(),
  52. _receiveTime(0)
  53. {
  54. }
  55. /**
  56. * Create a new packet-in-decode
  57. *
  58. * @param data Packet data
  59. * @param len Packet length
  60. * @param path Path over which packet arrived
  61. * @param now Current time
  62. * @throws std::out_of_range Range error processing packet
  63. */
  64. IncomingPacket(const void *data,unsigned int len,const SharedPtr<Path> &path,uint64_t now) :
  65. Packet(data,len),
  66. _receiveTime(now),
  67. _path(path)
  68. {
  69. }
  70. /**
  71. * Init packet-in-decode in place
  72. *
  73. * @param data Packet data
  74. * @param len Packet length
  75. * @param path Path over which packet arrived
  76. * @param now Current time
  77. * @throws std::out_of_range Range error processing packet
  78. */
  79. inline void init(const void *data,unsigned int len,const SharedPtr<Path> &path,uint64_t now)
  80. {
  81. copyFrom(data,len);
  82. _receiveTime = now;
  83. _path = path;
  84. }
  85. /**
  86. * Attempt to decode this packet
  87. *
  88. * Note that this returns 'true' if processing is complete. This says nothing
  89. * about whether the packet was valid. A rejection is 'complete.'
  90. *
  91. * Once true is returned, this must not be called again. The packet's state
  92. * may no longer be valid.
  93. *
  94. * @param RR Runtime environment
  95. * @return True if decoding and processing is complete, false if caller should try again
  96. */
  97. bool tryDecode(const RuntimeEnvironment *RR);
  98. /**
  99. * @return Time of packet receipt / start of decode
  100. */
  101. inline uint64_t receiveTime() const throw() { return _receiveTime; }
  102. private:
  103. // These are called internally to handle packet contents once it has
  104. // been authenticated, decrypted, decompressed, and classified.
  105. bool _doERROR(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  106. bool _doHELLO(const RuntimeEnvironment *RR,const bool alreadyAuthenticated);
  107. bool _doOK(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  108. bool _doWHOIS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  109. bool _doRENDEZVOUS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  110. bool _doFRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  111. bool _doEXT_FRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  112. bool _doECHO(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  113. bool _doMULTICAST_LIKE(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  114. bool _doNETWORK_CREDENTIALS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  115. bool _doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  116. bool _doNETWORK_CONFIG(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  117. bool _doMULTICAST_GATHER(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  118. bool _doMULTICAST_FRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  119. bool _doPUSH_DIRECT_PATHS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  120. bool _doCIRCUIT_TEST(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  121. bool _doCIRCUIT_TEST_REPORT(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  122. void _sendErrorNeedCredentials(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer,const uint64_t nwid);
  123. uint64_t _receiveTime;
  124. SharedPtr<Path> _path;
  125. };
  126. } // namespace ZeroTier
  127. #endif