IncomingPacket.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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: 2026-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 "MulticastGroup.hpp"
  16. #include "Packet.hpp"
  17. #include "Path.hpp"
  18. #include "Peer.hpp"
  19. #include "Utils.hpp"
  20. #include <stdexcept>
  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. public:
  44. IncomingPacket() : Packet(), _receiveTime(0), _path(), _authenticated(false)
  45. {
  46. }
  47. /**
  48. * Create a new packet-in-decode
  49. *
  50. * @param data Packet data
  51. * @param len Packet length
  52. * @param path Path over which packet arrived
  53. * @param now Current time
  54. * @throws std::out_of_range Range error processing packet
  55. */
  56. IncomingPacket(const void* data, unsigned int len, const SharedPtr<Path>& path, int64_t now) : Packet(data, len), _receiveTime(now), _path(path), _authenticated(false)
  57. {
  58. }
  59. /**
  60. * Init packet-in-decode in place
  61. *
  62. * @param data Packet data
  63. * @param len Packet length
  64. * @param path Path over which packet arrived
  65. * @param now Current time
  66. * @throws std::out_of_range Range error processing packet
  67. */
  68. inline void init(const void* data, unsigned int len, const SharedPtr<Path>& path, int64_t now)
  69. {
  70. copyFrom(data, len);
  71. _receiveTime = now;
  72. _path = path;
  73. _authenticated = false;
  74. }
  75. /**
  76. * Attempt to decode this packet
  77. *
  78. * Note that this returns 'true' if processing is complete. This says nothing
  79. * about whether the packet was valid. A rejection is 'complete.'
  80. *
  81. * Once true is returned, this must not be called again. The packet's state
  82. * may no longer be valid.
  83. *
  84. * @param RR Runtime environment
  85. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  86. * @return True if decoding and processing is complete, false if caller should try again
  87. */
  88. bool tryDecode(const RuntimeEnvironment* RR, void* tPtr, int32_t flowId);
  89. /**
  90. * @return Time of packet receipt / start of decode
  91. */
  92. inline uint64_t receiveTime() const
  93. {
  94. return _receiveTime;
  95. }
  96. private:
  97. // These are called internally to handle packet contents once it has
  98. // been authenticated, decrypted, decompressed, and classified.
  99. bool _doERROR(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  100. bool _doHELLO(const RuntimeEnvironment* RR, void* tPtr, const bool alreadyAuthenticated);
  101. bool _doACK(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  102. bool _doQOS_MEASUREMENT(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  103. bool _doOK(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  104. bool _doWHOIS(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  105. bool _doRENDEZVOUS(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  106. bool _doFRAME(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer, int32_t flowId);
  107. bool _doEXT_FRAME(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer, int32_t flowId);
  108. bool _doECHO(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  109. bool _doMULTICAST_LIKE(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  110. bool _doNETWORK_CREDENTIALS(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  111. bool _doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  112. bool _doNETWORK_CONFIG(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  113. bool _doMULTICAST_GATHER(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  114. bool _doMULTICAST_FRAME(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  115. bool _doPUSH_DIRECT_PATHS(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  116. bool _doUSER_MESSAGE(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  117. bool _doREMOTE_TRACE(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  118. bool _doPATH_NEGOTIATION_REQUEST(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer);
  119. void _sendErrorNeedCredentials(const RuntimeEnvironment* RR, void* tPtr, const SharedPtr<Peer>& peer, const uint64_t nwid);
  120. uint64_t _receiveTime;
  121. SharedPtr<Path> _path;
  122. bool _authenticated;
  123. };
  124. } // namespace ZeroTier
  125. #endif