forked from agrafix/WebSocketServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybi10.cpp
211 lines (167 loc) · 6.41 KB
/
hybi10.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* ,. , ,. . .---. . . .---.
* `| /| / ,-. |-. \___ ,-. ,-. | , ,-. |- \___ ,-. ,-. . , ,-. ,-.
* | / | / |-' | | \ | | | |< |-' | \ |-' | | / |-' |
* `' `' `-' ^-' `---' `-' `-' ' ` `-' `' `---' `-' ' `' `-' '
*
* Copyright 2012 by Alexander Thiemann <[email protected]>
*
* This file is part of WebSocketServer.
*
* WebSocketServer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WebSocketServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WebSocketServer. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "string.h"
#include "hybi10.h"
namespace hybi10 {
namespace { // local namespace
unsigned char randomChar() {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
return alphanum[rand() % (sizeof(alphanum) - 1)];
}
} // end
std::string encode(request hRequest, bool masked) {
std::stringstream sstr;
// set header flags: fin, rsv, opcode
unsigned char head;
if (hRequest.type == "text") {
head = 129; // 10000001
}
else if (hRequest.type == "close") {
head = 136; // 10001000
}
else if (hRequest.type == "ping") {
head = 137; // 10001001
}
else if (hRequest.type == "pong") {
head = 138; // 10001010
}
sstr << head;
// set payload info
size_t length = hRequest.payload.length();
if (length > 65535) {
// 9 bytes needed
unsigned long len = length;
unsigned char b1 = (masked ? 255 : 127);
sstr << b1;
for (int i = 7; i >= 0; i--) {
unsigned char b;
for (int j = 0; j < 8; j++) {
unsigned char shift = 0x01 << j;
shift = shift << (8 * i);
b += pow(2, j) * (len&shift);
}
if (i == 7 && b > 127) // frame too big
{
return "";
}
sstr << b;
}
}
else if (length > 125) {
// 3 bytes needed
unsigned short len = length;
unsigned char b1 = (masked ? 254 : 126);
unsigned char b2 = 128 * (len&0x8000) + 64 * (len&0x4000) + 32 * (len&0x2000) + 16 * (len&0x1000);
b2 += 8 * (len&0x800) + 4*(len&0x400) + 2*(len&0x200) + (len&0x100);
unsigned char b3 = 128 * (len&0x80) + 64 * (len&0x40) + 32 * (len&0x20) + 16 * (len&0x10);
b3 += 8 * (len&0x08) + 4*(len&0x04) + 2*(len&0x02) + (len&0x01);
sstr << b1;
sstr << b2;
sstr << b3;
}
else {
// 1 byte needed
unsigned char b = (masked ? length + 128 : length);
sstr << b;
}
unsigned char mask[4];
if (masked) {
// generate mask
for (int i = 0; i < 4; i++) {
mask[i] = randomChar();
sstr << mask[i];
}
}
for (int i = 0; i < length; i++) {
unsigned char chr = hRequest.payload[i];
unsigned char res = (masked ? (chr ^ mask[i % 4]) : chr);
sstr << res;
}
return sstr.str();
}
request decode(std::string data) {
// output
request Decoded;
Decoded.type = "Unknown";
Decoded.exitcode = 0;
Decoded.payload = "";
// header format
hybiPayloadHeader* pload;
pload = (struct hybiPayloadHeader*)data.substr(0, 2).c_str();
printf("Header: OPCODE %x MASKED? %d, LENGTH: %d\n", pload->opcode, pload->mask, pload->payload_len);
// check the op-code
if (pload->opcode == 1) {
Decoded.type = "text";
} else if (pload->opcode == 8) {
Decoded.type = "close";
} else if (pload->opcode == 9) {
Decoded.type = "ping";
} else if (pload->opcode == 10) {
Decoded.type = "pong";
} else {
Decoded.type = "unknown opcode";
Decoded.exitcode = 1003;
return Decoded;
}
std::string payloadBody = data.substr(2); // remove 2 byte long header
std::string payloadMask;
int payload_offset = 0;
if (pload->payload_len == 126)
{
// 2 byte payload length
payload_offset += 2;
}
else if (pload->payload_len == 127)
{
// 8 byte payload length
payload_offset += 8;
}
printf("Payload offset: %d \n", payload_offset);
if ((data[1]&0x80))
{
// extract the mask
payloadMask = payloadBody.substr(payload_offset, 4);
payload_offset += 4; // mask key is 4 bytes long
}
printf("Payload offset: %d \n", payload_offset);
payloadBody = payloadBody.substr(payload_offset);
printf("MASK: %s PAYLOAD: %s \n", payloadMask.c_str(), payloadBody.c_str());
// unmask payloadBody
int payloadBodyLength = payloadBody.length();
std::stringstream unm;
for (int i = 0; i < payloadBodyLength; i++) {
unsigned char d = payloadBody[i];
unsigned char m = payloadMask[i % 4];
unsigned char unmasked = d ^ m;
unm << unmasked;
}
printf("UNMASKED: %s \n", unm.str().c_str());
Decoded.payload = unm.str();
return Decoded;
}
}