-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket.c
343 lines (274 loc) · 6.41 KB
/
websocket.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#define _GNU_SOURCE
#include <assert.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include "program.h"
#include "websocket.h"
#define container_of(ptr, type, member) (type *)((char *)ptr - offsetof(type, member))
#define ARRAY_SIZE(array) (sizeof array / sizeof *array)
#define WS_FIN_TEXT UINT8_C(0x81)
#define WS_MASK_PRESENT UINT8_C(0x80)
static int ws = -1;
static uint8_t *buf;
static uint64_t frame_ptr,
/* <= */ buf_size,
/* <= */ buf_alloced;
int
ws_fileno(void)
{
return ws;
}
int
ws_is_alive(void)
{
return 0 <= ws;
}
void
ws_close(void)
{
close(ws), ws = -1;
on_ws_close();
}
static int
writev_all(int fd, struct iovec *iov, int nb_iov)
{
for (;;) {
ssize_t got = writev(fd, iov, nb_iov);
if (got < 0)
return -errno;
else if (!got)
return -EBADF;
size_t written = got;
while (iov[0].iov_len <= written) {
written -= (iov++)->iov_len;
if (!--nb_iov)
return 0;
}
*(char **)&iov[0].iov_base += written;
iov[0].iov_len -= written;
}
return 0;
}
static int
ws_http_upgrade(char const *host, in_port_t port)
{
static char const HTTP_SWITCHING_PROTOCOLS[] = "HTTP/1.1 101 ";
int rc;
buf_size = (size_t)sprintf((char *)buf,
"GET /jsonrpc HTTP/1.1\r\n"
"Host:%s:%hu\r\n"
"Upgrade:websocket\r\n"
"Connection:Upgrade\r\n"
"Sec-WebSocket-Key:AQIDBAUGBwgJCgsMDQ4PEC==\r\n"
"Sec-WebSocket-Version:13\r\n"
"\r\n",
host, port);
struct iovec iov = {
.iov_base = buf,
.iov_len = buf_size,
};
rc = writev_all(ws, &iov, 1);
if (rc < 0) {
show_error("%s", strerror(-rc));
return rc;
}
/* Make sure buf always starts with the good prefix. */
memcpy(buf, HTTP_SWITCHING_PROTOCOLS, strlen(HTTP_SWITCHING_PROTOCOLS));
buf_size = 0;
for (;;) {
ssize_t got = read(ws, buf, buf_alloced - buf_size);
if (got < 0) {
rc = errno;
show_error("%s", strerror(rc));
return -rc;
} else if (!got)
return -EBADF;
buf_size += (size_t)got;
if (memcmp(buf, HTTP_SWITCHING_PROTOCOLS, strlen(HTTP_SWITCHING_PROTOCOLS))) {
show_error("WebSocket connection refused");
return -EINVAL;
}
uint8_t *hdr_end = memmem(buf, buf_size, "\r\n\r\n", 4);
if (hdr_end) {
size_t hdr_size = (hdr_end + 4) - buf;
memmove(buf, buf + hdr_size, (buf_size -= hdr_size));
return 0;
}
}
}
static int
ws_connect(char const *host, in_port_t port)
{
int ret;
struct addrinfo *info, hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_protocol = IPPROTO_TCP,
};
char port_str[sizeof "65536"];
assert(!ws_is_alive() && "Already connected");
sprintf(port_str, "%hu", port);
if ((ret = getaddrinfo(host, port_str, &hints, &info))) {
show_error("%s", EAI_SYSTEM == ret
? strerror(errno)
: gai_strerror(ret));
return -1;
}
for (struct addrinfo *ai = info; ai; ai = ai->ai_next) {
ws = socket(ai->ai_family,
ai->ai_socktype
#ifdef SOCK_CLOEXEC
| SOCK_CLOEXEC
#endif
, ai->ai_protocol);
if (ws < 0)
continue;
if (0 <= connect(ws, ai->ai_addr, ai->ai_addrlen))
break;
close(ws), ws = -1;
}
freeaddrinfo(info);
if (ws < 0) {
show_error(strerror(errno));
return -1;
}
show_error(NULL);
return 0;
}
void
ws_open(char const *host, in_port_t port)
{
if (ws_connect(host, port) < 0) {
on_ws_close();
return;
}
buf = realloc(buf, (buf_alloced = BUFSIZ));
if (ws_http_upgrade(host, port) < 0) {
ws_close();
return;
}
#ifndef SOCK_CLOEXEC
fcntl(ws, F_SETFD, O_CLOEXEC | fcntl(ws, F_GETFD));
#endif
fcntl(ws, F_SETFL, O_NONBLOCK | fcntl(ws, F_GETFL));
on_ws_open();
}
int
ws_send(char const *msg, size_t msg_size)
{
struct iovec iov[2];
uint8_t header[2 /* Header. */ + 8 /* uint64_t length */ + 4 /* Mask. */];
uint8_t *p = header;
iov[0].iov_base = header;
iov[1].iov_base = (uint8_t *)msg;
iov[1].iov_len = msg_size;
/* Header. */
*p++ = WS_FIN_TEXT;
/* Payload length. */
if (msg_size < 126) {
*p++ = WS_MASK_PRESENT | msg_size;
} else if (msg_size <= UINT16_MAX) {
*p++ = WS_MASK_PRESENT | 126;
uint16_t size_be = htobe16(msg_size);
memcpy(p, &size_be, sizeof size_be);
p += sizeof size_be;
} else {
*p++ = WS_MASK_PRESENT | 127;
uint64_t size_be = htobe64(msg_size);
memcpy(p, &size_be, sizeof size_be);
p += sizeof size_be;
}
/* Mask; We use 0, so XOR-ing is convenient. */
memset(p, 0, sizeof(uint32_t));
p += sizeof(uint32_t);
iov[0].iov_len = p - header;
int rc = writev_all(ws, iov, ARRAY_SIZE(iov));
if (rc < 0) {
ws_close();
return rc;
}
return 0;
}
int
ws_recv(void)
{
ssize_t got = read(ws, buf + buf_size, buf_alloced - buf_size);
if (got < 0) {
int rc = -errno;
ws_close();
return rc;
} else if (!got)
return 0;
buf_size += (size_t)got;
uint64_t frame_size;
for (;;) {
if (buf_size < frame_ptr + 2) {
/* Size unknown, use a default value. */
frame_size = UINT8_MAX;
break;
}
uint64_t payload_size;
uint8_t header_size = 2;
uint8_t *payload;
uint8_t *header = buf + frame_ptr;
if (header[1] < UINT8_C(126)) {
payload_size = header[1];
} else if (UINT8_C(126) == header[1]) {
if (buf_size < frame_ptr + (header_size += sizeof(uint16_t))) {
frame_size = 2 + 2 + UINT8_MAX;
break;
}
uint16_t size_be;
memcpy(&size_be, header + 2, sizeof size_be);
payload_size = be16toh(size_be);
} else if (UINT8_C(127) == header[1]) {
if (buf_size < frame_ptr + (header_size += sizeof(uint64_t))) {
frame_size = 2 + 8 + UINT16_MAX;
break;
}
uint64_t size_be;
memcpy(&size_be, header + 2, sizeof size_be);
payload_size = be64toh(size_be);
} else {
assert(!"Received frame has mask bit set.");
}
payload = header + header_size;
frame_size = header_size + payload_size;
/* Complete packet has been received. */
if (buf_size < frame_ptr + frame_size)
break;
frame_ptr += frame_size;
switch (header[0]) {
case WS_FIN_TEXT:
on_ws_message((char *)payload, payload_size);
break;
default:
assert(!"Unexpected WebSocket frame type");
}
}
if (frame_ptr && buf_alloced < frame_ptr + frame_size) {
memmove(buf, buf + frame_ptr, buf_size - frame_ptr);
buf_size -= frame_ptr;
frame_ptr = 0;
}
if (buf_alloced < frame_size) {
assert(!frame_ptr);
uint8_t *p = realloc(buf, frame_size);
if (!p)
return -ENOMEM;
buf = p;
buf_alloced = frame_size;
}
return 0;
}