PacketDecoder.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. namespace ZeroTier {
  38. class RuntimeEnvironment;
  39. /**
  40. * Subclass of packet that handles the decoding of it
  41. */
  42. class PacketDecoder : public Packet
  43. {
  44. friend class SharedPtr<PacketDecoder>;
  45. public:
  46. /**
  47. * Create a new packet-in-decode
  48. *
  49. * @param b Source buffer with raw packet data
  50. * @param localPort Local port on which packet was received
  51. * @param remoteAddress Address from which packet came
  52. * @throws std::out_of_range Range error processing packet
  53. */
  54. template<unsigned int C2>
  55. PacketDecoder(const Buffer<C2> &b,Demarc::Port localPort,const InetAddress &remoteAddress)
  56. throw(std::out_of_range) :
  57. Packet(b),
  58. _receiveTime(Utils::now()),
  59. _localPort(localPort),
  60. _remoteAddress(remoteAddress),
  61. _step(DECODE_WAITING_FOR_SENDER_LOOKUP),
  62. __refCount()
  63. {
  64. }
  65. /**
  66. * Attempt to decode this packet
  67. *
  68. * Note that this returns 'true' if processing is complete. This says nothing
  69. * about whether the packet was valid. A rejection is 'complete.'
  70. *
  71. * Once true is returned, this should not be called again.
  72. *
  73. * @param _r Runtime environment
  74. * @return True if decoding and processing is complete, false if caller should try again
  75. * @throws std::out_of_range Range error processing packet (should be discarded)
  76. * @throws std::runtime_error Other error processing packet (should be discarded)
  77. */
  78. bool tryDecode(const RuntimeEnvironment *_r)
  79. throw(std::out_of_range,std::runtime_error);
  80. /**
  81. * @return Time of packet receipt
  82. */
  83. inline uint64_t receiveTime() const throw() { return _receiveTime; }
  84. private:
  85. struct _CBaddPeerFromHello_Data
  86. {
  87. const RuntimeEnvironment *renv;
  88. Address source;
  89. InetAddress remoteAddress;
  90. int localPort;
  91. unsigned int vMajor,vMinor,vRevision;
  92. uint64_t helloPacketId;
  93. uint64_t helloTimestamp;
  94. };
  95. static void _CBaddPeerFromHello(
  96. void *arg, // _CBaddPeerFromHello_Data
  97. const SharedPtr<Peer> &p,
  98. Topology::PeerVerifyResult result);
  99. static void _CBaddPeerFromWhois(
  100. void *arg, // RuntimeEnvironment
  101. const SharedPtr<Peer> &p,
  102. Topology::PeerVerifyResult result);
  103. // These are called internally to handle packet contents once it has
  104. // been authenticated, decrypted, decompressed, and classified.
  105. bool _doERROR(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  106. bool _doHELLO(const RuntimeEnvironment *_r);
  107. bool _doOK(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  108. bool _doWHOIS(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  109. bool _doRENDEZVOUS(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  110. bool _doFRAME(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  111. bool _doMULTICAST_LIKE(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  112. bool _doMULTICAST_FRAME(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  113. bool _doNETWORK_MEMBERSHIP_CERTIFICATE(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  114. bool _doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  115. bool _doNETWORK_CONFIG_REFRESH(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  116. uint64_t _receiveTime;
  117. Demarc::Port _localPort;
  118. InetAddress _remoteAddress;
  119. enum {
  120. DECODE_WAITING_FOR_SENDER_LOOKUP, // on initial receipt, we need peer's identity
  121. DECODE_WAITING_FOR_MULTICAST_FRAME_ORIGINAL_SENDER_LOOKUP,
  122. } _step;
  123. AtomicCounter __refCount;
  124. };
  125. } // namespace ZeroTier
  126. #endif