PacketDecoder.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. template<unsigned int C2>
  47. PacketDecoder(const Buffer<C2> &b,Demarc::Port localPort,const InetAddress &remoteAddress)
  48. throw(std::out_of_range) :
  49. Packet(b),
  50. _receiveTime(Utils::now()),
  51. _localPort(localPort),
  52. _remoteAddress(remoteAddress),
  53. _step(DECODE_STEP_WAITING_FOR_SENDER_LOOKUP),
  54. __refCount()
  55. {
  56. }
  57. /**
  58. * Attempt to decode this packet
  59. *
  60. * @param _r Runtime environment
  61. * @return True if decoding and processing is complete, false on failure (try again)
  62. */
  63. bool tryDecode(const RuntimeEnvironment *_r)
  64. throw(std::out_of_range,std::runtime_error);
  65. /**
  66. * @return Time of packet receipt
  67. */
  68. inline uint64_t receiveTime() const throw() { return _receiveTime; }
  69. private:
  70. struct _CBaddPeerFromHello_Data
  71. {
  72. const RuntimeEnvironment *renv;
  73. Address source;
  74. InetAddress remoteAddress;
  75. int localPort;
  76. unsigned int vMajor,vMinor,vRevision;
  77. uint64_t helloPacketId;
  78. uint64_t helloTimestamp;
  79. };
  80. static void _CBaddPeerFromHello(
  81. void *arg, // _CBaddPeerFromHello_Data
  82. const SharedPtr<Peer> &p,
  83. Topology::PeerVerifyResult result);
  84. static void _CBaddPeerFromWhois(
  85. void *arg, // RuntimeEnvironment
  86. const SharedPtr<Peer> &p,
  87. Topology::PeerVerifyResult result);
  88. bool _doERROR(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  89. bool _doHELLO(const RuntimeEnvironment *_r);
  90. bool _doOK(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  91. bool _doWHOIS(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  92. bool _doRENDEZVOUS(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  93. bool _doFRAME(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  94. bool _doMULTICAST_LIKE(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  95. bool _doMULTICAST_FRAME(const RuntimeEnvironment *_r,const SharedPtr<Peer> &peer);
  96. uint64_t _receiveTime;
  97. Demarc::Port _localPort;
  98. InetAddress _remoteAddress;
  99. enum {
  100. DECODE_STEP_WAITING_FOR_SENDER_LOOKUP, // on initial receipt, we need peer's identity
  101. DECODE_STEP_WAITING_FOR_ORIGINAL_SUBMITTER_LOOKUP // this only applies to MULTICAST_FRAME
  102. } _step;
  103. AtomicCounter __refCount;
  104. };
  105. } // namespace ZeroTier
  106. #endif