IncomingPacket.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_INCOMINGPACKET_HPP
  14. #define ZT_INCOMINGPACKET_HPP
  15. #include <stdexcept>
  16. #include "Packet.hpp"
  17. #include "Path.hpp"
  18. #include "Utils.hpp"
  19. #include "MulticastGroup.hpp"
  20. #include "Peer.hpp"
  21. /*
  22. * The big picture:
  23. *
  24. * tryDecode gets called for a given fully-assembled packet until it returns
  25. * true or the packet's time to live has been exceeded, in which case it is
  26. * discarded as failed decode. Any exception thrown by tryDecode also causes
  27. * the packet to be discarded.
  28. *
  29. * Thus a return of false from tryDecode() indicates that it should be called
  30. * again. Logic is very simple as to when, and it's in doAnythingWaitingForPeer
  31. * in Switch. This might be expanded to be more fine grained in the future.
  32. *
  33. * A return value of true indicates that the packet is done. tryDecode must
  34. * never be called again after that.
  35. */
  36. namespace ZeroTier {
  37. class RuntimeEnvironment;
  38. class Network;
  39. /**
  40. * Subclass of packet that handles the decoding of it
  41. */
  42. class IncomingPacket : public Packet
  43. {
  44. public:
  45. IncomingPacket() :
  46. Packet(),
  47. _receiveTime(0),
  48. _path(),
  49. _authenticated(false)
  50. {
  51. }
  52. /**
  53. * Create a new packet-in-decode
  54. *
  55. * @param data Packet data
  56. * @param len Packet length
  57. * @param path Path over which packet arrived
  58. * @param now Current time
  59. * @throws std::out_of_range Range error processing packet
  60. */
  61. IncomingPacket(const void *data,unsigned int len,const SharedPtr<Path> &path,int64_t now) :
  62. Packet(data,len),
  63. _receiveTime(now),
  64. _path(path),
  65. _authenticated(false)
  66. {
  67. }
  68. /**
  69. * Init packet-in-decode in place
  70. *
  71. * @param data Packet data
  72. * @param len Packet length
  73. * @param path Path over which packet arrived
  74. * @param now Current time
  75. * @throws std::out_of_range Range error processing packet
  76. */
  77. inline void init(const void *data,unsigned int len,const SharedPtr<Path> &path,int64_t now)
  78. {
  79. copyFrom(data,len);
  80. _receiveTime = now;
  81. _path = path;
  82. _authenticated = false;
  83. }
  84. /**
  85. * Attempt to decode this packet
  86. *
  87. * Note that this returns 'true' if processing is complete. This says nothing
  88. * about whether the packet was valid. A rejection is 'complete.'
  89. *
  90. * Once true is returned, this must not be called again. The packet's state
  91. * may no longer be valid.
  92. *
  93. * @param RR Runtime environment
  94. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  95. * @return True if decoding and processing is complete, false if caller should try again
  96. */
  97. bool tryDecode(const RuntimeEnvironment *RR,void *tPtr,int32_t flowId);
  98. /**
  99. * @return Time of packet receipt / start of decode
  100. */
  101. inline uint64_t receiveTime() const { 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,void *tPtr,const SharedPtr<Peer> &peer);
  106. bool _doHELLO(const RuntimeEnvironment *RR,void *tPtr,const bool alreadyAuthenticated);
  107. bool _doACK(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  108. bool _doQOS_MEASUREMENT(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  109. bool _doOK(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  110. bool _doWHOIS(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  111. bool _doRENDEZVOUS(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  112. bool _doFRAME(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer,int32_t flowId);
  113. bool _doEXT_FRAME(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer,int32_t flowId);
  114. bool _doECHO(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  115. bool _doMULTICAST_LIKE(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  116. bool _doNETWORK_CREDENTIALS(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  117. bool _doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  118. bool _doNETWORK_CONFIG(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  119. bool _doMULTICAST_GATHER(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  120. bool _doMULTICAST_FRAME(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  121. bool _doPUSH_DIRECT_PATHS(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  122. bool _doUSER_MESSAGE(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  123. bool _doREMOTE_TRACE(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  124. bool _doPATH_NEGOTIATION_REQUEST(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer);
  125. void _sendErrorNeedCredentials(const RuntimeEnvironment *RR,void *tPtr,const SharedPtr<Peer> &peer,const uint64_t nwid);
  126. uint64_t _receiveTime;
  127. SharedPtr<Path> _path;
  128. bool _authenticated;
  129. };
  130. } // namespace ZeroTier
  131. #endif