-
Notifications
You must be signed in to change notification settings - Fork 44
/
miniwebsock.c
284 lines (232 loc) · 5.23 KB
/
miniwebsock.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "miniwebsock.h"
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <glib.h>
const struct frame_header_0
final_frame = { 0x88, 0x80, 0x0 };
gchar*
encode_by_mask(
const gchar* input,
guint32 mask
)
{
gchar* dup = g_strdup(input);
gint64 size = strlen(input);
gchar* it = dup;
gchar *mask_holder = (gchar*)&mask;
guint32 *temp;
while ((size - 4) >= 0)
{
temp = (guint32*)it;
*temp ^= mask;
size -= 4;
it += 4;
}
switch (size)
{
case 3: it[2] ^= mask_holder[2];
case 2: it[1] ^= mask_holder[1];
case 1: it[0] ^= mask_holder[0];
case 0:
break;
}
return dup;
}
guint64
create_frame_from_text(
const gchar* txt,
guchar** out
)
{
GRand* rnd = g_rand_new ();
guint32 mask_key = g_rand_int(rnd);
guint64 str_size = strlen(txt);
guchar* data;
int header_size;
if (str_size > 125)
{
if (str_size > 0xffff)
{
struct frame_header_8 header;
header.header = 0x81;
header.mask_len = 0xff;
header.len = str_size;
header.mask = mask_key;
header_size = 14;
data = g_new0(guchar, header_size + str_size);
memcpy(data, &header, header_size);
}
else
{
struct frame_header_2 header;
header.header = 0x81;
header.mask_len = 0xfe;
header.len = (guint16) str_size;
header.mask = mask_key;
header_size = 8;
data = g_new0(guchar, header_size + str_size);
memcpy(data, &header, header_size);
}
} else
{
struct frame_header_0 header;
header.header = 0x81;
header.mask_len = 0x80 | str_size;
header.mask = mask_key;
header_size = 6;
data = g_new0(guchar, header_size + str_size);
memcpy(data, &header, header_size);
}
gchar *masked_text = encode_by_mask(txt, mask_key);
memcpy(data+header_size, masked_text, str_size);
g_free(masked_text);
g_rand_free(rnd);
*out = data;
return header_size + str_size;
}
int
start_websocket_session(
int socket_fd,
gchar *host,
gchar *req
)
{
int rezult = 0;
GRand* rnd = g_rand_new ();
guchar *key = g_new0(guchar, 16);
for (int i = 0; i < 16; i++)
key[i] = (guchar)g_rand_int_range(rnd, 0, 255);
gchar* ekey = g_base64_encode(key, 16);
GString *request = g_string_new(NULL);
g_string_append_printf(request, "GET %s HTTP/1.1\r\n", req);
g_string_append_printf(request, "Host: %s\r\n", host);
g_string_append_printf(request, "Upgrade: websocket\r\n");
g_string_append_printf(request, "Connection: Upgrade\r\n");
g_string_append_printf(request, "Sec-WebSocket-Key: %s\r\n", ekey);
//g_string_append_printf(request, "Sec-WebSocket-Protocol: chat, superchat\r\n");
g_string_append_printf(request, "Sec-WebSocket-Version: 13\r\n\r\n");
int n = write(socket_fd, request->str, request->len);
if (n < 0)
return 0;
char chunk[256];
GString *fullans = g_string_new(NULL);
int flags = fcntl(socket_fd, F_GETFL, 0);
fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK);
char* rnrn_found = NULL;
while (!rnrn_found)
{
n = read(socket_fd, chunk, 255);
if (n > 0)
{
chunk[n] = '\0';
rnrn_found = strstr(chunk, "\r\n\r\n");
fullans = g_string_append(fullans, chunk);
}
}
gchar** params = g_strsplit(fullans->str, "\r\n", -1);
gchar** first = g_strsplit(params[0], " ", -1);
guint code = g_ascii_strtoll(first[1], NULL, 10);
if (code == 101)
{
// TODO Check Upgrade: websocket
// TODO Check Connection: Upgrade
// TODO Check Sec-WebSocket-Accept
// TODO Check Sec-WebSocket-Extensions
// TODO Check Sec-WebSocket-Protocol
rezult = 1;
}
g_strfreev(first);
g_strfreev(params);
g_string_free(fullans, FALSE);
g_string_free(request, FALSE);
g_free(ekey);
g_free(key);
g_rand_free(rnd);
return rezult;
}
int
close_websocket_session(
int socket_fd
)
{
struct frame_header_0 fframe = final_frame;
int n = write(socket_fd, (void*)&final_frame, 6);
return n == 6;
}
frame_value
poll_frame(
int socket_fd
)
{
frame_value val;
val.data = NULL;
val.type = frame_none;
guint8 header[2];
int n = read(socket_fd, header, 2);
if (n == 2)
{
val.last = (header[0] & 0x80) ? last_frame : not_last_frame;
int opcode = header[0] & 0x0f;
switch (opcode)
{
case 0x1:
val.type = frame_text;
break;
case 0x2:
val.type = frame_binary;
break;
case 0x8:
val.type = frame_close;
break;
case 0x9:
val.type = frame_ping;
break;
case 0xa:
val.type = frame_pong;
break;
default:
val.type = frame_none;
break;
}
int masked = header[1] & 0x80;
guint32 mask = 0;
guint64 payload_length = header[1] & 0x7f;
switch (payload_length)
{
case 126:
n = read(socket_fd, &payload_length, 2); // TODO check readed
break;
case 127:
n = read(socket_fd, &payload_length, 8); // TODO check readed
break;
}
if (masked)
{
n = read(socket_fd, &mask, 4); // TODO check readed
}
guchar* temp = g_new0(guchar, payload_length); // TODO check memory malloced
n = read(socket_fd, temp, payload_length);
// TODO check readed
if (masked)
{
val.data = encode_by_mask(temp, mask);
g_free(temp);
} else
val.data = temp;
}
return val;
}
int
send_text_frame(
int socket_fd,
const char* text
)
{
guchar* frame = NULL;
gint64 len = create_frame_from_text(text, &frame);
gint64 n = write(socket_fd, (void*)frame, len);
g_free(frame);
return n == len;
}