IncomingPacket.hpp 5.5 KB

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