Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013, Matt Godbolt |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 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 |
| 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #include "internal/HybiPacketDecoder.h" |
| 27 | #include "internal/LogStream.h" |
| 28 | |
| 29 | #include <arpa/inet.h> |
| 30 | |
| 31 | namespace seasocks { |
| 32 | |
| 33 | HybiPacketDecoder::HybiPacketDecoder(Logger& logger, const std::vector<uint8_t>& buffer) : |
| 34 | _logger(logger), |
| 35 | _buffer(buffer), |
| 36 | _messageStart(0) { |
| 37 | } |
| 38 | |
| 39 | HybiPacketDecoder::MessageState HybiPacketDecoder::decodeNextMessage(std::vector<uint8_t>& messageOut) { |
| 40 | if (_messageStart + 1 >= _buffer.size()) { |
| 41 | return NoMessage; |
| 42 | } |
| 43 | if ((_buffer[_messageStart] & 0x80) == 0) { |
| 44 | // FIN bit is not clear... |
| 45 | // TODO: support |
| 46 | LS_WARNING(&_logger, "Received hybi frame without FIN bit set - unsupported"); |
| 47 | return Error; |
| 48 | } |
| 49 | if ((_buffer[_messageStart] & (7<<4)) != 0) { |
| 50 | LS_WARNING(&_logger, "Received hybi frame with reserved bits set - error"); |
| 51 | return Error; |
| 52 | } |
| 53 | auto opcode = _buffer[_messageStart] & 0xf; |
| 54 | size_t payloadLength = _buffer[_messageStart + 1] & 0x7f; |
| 55 | auto maskBit = _buffer[_messageStart + 1] & 0x80; |
| 56 | auto ptr = _messageStart + 2; |
| 57 | if (payloadLength == 126) { |
| 58 | if (_buffer.size() < 4) { return NoMessage; } |
| 59 | payloadLength = htons(*reinterpret_cast<const uint16_t*>(&_buffer[ptr])); |
| 60 | ptr += 2; |
| 61 | } else if (payloadLength == 127) { |
| 62 | if (_buffer.size() < 10) { return NoMessage; } |
| 63 | payloadLength = __bswap_64(*reinterpret_cast<const uint64_t*>(&_buffer[ptr])); |
| 64 | ptr += 8; |
| 65 | } |
| 66 | uint32_t mask = 0; |
| 67 | if (maskBit) { |
| 68 | // MASK is set. |
| 69 | if (_buffer.size() < ptr + 4) { return NoMessage; } |
| 70 | mask = htonl(*reinterpret_cast<const uint32_t*>(&_buffer[ptr])); |
| 71 | ptr += 4; |
| 72 | } |
| 73 | auto bytesLeftInBuffer = _buffer.size() - ptr; |
| 74 | if (payloadLength > bytesLeftInBuffer) { return NoMessage; } |
| 75 | |
| 76 | messageOut.clear(); |
| 77 | messageOut.reserve(payloadLength); |
| 78 | for (auto i = 0u; i < payloadLength; ++i) { |
| 79 | auto byteShift = (3 - (i & 3)) * 8; |
| 80 | messageOut.push_back(static_cast<char>((_buffer[ptr++] ^ (mask >> byteShift)) & 0xff)); |
| 81 | } |
| 82 | _messageStart = ptr; |
| 83 | switch (opcode) { |
| 84 | default: |
| 85 | LS_WARNING(&_logger, "Received hybi frame with unknown opcode " << opcode); |
| 86 | return Error; |
| 87 | case OPCODE_TEXT: |
| 88 | return TextMessage; |
| 89 | case OPCODE_BINARY: |
| 90 | return BinaryMessage; |
| 91 | case OPCODE_PING: |
| 92 | return Ping; |
| 93 | case OPCODE_CLOSE: |
| 94 | return Close; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | size_t HybiPacketDecoder::numBytesDecoded() const { |
| 99 | return _messageStart; |
| 100 | } |
| 101 | |
| 102 | } |