blob: 80977cd5f2c8e809aea28da1fc4ddf97f430205a [file] [log] [blame]
Austin Schuh9d823002019-04-14 12:53:17 -07001// Copyright (c) 2013-2017, Matt Godbolt
Austin Schuh24adb6b2015-09-06 17:37:40 -07002// All rights reserved.
Austin Schuh9d823002019-04-14 12:53:17 -07003//
4// Redistribution and use in source and binary forms, with or without
Austin Schuh24adb6b2015-09-06 17:37:40 -07005// modification, are permitted provided that the following conditions are met:
Austin Schuh9d823002019-04-14 12:53:17 -07006//
7// Redistributions of source code must retain the above copyright notice, this
Austin Schuh24adb6b2015-09-06 17:37:40 -07008// list of conditions and the following disclaimer.
Austin Schuh9d823002019-04-14 12:53:17 -07009//
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
Austin Schuh24adb6b2015-09-06 17:37:40 -070012// and/or other materials provided with the distribution.
Austin Schuh9d823002019-04-14 12:53:17 -070013//
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 Schuh24adb6b2015-09-06 17:37:40 -070024// POSSIBILITY OF SUCH DAMAGE.
25
26#include "internal/HybiPacketDecoder.h"
27#include "internal/LogStream.h"
28
29#include <arpa/inet.h>
Austin Schuh9d823002019-04-14 12:53:17 -070030#include <byteswap.h>
31#include <cstring>
Austin Schuh24adb6b2015-09-06 17:37:40 -070032
33namespace seasocks {
34
Austin Schuh9d823002019-04-14 12:53:17 -070035HybiPacketDecoder::HybiPacketDecoder(Logger& logger,
36 const std::vector<uint8_t>& buffer)
37 : _logger(logger),
38 _buffer(buffer),
39 _messageStart(0) {
Austin Schuh24adb6b2015-09-06 17:37:40 -070040}
41
Austin Schuh9d823002019-04-14 12:53:17 -070042HybiPacketDecoder::MessageState HybiPacketDecoder::decodeNextMessage(
43 std::vector<uint8_t>& messageOut, bool& deflateNeeded) {
Austin Schuh24adb6b2015-09-06 17:37:40 -070044 if (_messageStart + 1 >= _buffer.size()) {
Austin Schuh9d823002019-04-14 12:53:17 -070045 return MessageState::NoMessage;
Austin Schuh24adb6b2015-09-06 17:37:40 -070046 }
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 Schuh9d823002019-04-14 12:53:17 -070051 return MessageState::Error;
Austin Schuh24adb6b2015-09-06 17:37:40 -070052 }
Austin Schuh9d823002019-04-14 12:53:17 -070053
54 auto reservedBits = _buffer[_messageStart] & (7 << 4);
55 if ((reservedBits & 0x30) != 0) {
Austin Schuh24adb6b2015-09-06 17:37:40 -070056 LS_WARNING(&_logger, "Received hybi frame with reserved bits set - error");
Austin Schuh9d823002019-04-14 12:53:17 -070057 return MessageState::Error;
Austin Schuh24adb6b2015-09-06 17:37:40 -070058 }
Austin Schuh9d823002019-04-14 12:53:17 -070059
60 deflateNeeded = !!(reservedBits & 0x40);
61
62 auto opcode = static_cast<Opcode>(_buffer[_messageStart] & 0xf);
63 size_t payloadLength = _buffer[_messageStart + 1] & 0x7fu;
Austin Schuh24adb6b2015-09-06 17:37:40 -070064 auto maskBit = _buffer[_messageStart + 1] & 0x80;
65 auto ptr = _messageStart + 2;
66 if (payloadLength == 126) {
Austin Schuh9d823002019-04-14 12:53:17 -070067 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 Schuh24adb6b2015-09-06 17:37:40 -070073 ptr += 2;
74 } else if (payloadLength == 127) {
Austin Schuh9d823002019-04-14 12:53:17 -070075 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 Schuh24adb6b2015-09-06 17:37:40 -070081 ptr += 8;
82 }
83 uint32_t mask = 0;
84 if (maskBit) {
85 // MASK is set.
Austin Schuh9d823002019-04-14 12:53:17 -070086 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 Schuh24adb6b2015-09-06 17:37:40 -070092 ptr += 4;
93 }
94 auto bytesLeftInBuffer = _buffer.size() - ptr;
Austin Schuh9d823002019-04-14 12:53:17 -070095 if (payloadLength > bytesLeftInBuffer) {
96 return MessageState::NoMessage;
97 }
Austin Schuh24adb6b2015-09-06 17:37:40 -070098
99 messageOut.clear();
100 messageOut.reserve(payloadLength);
101 for (auto i = 0u; i < payloadLength; ++i) {
102 auto byteShift = (3 - (i & 3)) * 8;
Austin Schuh9d823002019-04-14 12:53:17 -0700103 messageOut.push_back(static_cast<uint8_t>((_buffer[ptr++] ^ (mask >> byteShift)) & 0xff));
Austin Schuh24adb6b2015-09-06 17:37:40 -0700104 }
105 _messageStart = ptr;
106 switch (opcode) {
Austin Schuh9d823002019-04-14 12:53:17 -0700107 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 Schuh24adb6b2015-09-06 17:37:40 -0700121 }
122}
123
124size_t HybiPacketDecoder::numBytesDecoded() const {
125 return _messageStart;
126}
127
128}