IncomingPacket.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.
  110. *
  111. * @param RR Runtime environment
  112. * @return True if decoding and processing is complete, false if caller should try again
  113. */
  114. bool tryDecode(const RuntimeEnvironment *RR);
  115. /**
  116. * @return Time of packet receipt / start of decode
  117. */
  118. inline uint64_t receiveTime() const throw() { return _receiveTime; }
  119. /**
  120. * Compute the Salsa20/12+SHA512 proof of work function
  121. *
  122. * @param difficulty Difficulty in bits (max: 64)
  123. * @param challenge Challenge string
  124. * @param challengeLength Length of challenge in bytes (max allowed: ZT_PROTO_MAX_PACKET_LENGTH)
  125. * @param result Buffer to fill with 16-byte result
  126. */
  127. static void computeSalsa2012Sha512ProofOfWork(unsigned int difficulty,const void *challenge,unsigned int challengeLength,unsigned char result[16]);
  128. /**
  129. * Verify the result of Salsa20/12+SHA512 proof of work
  130. *
  131. * @param difficulty Difficulty in bits (max: 64)
  132. * @param challenge Challenge bytes
  133. * @param challengeLength Length of challenge in bytes (max allowed: ZT_PROTO_MAX_PACKET_LENGTH)
  134. * @param proposedResult Result supplied by client
  135. * @return True if result is valid
  136. */
  137. static bool testSalsa2012Sha512ProofOfWorkResult(unsigned int difficulty,const void *challenge,unsigned int challengeLength,const unsigned char proposedResult[16]);
  138. private:
  139. // These are called internally to handle packet contents once it has
  140. // been authenticated, decrypted, decompressed, and classified.
  141. bool _doERROR(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  142. bool _doHELLO(const RuntimeEnvironment *RR,SharedPtr<Peer> &peer); // can be called with NULL peer, while all others cannot
  143. bool _doOK(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  144. bool _doWHOIS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  145. bool _doRENDEZVOUS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  146. bool _doFRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  147. bool _doEXT_FRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  148. bool _doECHO(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  149. bool _doMULTICAST_LIKE(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  150. bool _doNETWORK_CREDENTIALS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  151. bool _doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  152. bool _doNETWORK_CONFIG_REFRESH(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  153. bool _doMULTICAST_GATHER(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  154. bool _doMULTICAST_FRAME(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  155. bool _doPUSH_DIRECT_PATHS(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  156. bool _doCIRCUIT_TEST(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  157. bool _doCIRCUIT_TEST_REPORT(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  158. bool _doREQUEST_PROOF_OF_WORK(const RuntimeEnvironment *RR,const SharedPtr<Peer> &peer);
  159. uint64_t _receiveTime;
  160. InetAddress _localAddress;
  161. InetAddress _remoteAddress;
  162. };
  163. } // namespace ZeroTier
  164. #endif