IncomingPacket.hpp 7.3 KB

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