IncomingPacket.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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: 2024-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 "Packet.hpp"
  16. #include "Path.hpp"
  17. #include "Utils.hpp"
  18. #include "MulticastGroup.hpp"
  19. #include "Peer.hpp"
  20. /*
  21. * The big picture:
  22. *
  23. * tryDecode gets called for a given fully-assembled packet until it returns
  24. * true or the packet's time to live has been exceeded, in which case it is
  25. * discarded as failed decode. Any exception thrown by tryDecode also causes
  26. * the packet to be discarded.
  27. *
  28. * Thus a return of false from tryDecode() indicates that it should be called
  29. * again. Logic is very simple as to when, and it's in doAnythingWaitingForPeer
  30. * in Switch. This might be expanded to be more fine grained in the future.
  31. *
  32. * A return value of true indicates that the packet is done. tryDecode must
  33. * never be called again after that.
  34. */
  35. namespace ZeroTier {
  36. class RuntimeEnvironment;
  37. class Network;
  38. class IncomingPacket : public Packet
  39. {
  40. public:
  41. ZT_ALWAYS_INLINE IncomingPacket() : Packet(),_receiveTime(0),_path() {}
  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. ZT_ALWAYS_INLINE IncomingPacket(const void *data,unsigned int len,const SharedPtr<Path> &path,int64_t now) :
  52. Packet(data,len),
  53. _receiveTime(now),
  54. _path(path)
  55. {
  56. }
  57. /**
  58. * Init packet-in-decode in place
  59. *
  60. * @param data Packet data
  61. * @param len Packet length
  62. * @param path Path over which packet arrived
  63. * @param now Current time
  64. * @throws std::out_of_range Range error processing packet
  65. */
  66. ZT_ALWAYS_INLINE void init(const void *data,unsigned int len,const SharedPtr<Path> &path,int64_t now)
  67. {
  68. copyFrom(data,len);
  69. _receiveTime = now;
  70. _path = path;
  71. }
  72. /**
  73. * Attempt to decode this packet
  74. *
  75. * Note that this returns 'true' if processing is complete. This says nothing
  76. * about whether the packet was valid. A rejection is 'complete.'
  77. *
  78. * Once true is returned, this must not be called again. The packet's state
  79. * may no longer be valid.
  80. *
  81. * @param RR Runtime environment
  82. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  83. * @return True if decoding and processing is complete, false if caller should try again
  84. */
  85. bool tryDecode(const RuntimeEnvironment *RR,void *tPtr);
  86. /**
  87. * @return Time of packet receipt / start of decode
  88. */
  89. ZT_ALWAYS_INLINE uint64_t receiveTime() const { return _receiveTime; }
  90. private:
  91. uint64_t _receiveTime;
  92. SharedPtr<Path> _path;
  93. };
  94. } // namespace ZeroTier
  95. #endif