IncomingPacket.hpp 5.1 KB

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