IncomingPacket.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "Path.hpp"
  16. #include "Utils.hpp"
  17. #include "MulticastGroup.hpp"
  18. #include "Peer.hpp"
  19. #include "Buf.hpp"
  20. #include "Protocol.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. class IncomingPacket
  40. {
  41. public:
  42. ZT_ALWAYS_INLINE IncomingPacket() {}
  43. template<typename X>
  44. ZT_ALWAYS_INLINE void set(const SharedPtr< Buf<X> > &pkt_,const unsigned int pktSize_,const SharedPtr<Path> &path_,const int64_t now_)
  45. {
  46. idBE = 0; // initially zero, set when decryption/auth occurs
  47. receiveTime = now_;
  48. path = path_;
  49. pkt = reinterpret_cast< SharedPtr< Buf< Protocol::Header > > >(pkt_);
  50. size = pktSize_;
  51. hops = Protocol::packetHops(pkt->data.fields);
  52. }
  53. /**
  54. * Attempt to decode this packet
  55. *
  56. * Note that this returns 'true' if processing is complete. This says nothing
  57. * about whether the packet was valid. A rejection is 'complete.'
  58. *
  59. * Once true is returned, this must not be called again. The packet's state
  60. * may no longer be valid.
  61. *
  62. * @param RR Runtime environment
  63. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  64. * @return True if decoding and processing is complete, false if caller should try again
  65. */
  66. bool tryDecode(const RuntimeEnvironment *RR,void *tPtr);
  67. /**
  68. * Packet ID in big-endian byte order or 0 if not decrypted/dearmored yet
  69. */
  70. uint64_t idBE;
  71. /**
  72. * Time packet was received
  73. */
  74. int64_t receiveTime;
  75. /**
  76. * Path over which packet was received
  77. */
  78. SharedPtr< Path > path;
  79. /**
  80. * Packet itself
  81. */
  82. SharedPtr< Buf< Protocol::Header > > pkt;
  83. /**
  84. * Size of packet in bytes
  85. */
  86. unsigned int size;
  87. /**
  88. * Hop count for received packet
  89. */
  90. uint8_t hops;
  91. };
  92. } // namespace ZeroTier
  93. #endif