PacketDecoder.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_PACKETDECODER_HPP
  28. #define _ZT_PACKETDECODER_HPP
  29. #include <stdexcept>
  30. #include "Packet.hpp"
  31. #include "Demarc.hpp"
  32. #include "InetAddress.hpp"
  33. #include "Utils.hpp"
  34. #include "SharedPtr.hpp"
  35. #include "AtomicCounter.hpp"
  36. #include "Peer.hpp"
  37. /*
  38. * The big picture:
  39. *
  40. * tryDecode gets called for a given fully-assembled packet until it returns
  41. * true or the packet's time to live has been exceeded, in which case it is
  42. * discarded as failed decode. Any exception thrown by tryDecode also causes
  43. * the packet to be discarded.
  44. *
  45. * Thus a return of false from tryDecode() indicates that it should be called
  46. * again. Logic is very simple as to when, and it's in doAnythingWaitingForPeer
  47. * in Switch. This might be expanded to be more fine grained in the future.
  48. *
  49. * A return value of true indicates that the packet is done. tryDecode must
  50. * never be called again after that.
  51. */
  52. namespace ZeroTier {
  53. class RuntimeEnvironment;
  54. /**
  55. * Subclass of packet that handles the decoding of it
  56. */
  57. class PacketDecoder : public Packet
  58. {
  59. friend class SharedPtr<PacketDecoder>;
  60. public:
  61. /**
  62. * Create a new packet-in-decode
  63. *
  64. * @param b Source buffer with raw packet data
  65. * @param localPort Local port on which packet was received
  66. * @param remoteAddress Address from which packet came
  67. * @throws std::out_of_range Range error processing packet
  68. */
  69. template<unsigned int C2>
  70. PacketDecoder(const Buffer<C2> &b,Demarc::Port localPort,const InetAddress &remoteAddress)
  71. throw(std::out_of_range) :
  72. Packet(b),
  73. _receiveTime(Utils::now()),
  74. _localPort(localPort),
  75. _remoteAddress(remoteAddress),
  76. _step(DECODE_WAITING_FOR_SENDER_LOOKUP),
  77. __refCount()
  78. {
  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 _r Runtime environment
  90. * @return True if decoding and processing is complete, false if caller should try again
  91. * @throws std::out_of_range Range error processing packet (should be discarded)
  92. * @throws std::runtime_error Other error processing packet (should be discarded)
  93. */
  94. bool tryDecode(const RuntimeEnvironment *_r)
  95. throw(std::out_of_range,std::runtime_error);
  96. /**
  97. * @return Time of packet receipt / start of decode
  98. */
  99. inline uint64_t receiveTime() const throw() { return _receiveTime; }
  100. private:
  101. struct _CBaddPeerFromHello_Data
  102. {
  103. const RuntimeEnvironment *renv;
  104. Address source;
  105. InetAddress remoteAddress;
  106. Demarc::Port localPort;
  107. unsigned int vMajor,vMinor,vRevision;
  108. uint64_t helloPacketId;
  109. uint64_t helloTimestamp;
  110. };
  111. static void _CBaddPeerFromHello(
  112. void *arg, // _CBaddPeerFromHello_Data
  113. const SharedPtr<Peer> &p,
  114. Topology::PeerVerifyResult result);
  115. static void _CBaddPeerFromWhois(
  116. void *arg, // RuntimeEnvironment
  117. const SharedPtr<Peer> &p,
  118. Topology::PeerVerifyResult result);
  119. // These are called internally to handle packet contents once it has
  120. // been authenticated, decrypted, decompressed, and classified.
  121. bool _doERROR(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  122. bool _doHELLO(const RuntimeEnvironment *_r);
  123. bool _doOK(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  124. bool _doWHOIS(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  125. bool _doRENDEZVOUS(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  126. bool _doFRAME(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  127. bool _doMULTICAST_LIKE(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  128. bool _doMULTICAST_GOT(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  129. bool _doMULTICAST_FRAME(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  130. bool _doNETWORK_MEMBERSHIP_CERTIFICATE(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  131. bool _doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  132. bool _doNETWORK_CONFIG_REFRESH(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  133. uint64_t _receiveTime;
  134. Demarc::Port _localPort;
  135. InetAddress _remoteAddress;
  136. enum {
  137. DECODE_WAITING_FOR_SENDER_LOOKUP, // on initial receipt, we need peer's identity
  138. DECODE_WAITING_FOR_MULTICAST_FRAME_ORIGINAL_SENDER_LOOKUP,
  139. } _step;
  140. AtomicCounter __refCount;
  141. };
  142. } // namespace ZeroTier
  143. #endif