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