RLPXFrameReader.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file RLPXFrameReader.cpp
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @date 2015
  17. */
  18. #include "RLPXFrameReader.h"
  19. using namespace std;
  20. using namespace dev;
  21. using namespace dev::p2p;
  22. std::vector<RLPXPacket> RLPXFrameReader::demux(RLPXFrameCoder& _coder, RLPXFrameInfo const& _info, bytesRef _frame)
  23. {
  24. if (!_coder.authAndDecryptFrame(_frame))
  25. BOOST_THROW_EXCEPTION(RLPXFrameDecryptFailed());
  26. std::vector<RLPXPacket> ret;
  27. if (_frame.empty())
  28. // drop: bad frame (empty)
  29. return ret;
  30. if (_info.multiFrame && _info.totalLength && (_frame.size() - (h128::size + _info.padding) > _info.totalLength))
  31. // drop: bad frame (too large)
  32. return ret;
  33. // trim mac
  34. bytesConstRef buffer = _frame.cropped(0, _frame.size() - (h128::size + _info.padding));
  35. // continue populating multiframe packets
  36. if (_info.multiFrame && m_incomplete.count(_info.sequenceId))
  37. {
  38. uint32_t& remaining = m_incomplete.at(_info.sequenceId).second;
  39. if (!_info.totalLength && buffer.size() > 0 && buffer.size() <= remaining)
  40. {
  41. remaining -= buffer.size();
  42. RLPXPacket& p = m_incomplete.at(_info.sequenceId).first;
  43. if(p.append(buffer) && !remaining)
  44. ret.push_back(std::move(p));
  45. if (remaining)
  46. {
  47. if (!ret.empty())
  48. BOOST_THROW_EXCEPTION(RLPXInvalidPacket());
  49. }
  50. else
  51. {
  52. m_incomplete.erase(_info.sequenceId);
  53. if (ret.empty())
  54. BOOST_THROW_EXCEPTION(RLPXInvalidPacket());
  55. }
  56. return ret;
  57. }
  58. else
  59. m_incomplete.erase(_info.sequenceId);
  60. }
  61. while (!buffer.empty())
  62. {
  63. auto type = nextRLP(buffer);
  64. if (type.empty())
  65. break;
  66. buffer = buffer.cropped(type.size());
  67. // consume entire buffer if packet has sequence
  68. auto packet = _info.multiFrame ? buffer : nextRLP(buffer);
  69. buffer = buffer.cropped(packet.size());
  70. RLPXPacket p(m_protocolType, type);
  71. if (!packet.empty())
  72. p.append(packet);
  73. uint32_t remaining = _info.totalLength - type.size() - packet.size();
  74. if (p.isValid())
  75. ret.push_back(std::move(p));
  76. else if (_info.multiFrame && remaining)
  77. m_incomplete.insert(std::make_pair(_info.sequenceId, std::make_pair(std::move(p), remaining)));
  78. else
  79. BOOST_THROW_EXCEPTION(RLPXInvalidPacket());
  80. }
  81. return ret;
  82. }