IncomingPacket.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #ifndef ZT_INCOMINGPACKET_HPP
  19. #define ZT_INCOMINGPACKET_HPP
  20. #include <stdexcept>
  21. #include "Packet.hpp"
  22. #include "InetAddress.hpp"
  23. #include "Utils.hpp"
  24. #include "SharedPtr.hpp"
  25. #include "AtomicCounter.hpp"
  26. #include "MulticastGroup.hpp"
  27. #include "Peer.hpp"
  28. /*
  29. * The big picture:
  30. *
  31. * tryDecode gets called for a given fully-assembled packet until it returns
  32. * true or the packet's time to live has been exceeded, in which case it is
  33. * discarded as failed decode. Any exception thrown by tryDecode also causes
  34. * the packet to be discarded.
  35. *
  36. * Thus a return of false from tryDecode() indicates that it should be called
  37. * again. Logic is very simple as to when, and it's in doAnythingWaitingForPeer
  38. * in Switch. This might be expanded to be more fine grained in the future.
  39. *
  40. * A return value of true indicates that the packet is done. tryDecode must
  41. * never be called again after that.
  42. */
  43. namespace ZeroTier {
  44. class RuntimeEnvironment;
  45. class Network;
  46. /**
  47. * Subclass of packet that handles the decoding of it
  48. */
  49. class IncomingPacket : public Packet
  50. {
  51. friend class SharedPtr<IncomingPacket>;
  52. public:
  53. /**
  54. * Create a new packet-in-decode
  55. *
  56. * @param data Packet data
  57. * @param len Packet length
  58. * @param localAddress Local interface address
  59. * @param remoteAddress Address from which packet came
  60. * @param now Current time
  61. * @throws std::out_of_range Range error processing packet
  62. */
  63. IncomingPacket(const void *data,unsigned int len,const InetAddress &localAddress,const InetAddress &remoteAddress,uint64_t now) :
  64. Packet(data,len),
  65. _receiveTime(now),
  66. _localAddress(localAddress),
  67. _remoteAddress(remoteAddress),
  68. __refCount()
  69. {
  70. }
  71. /**
  72. * Attempt to decode this packet
  73. *
  74. * Note that this returns 'true' if processing is complete. This says nothing
  75. * about whether the packet was valid. A rejection is 'complete.'
  76. *
  77. * Once true is returned, this must not be called again. The packet's state
  78. * may no longer be valid. The only exception is deferred decoding. In this
  79. * case true is returned to indicate to the normal decode path that it is
  80. * finished with the packet. The packet will have added itself to the
  81. * deferred queue and will expect tryDecode() to be called one more time
  82. * with deferred set to true.
  83. *
  84. * Deferred decoding is performed by DeferredPackets.cpp and should not be
  85. * done elsewhere. Under deferred decoding packets only get one shot and
  86. * so the return value of tryDecode() is ignored.
  87. *
  88. * @param RR Runtime environment
  89. * @param deferred If true, this is a deferred decode and the return is ignored
  90. * @return True if decoding and processing is complete, false if caller should try again
  91. */
  92. bool tryDecode(const RuntimeEnvironment *RR,bool deferred);
  93. /**
  94. * @return Time of packet receipt / start of decode
  95. */
  96. inline uint64_t receiveTime() const throw() { return _receiveTime; }
  97. /**
  98. * Compute the Salsa20/12+SHA512 proof of work function
  99. *
  100. * @param difficulty Difficulty in bits (max: 64)
  101. * @param challenge Challenge string
  102. * @param challengeLength Length of challenge in bytes (max allowed: ZT_PROTO_MAX_PACKET_LENGTH)
  103. * @param result Buffer to fill with 16-byte result
  104. */
  105. static void computeSalsa2012Sha512ProofOfWork(unsigned int difficulty,const void *challenge,unsigned int challengeLength,unsigned char result[16]);
  106. /**
  107. * Verify the result of Salsa20/12+SHA512 proof of work
  108. *
  109. * @param difficulty Difficulty in bits (max: 64)
  110. * @param challenge Challenge bytes
  111. * @param challengeLength Length of challenge in bytes (max allowed: ZT_PROTO_MAX_PACKET_LENGTH)
  112. * @param proposedResult Result supplied by client
  113. * @return True if result is valid
  114. */
  115. static bool testSalsa2012Sha512ProofOfWorkResult(unsigned int difficulty,const void *challenge,unsigned int challengeLength,const unsigned char proposedResult[16]);
  116. private:
  117. // These are called internally to handle packet contents once it has
  118. // been authenticated, decrypted, decompressed, and classified.
  119. bool _doERROR(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  120. bool _doHELLO(const RuntimeEnvironment *RR,SharedPtr<Peer> &peer); // can be called with NULL peer, while all others cannot
  121. bool _doOK(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  122. bool _doWHOIS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  123. bool _doRENDEZVOUS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  124. bool _doFRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  125. bool _doEXT_FRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  126. bool _doECHO(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  127. bool _doMULTICAST_LIKE(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  128. bool _doNETWORK_MEMBERSHIP_CERTIFICATE(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  129. bool _doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  130. bool _doNETWORK_CONFIG_REFRESH(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  131. bool _doMULTICAST_GATHER(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  132. bool _doMULTICAST_FRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  133. bool _doPUSH_DIRECT_PATHS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  134. bool _doCIRCUIT_TEST(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  135. bool _doCIRCUIT_TEST_REPORT(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  136. bool _doREQUEST_PROOF_OF_WORK(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  137. // Send an ERROR_NEED_MEMBERSHIP_CERTIFICATE to a peer indicating that an updated cert is needed to communicate
  138. void _sendErrorNeedCertificate(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer,uint64_t nwid);
  139. uint64_t _receiveTime;
  140. InetAddress _localAddress;
  141. InetAddress _remoteAddress;
  142. AtomicCounter __refCount;
  143. };
  144. } // namespace ZeroTier
  145. #endif