Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013-2017, Matt Godbolt |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 2 | // All rights reserved. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 5 | // modification, are permitted provided that the following conditions are met: |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 8 | // list of conditions and the following disclaimer. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 12 | // and/or other materials provided with the distribution. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #include "internal/HybiPacketDecoder.h" |
| 27 | #include "internal/LogStream.h" |
| 28 | |
| 29 | #include <arpa/inet.h> |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 30 | #include <byteswap.h> |
| 31 | #include <cstring> |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 32 | |
| 33 | namespace seasocks { |
| 34 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 35 | HybiPacketDecoder::HybiPacketDecoder(Logger& logger, |
| 36 | const std::vector<uint8_t>& buffer) |
| 37 | : _logger(logger), |
| 38 | _buffer(buffer), |
| 39 | _messageStart(0) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 42 | HybiPacketDecoder::MessageState HybiPacketDecoder::decodeNextMessage( |
| 43 | std::vector<uint8_t>& messageOut, bool& deflateNeeded) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 44 | if (_messageStart + 1 >= _buffer.size()) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 45 | return MessageState::NoMessage; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 46 | } |
| 47 | if ((_buffer[_messageStart] & 0x80) == 0) { |
| 48 | // FIN bit is not clear... |
| 49 | // TODO: support |
| 50 | LS_WARNING(&_logger, "Received hybi frame without FIN bit set - unsupported"); |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 51 | return MessageState::Error; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 52 | } |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 53 | |
| 54 | auto reservedBits = _buffer[_messageStart] & (7 << 4); |
| 55 | if ((reservedBits & 0x30) != 0) { |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 56 | LS_WARNING(&_logger, "Received hybi frame with reserved bits set - error"); |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 57 | return MessageState::Error; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 58 | } |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 59 | |
| 60 | deflateNeeded = !!(reservedBits & 0x40); |
| 61 | |
| 62 | auto opcode = static_cast<Opcode>(_buffer[_messageStart] & 0xf); |
| 63 | size_t payloadLength = _buffer[_messageStart + 1] & 0x7fu; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 64 | auto maskBit = _buffer[_messageStart + 1] & 0x80; |
| 65 | auto ptr = _messageStart + 2; |
| 66 | if (payloadLength == 126) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 67 | if (_buffer.size() < 4) { |
| 68 | return MessageState::NoMessage; |
| 69 | } |
| 70 | uint16_t raw_length; |
| 71 | memcpy(&raw_length, &_buffer[ptr], sizeof(raw_length)); |
| 72 | payloadLength = htons(raw_length); |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 73 | ptr += 2; |
| 74 | } else if (payloadLength == 127) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 75 | if (_buffer.size() < 10) { |
| 76 | return MessageState::NoMessage; |
| 77 | } |
| 78 | uint64_t raw_length; |
| 79 | memcpy(&raw_length, &_buffer[ptr], sizeof(raw_length)); |
| 80 | payloadLength = __bswap_64(raw_length); |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 81 | ptr += 8; |
| 82 | } |
| 83 | uint32_t mask = 0; |
| 84 | if (maskBit) { |
| 85 | // MASK is set. |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 86 | if (_buffer.size() < ptr + 4) { |
| 87 | return MessageState::NoMessage; |
| 88 | } |
| 89 | uint32_t raw_length; |
| 90 | memcpy(&raw_length, &_buffer[ptr], sizeof(raw_length)); |
| 91 | mask = htonl(raw_length); |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 92 | ptr += 4; |
| 93 | } |
| 94 | auto bytesLeftInBuffer = _buffer.size() - ptr; |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 95 | if (payloadLength > bytesLeftInBuffer) { |
| 96 | return MessageState::NoMessage; |
| 97 | } |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 98 | |
| 99 | messageOut.clear(); |
| 100 | messageOut.reserve(payloadLength); |
| 101 | for (auto i = 0u; i < payloadLength; ++i) { |
| 102 | auto byteShift = (3 - (i & 3)) * 8; |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 103 | messageOut.push_back(static_cast<uint8_t>((_buffer[ptr++] ^ (mask >> byteShift)) & 0xff)); |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 104 | } |
| 105 | _messageStart = ptr; |
| 106 | switch (opcode) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 107 | default: |
| 108 | LS_WARNING(&_logger, "Received hybi frame with unknown opcode " |
| 109 | << static_cast<int>(opcode)); |
| 110 | return MessageState::Error; |
| 111 | case Opcode::Text: |
| 112 | return MessageState::TextMessage; |
| 113 | case Opcode::Binary: |
| 114 | return MessageState::BinaryMessage; |
| 115 | case Opcode::Ping: |
| 116 | return MessageState::Ping; |
| 117 | case Opcode::Pong: |
| 118 | return MessageState::Pong; |
| 119 | case Opcode::Close: |
| 120 | return MessageState::Close; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | size_t HybiPacketDecoder::numBytesDecoded() const { |
| 125 | return _messageStart; |
| 126 | } |
| 127 | |
| 128 | } |