tcg/arm: Code rearrangement
[qemu/kevin.git] / io / channel-websock.c
blob5a3badbec2bbcc1233489692a34ad76716f74931
1 /*
2 * QEMU I/O channels driver websockets
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu/bswap.h"
24 #include "io/channel-websock.h"
25 #include "crypto/hash.h"
26 #include "trace.h"
29 /* Max amount to allow in rawinput/rawoutput buffers */
30 #define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
32 #define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
33 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
34 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
36 #define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
37 #define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
38 #define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
39 #define QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE "upgrade"
40 #define QIO_CHANNEL_WEBSOCK_HEADER_HOST "host"
41 #define QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION "connection"
43 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
44 #define QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE "Upgrade"
45 #define QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET "websocket"
47 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RESPONSE \
48 "HTTP/1.1 101 Switching Protocols\r\n" \
49 "Upgrade: websocket\r\n" \
50 "Connection: Upgrade\r\n" \
51 "Sec-WebSocket-Accept: %s\r\n" \
52 "Sec-WebSocket-Protocol: binary\r\n" \
53 "\r\n"
54 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
55 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
56 #define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
57 #define QIO_CHANNEL_WEBSOCK_HTTP_METHOD "GET"
58 #define QIO_CHANNEL_WEBSOCK_HTTP_PATH "/"
59 #define QIO_CHANNEL_WEBSOCK_HTTP_VERSION "HTTP/1.1"
61 /* The websockets packet header is variable length
62 * depending on the size of the payload... */
64 /* ...length when using 7-bit payload length */
65 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
66 /* ...length when using 16-bit payload length */
67 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
68 /* ...length when using 64-bit payload length */
69 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
71 /* Length of the optional data mask field in header */
72 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
74 /* Maximum length that can fit in 7-bit payload size */
75 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
76 /* Maximum length that can fit in 16-bit payload size */
77 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
79 /* Magic 7-bit length to indicate use of 16-bit payload length */
80 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
81 /* Magic 7-bit length to indicate use of 64-bit payload length */
82 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
84 /* Bitmasks & shifts for accessing header fields */
85 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
86 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
87 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
88 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
89 #define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN 7
90 #define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK 7
92 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader;
94 struct QEMU_PACKED QIOChannelWebsockHeader {
95 unsigned char b0;
96 unsigned char b1;
97 union {
98 struct QEMU_PACKED {
99 uint16_t l16;
100 QIOChannelWebsockMask m16;
101 } s16;
102 struct QEMU_PACKED {
103 uint64_t l64;
104 QIOChannelWebsockMask m64;
105 } s64;
106 QIOChannelWebsockMask m;
107 } u;
110 typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader;
112 struct QIOChannelWebsockHTTPHeader {
113 char *name;
114 char *value;
117 enum {
118 QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION = 0x0,
119 QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME = 0x1,
120 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME = 0x2,
121 QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE = 0x8,
122 QIO_CHANNEL_WEBSOCK_OPCODE_PING = 0x9,
123 QIO_CHANNEL_WEBSOCK_OPCODE_PONG = 0xA
126 static size_t
127 qio_channel_websock_extract_headers(char *buffer,
128 QIOChannelWebsockHTTPHeader *hdrs,
129 size_t nhdrsalloc,
130 Error **errp)
132 char *nl, *sep, *tmp;
133 size_t nhdrs = 0;
136 * First parse the HTTP protocol greeting of format:
138 * $METHOD $PATH $VERSION
140 * e.g.
142 * GET / HTTP/1.1
145 nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
146 if (!nl) {
147 error_setg(errp, "Missing HTTP header delimiter");
148 return 0;
150 *nl = '\0';
152 tmp = strchr(buffer, ' ');
153 if (!tmp) {
154 error_setg(errp, "Missing HTTP path delimiter");
155 return 0;
157 *tmp = '\0';
159 if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_METHOD)) {
160 error_setg(errp, "Unsupported HTTP method %s", buffer);
161 return 0;
164 buffer = tmp + 1;
165 tmp = strchr(buffer, ' ');
166 if (!tmp) {
167 error_setg(errp, "Missing HTTP version delimiter");
168 return 0;
170 *tmp = '\0';
172 if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_PATH)) {
173 error_setg(errp, "Unexpected HTTP path %s", buffer);
174 return 0;
177 buffer = tmp + 1;
179 if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_VERSION)) {
180 error_setg(errp, "Unsupported HTTP version %s", buffer);
181 return 0;
184 buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
187 * Now parse all the header fields of format
189 * $NAME: $VALUE
191 * e.g.
193 * Cache-control: no-cache
195 do {
196 QIOChannelWebsockHTTPHeader *hdr;
198 nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
199 if (nl) {
200 *nl = '\0';
203 sep = strchr(buffer, ':');
204 if (!sep) {
205 error_setg(errp, "Malformed HTTP header");
206 return 0;
208 *sep = '\0';
209 sep++;
210 while (*sep == ' ') {
211 sep++;
214 if (nhdrs >= nhdrsalloc) {
215 error_setg(errp, "Too many HTTP headers");
216 return 0;
219 hdr = &hdrs[nhdrs++];
220 hdr->name = buffer;
221 hdr->value = sep;
223 /* Canonicalize header name for easier identification later */
224 for (tmp = hdr->name; *tmp; tmp++) {
225 *tmp = g_ascii_tolower(*tmp);
228 if (nl) {
229 buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
231 } while (nl != NULL);
233 return nhdrs;
236 static const char *
237 qio_channel_websock_find_header(QIOChannelWebsockHTTPHeader *hdrs,
238 size_t nhdrs,
239 const char *name)
241 size_t i;
243 for (i = 0; i < nhdrs; i++) {
244 if (g_str_equal(hdrs[i].name, name)) {
245 return hdrs[i].value;
249 return NULL;
253 static int qio_channel_websock_handshake_send_response(QIOChannelWebsock *ioc,
254 const char *key,
255 Error **errp)
257 char combined_key[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
258 QIO_CHANNEL_WEBSOCK_GUID_LEN + 1];
259 char *accept = NULL, *response = NULL;
260 size_t responselen;
262 g_strlcpy(combined_key, key, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN + 1);
263 g_strlcat(combined_key, QIO_CHANNEL_WEBSOCK_GUID,
264 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
265 QIO_CHANNEL_WEBSOCK_GUID_LEN + 1);
267 /* hash and encode it */
268 if (qcrypto_hash_base64(QCRYPTO_HASH_ALG_SHA1,
269 combined_key,
270 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
271 QIO_CHANNEL_WEBSOCK_GUID_LEN,
272 &accept,
273 errp) < 0) {
274 return -1;
277 response = g_strdup_printf(QIO_CHANNEL_WEBSOCK_HANDSHAKE_RESPONSE, accept);
278 responselen = strlen(response);
279 buffer_reserve(&ioc->encoutput, responselen);
280 buffer_append(&ioc->encoutput, response, responselen);
282 g_free(accept);
283 g_free(response);
285 return 0;
288 static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
289 char *buffer,
290 Error **errp)
292 QIOChannelWebsockHTTPHeader hdrs[32];
293 size_t nhdrs = G_N_ELEMENTS(hdrs);
294 const char *protocols = NULL, *version = NULL, *key = NULL,
295 *host = NULL, *connection = NULL, *upgrade = NULL;
297 nhdrs = qio_channel_websock_extract_headers(buffer, hdrs, nhdrs, errp);
298 if (!nhdrs) {
299 return -1;
302 protocols = qio_channel_websock_find_header(
303 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL);
304 if (!protocols) {
305 error_setg(errp, "Missing websocket protocol header data");
306 return -1;
309 version = qio_channel_websock_find_header(
310 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_VERSION);
311 if (!version) {
312 error_setg(errp, "Missing websocket version header data");
313 return -1;
316 key = qio_channel_websock_find_header(
317 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_KEY);
318 if (!key) {
319 error_setg(errp, "Missing websocket key header data");
320 return -1;
323 host = qio_channel_websock_find_header(
324 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_HOST);
325 if (!host) {
326 error_setg(errp, "Missing websocket host header data");
327 return -1;
330 connection = qio_channel_websock_find_header(
331 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION);
332 if (!connection) {
333 error_setg(errp, "Missing websocket connection header data");
334 return -1;
337 upgrade = qio_channel_websock_find_header(
338 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE);
339 if (!upgrade) {
340 error_setg(errp, "Missing websocket upgrade header data");
341 return -1;
344 if (!g_strrstr(protocols, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY)) {
345 error_setg(errp, "No '%s' protocol is supported by client '%s'",
346 QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY, protocols);
347 return -1;
350 if (!g_str_equal(version, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION)) {
351 error_setg(errp, "Version '%s' is not supported by client '%s'",
352 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION, version);
353 return -1;
356 if (strlen(key) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN) {
357 error_setg(errp, "Key length '%zu' was not as expected '%d'",
358 strlen(key), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN);
359 return -1;
362 if (!g_strrstr(connection, QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE)) {
363 error_setg(errp, "No connection upgrade requested '%s'", connection);
364 return -1;
367 if (!g_str_equal(upgrade, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET)) {
368 error_setg(errp, "Incorrect upgrade method '%s'", upgrade);
369 return -1;
372 return qio_channel_websock_handshake_send_response(ioc, key, errp);
375 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
376 Error **errp)
378 char *handshake_end;
379 ssize_t ret;
380 /* Typical HTTP headers from novnc are 512 bytes, so limiting
381 * total header size to 4096 is easily enough. */
382 size_t want = 4096 - ioc->encinput.offset;
383 buffer_reserve(&ioc->encinput, want);
384 ret = qio_channel_read(ioc->master,
385 (char *)buffer_end(&ioc->encinput), want, errp);
386 if (ret < 0) {
387 return -1;
389 ioc->encinput.offset += ret;
391 handshake_end = g_strstr_len((char *)ioc->encinput.buffer,
392 ioc->encinput.offset,
393 QIO_CHANNEL_WEBSOCK_HANDSHAKE_END);
394 if (!handshake_end) {
395 if (ioc->encinput.offset >= 4096) {
396 error_setg(errp,
397 "End of headers not found in first 4096 bytes");
398 return -1;
399 } else {
400 return 0;
403 *handshake_end = '\0';
405 if (qio_channel_websock_handshake_process(ioc,
406 (char *)ioc->encinput.buffer,
407 errp) < 0) {
408 return -1;
411 buffer_advance(&ioc->encinput,
412 handshake_end - (char *)ioc->encinput.buffer +
413 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END));
414 return 1;
417 static gboolean qio_channel_websock_handshake_send(QIOChannel *ioc,
418 GIOCondition condition,
419 gpointer user_data)
421 QIOTask *task = user_data;
422 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(
423 qio_task_get_source(task));
424 Error *err = NULL;
425 ssize_t ret;
427 ret = qio_channel_write(wioc->master,
428 (char *)wioc->encoutput.buffer,
429 wioc->encoutput.offset,
430 &err);
432 if (ret < 0) {
433 trace_qio_channel_websock_handshake_fail(ioc);
434 qio_task_set_error(task, err);
435 qio_task_complete(task);
436 return FALSE;
439 buffer_advance(&wioc->encoutput, ret);
440 if (wioc->encoutput.offset == 0) {
441 trace_qio_channel_websock_handshake_complete(ioc);
442 qio_task_complete(task);
443 return FALSE;
445 trace_qio_channel_websock_handshake_pending(ioc, G_IO_OUT);
446 return TRUE;
449 static gboolean qio_channel_websock_handshake_io(QIOChannel *ioc,
450 GIOCondition condition,
451 gpointer user_data)
453 QIOTask *task = user_data;
454 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(
455 qio_task_get_source(task));
456 Error *err = NULL;
457 int ret;
459 ret = qio_channel_websock_handshake_read(wioc, &err);
460 if (ret < 0) {
461 trace_qio_channel_websock_handshake_fail(ioc);
462 qio_task_set_error(task, err);
463 qio_task_complete(task);
464 return FALSE;
466 if (ret == 0) {
467 trace_qio_channel_websock_handshake_pending(ioc, G_IO_IN);
468 /* need more data still */
469 return TRUE;
472 trace_qio_channel_websock_handshake_reply(ioc);
473 qio_channel_add_watch(
474 wioc->master,
475 G_IO_OUT,
476 qio_channel_websock_handshake_send,
477 task,
478 NULL);
479 return FALSE;
483 static void qio_channel_websock_encode(QIOChannelWebsock *ioc)
485 size_t header_size;
486 union {
487 char buf[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT];
488 QIOChannelWebsockHeader ws;
489 } header;
491 if (!ioc->rawoutput.offset) {
492 return;
495 header.ws.b0 = (1 << QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN) |
496 (QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME &
497 QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE);
498 if (ioc->rawoutput.offset <
499 QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) {
500 header.ws.b1 = (uint8_t)ioc->rawoutput.offset;
501 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;
502 } else if (ioc->rawoutput.offset <
503 QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) {
504 header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT;
505 header.ws.u.s16.l16 = cpu_to_be16((uint16_t)ioc->rawoutput.offset);
506 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;
507 } else {
508 header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT;
509 header.ws.u.s64.l64 = cpu_to_be64(ioc->rawoutput.offset);
510 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;
512 header_size -= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK;
514 buffer_reserve(&ioc->encoutput, header_size + ioc->rawoutput.offset);
515 buffer_append(&ioc->encoutput, header.buf, header_size);
516 buffer_append(&ioc->encoutput, ioc->rawoutput.buffer,
517 ioc->rawoutput.offset);
518 buffer_reset(&ioc->rawoutput);
522 static ssize_t qio_channel_websock_decode_header(QIOChannelWebsock *ioc,
523 Error **errp)
525 unsigned char opcode, fin, has_mask;
526 size_t header_size;
527 size_t payload_len;
528 QIOChannelWebsockHeader *header =
529 (QIOChannelWebsockHeader *)ioc->encinput.buffer;
531 if (ioc->payload_remain) {
532 error_setg(errp,
533 "Decoding header but %zu bytes of payload remain",
534 ioc->payload_remain);
535 return -1;
537 if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT) {
538 /* header not complete */
539 return QIO_CHANNEL_ERR_BLOCK;
542 fin = (header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN) >>
543 QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN;
544 opcode = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE;
545 has_mask = (header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK) >>
546 QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK;
547 payload_len = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN;
549 if (opcode == QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) {
550 /* disconnect */
551 return 0;
554 /* Websocket frame sanity check:
555 * * Websocket fragmentation is not supported.
556 * * All websockets frames sent by a client have to be masked.
557 * * Only binary encoding is supported.
559 if (!fin) {
560 error_setg(errp, "websocket fragmentation is not supported");
561 return -1;
563 if (!has_mask) {
564 error_setg(errp, "websocket frames must be masked");
565 return -1;
567 if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) {
568 error_setg(errp, "only binary websocket frames are supported");
569 return -1;
572 if (payload_len < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT) {
573 ioc->payload_remain = payload_len;
574 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;
575 ioc->mask = header->u.m;
576 } else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT &&
577 ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT) {
578 ioc->payload_remain = be16_to_cpu(header->u.s16.l16);
579 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;
580 ioc->mask = header->u.s16.m16;
581 } else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT &&
582 ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT) {
583 ioc->payload_remain = be64_to_cpu(header->u.s64.l64);
584 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;
585 ioc->mask = header->u.s64.m64;
586 } else {
587 /* header not complete */
588 return QIO_CHANNEL_ERR_BLOCK;
591 buffer_advance(&ioc->encinput, header_size);
592 return 1;
596 static ssize_t qio_channel_websock_decode_payload(QIOChannelWebsock *ioc,
597 Error **errp)
599 size_t i;
600 size_t payload_len;
601 uint32_t *payload32;
603 if (!ioc->payload_remain) {
604 error_setg(errp,
605 "Decoding payload but no bytes of payload remain");
606 return -1;
609 /* If we aren't at the end of the payload, then drop
610 * off the last bytes, so we're always multiple of 4
611 * for purpose of unmasking, except at end of payload
613 if (ioc->encinput.offset < ioc->payload_remain) {
614 payload_len = ioc->encinput.offset - (ioc->encinput.offset % 4);
615 } else {
616 payload_len = ioc->payload_remain;
618 if (payload_len == 0) {
619 return QIO_CHANNEL_ERR_BLOCK;
622 ioc->payload_remain -= payload_len;
624 /* unmask frame */
625 /* process 1 frame (32 bit op) */
626 payload32 = (uint32_t *)ioc->encinput.buffer;
627 for (i = 0; i < payload_len / 4; i++) {
628 payload32[i] ^= ioc->mask.u;
630 /* process the remaining bytes (if any) */
631 for (i *= 4; i < payload_len; i++) {
632 ioc->encinput.buffer[i] ^= ioc->mask.c[i % 4];
635 buffer_reserve(&ioc->rawinput, payload_len);
636 buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len);
637 buffer_advance(&ioc->encinput, payload_len);
638 return payload_len;
642 QIOChannelWebsock *
643 qio_channel_websock_new_server(QIOChannel *master)
645 QIOChannelWebsock *wioc;
646 QIOChannel *ioc;
648 wioc = QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK));
649 ioc = QIO_CHANNEL(wioc);
651 wioc->master = master;
652 if (qio_channel_has_feature(master, QIO_CHANNEL_FEATURE_SHUTDOWN)) {
653 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
655 object_ref(OBJECT(master));
657 trace_qio_channel_websock_new_server(wioc, master);
658 return wioc;
661 void qio_channel_websock_handshake(QIOChannelWebsock *ioc,
662 QIOTaskFunc func,
663 gpointer opaque,
664 GDestroyNotify destroy)
666 QIOTask *task;
668 task = qio_task_new(OBJECT(ioc),
669 func,
670 opaque,
671 destroy);
673 trace_qio_channel_websock_handshake_start(ioc);
674 trace_qio_channel_websock_handshake_pending(ioc, G_IO_IN);
675 qio_channel_add_watch(ioc->master,
676 G_IO_IN,
677 qio_channel_websock_handshake_io,
678 task,
679 NULL);
683 static void qio_channel_websock_finalize(Object *obj)
685 QIOChannelWebsock *ioc = QIO_CHANNEL_WEBSOCK(obj);
687 buffer_free(&ioc->encinput);
688 buffer_free(&ioc->encoutput);
689 buffer_free(&ioc->rawinput);
690 buffer_free(&ioc->rawoutput);
691 object_unref(OBJECT(ioc->master));
692 if (ioc->io_tag) {
693 g_source_remove(ioc->io_tag);
695 if (ioc->io_err) {
696 error_free(ioc->io_err);
701 static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
702 Error **errp)
704 ssize_t ret;
706 if (ioc->encinput.offset < 4096) {
707 size_t want = 4096 - ioc->encinput.offset;
709 buffer_reserve(&ioc->encinput, want);
710 ret = qio_channel_read(ioc->master,
711 (char *)ioc->encinput.buffer +
712 ioc->encinput.offset,
713 want,
714 errp);
715 if (ret < 0) {
716 return ret;
718 if (ret == 0 &&
719 ioc->encinput.offset == 0) {
720 return 0;
722 ioc->encinput.offset += ret;
725 while (ioc->encinput.offset != 0) {
726 if (ioc->payload_remain == 0) {
727 ret = qio_channel_websock_decode_header(ioc, errp);
728 if (ret < 0) {
729 return ret;
731 if (ret == 0) {
732 ioc->io_eof = TRUE;
733 break;
737 ret = qio_channel_websock_decode_payload(ioc, errp);
738 if (ret < 0) {
739 return ret;
742 return 1;
746 static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *ioc,
747 Error **errp)
749 ssize_t ret;
750 ssize_t done = 0;
751 qio_channel_websock_encode(ioc);
753 while (ioc->encoutput.offset > 0) {
754 ret = qio_channel_write(ioc->master,
755 (char *)ioc->encoutput.buffer,
756 ioc->encoutput.offset,
757 errp);
758 if (ret < 0) {
759 if (ret == QIO_CHANNEL_ERR_BLOCK &&
760 done > 0) {
761 return done;
762 } else {
763 return ret;
766 buffer_advance(&ioc->encoutput, ret);
767 done += ret;
769 return done;
773 static void qio_channel_websock_flush_free(gpointer user_data)
775 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(user_data);
776 object_unref(OBJECT(wioc));
779 static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc);
781 static gboolean qio_channel_websock_flush(QIOChannel *ioc,
782 GIOCondition condition,
783 gpointer user_data)
785 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(user_data);
786 ssize_t ret;
788 if (condition & G_IO_OUT) {
789 ret = qio_channel_websock_write_wire(wioc, &wioc->io_err);
790 if (ret < 0) {
791 goto cleanup;
795 if (condition & G_IO_IN) {
796 ret = qio_channel_websock_read_wire(wioc, &wioc->io_err);
797 if (ret < 0) {
798 goto cleanup;
802 cleanup:
803 qio_channel_websock_set_watch(wioc);
804 return FALSE;
808 static void qio_channel_websock_unset_watch(QIOChannelWebsock *ioc)
810 if (ioc->io_tag) {
811 g_source_remove(ioc->io_tag);
812 ioc->io_tag = 0;
816 static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc)
818 GIOCondition cond = 0;
820 qio_channel_websock_unset_watch(ioc);
822 if (ioc->io_err) {
823 return;
826 if (ioc->encoutput.offset) {
827 cond |= G_IO_OUT;
829 if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER &&
830 !ioc->io_eof) {
831 cond |= G_IO_IN;
834 if (cond) {
835 object_ref(OBJECT(ioc));
836 ioc->io_tag =
837 qio_channel_add_watch(ioc->master,
838 cond,
839 qio_channel_websock_flush,
840 ioc,
841 qio_channel_websock_flush_free);
846 static ssize_t qio_channel_websock_readv(QIOChannel *ioc,
847 const struct iovec *iov,
848 size_t niov,
849 int **fds,
850 size_t *nfds,
851 Error **errp)
853 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
854 size_t i;
855 ssize_t got = 0;
856 ssize_t ret;
858 if (wioc->io_err) {
859 error_propagate(errp, error_copy(wioc->io_err));
860 return -1;
863 if (!wioc->rawinput.offset) {
864 ret = qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc), errp);
865 if (ret < 0) {
866 return ret;
870 for (i = 0 ; i < niov ; i++) {
871 size_t want = iov[i].iov_len;
872 if (want > (wioc->rawinput.offset - got)) {
873 want = (wioc->rawinput.offset - got);
876 memcpy(iov[i].iov_base,
877 wioc->rawinput.buffer + got,
878 want);
879 got += want;
881 if (want < iov[i].iov_len) {
882 break;
886 buffer_advance(&wioc->rawinput, got);
887 qio_channel_websock_set_watch(wioc);
888 return got;
892 static ssize_t qio_channel_websock_writev(QIOChannel *ioc,
893 const struct iovec *iov,
894 size_t niov,
895 int *fds,
896 size_t nfds,
897 Error **errp)
899 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
900 size_t i;
901 ssize_t done = 0;
902 ssize_t ret;
904 if (wioc->io_err) {
905 error_propagate(errp, error_copy(wioc->io_err));
906 return -1;
909 if (wioc->io_eof) {
910 error_setg(errp, "%s", "Broken pipe");
911 return -1;
914 for (i = 0; i < niov; i++) {
915 size_t want = iov[i].iov_len;
916 if ((want + wioc->rawoutput.offset) > QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
917 want = (QIO_CHANNEL_WEBSOCK_MAX_BUFFER - wioc->rawoutput.offset);
919 if (want == 0) {
920 goto done;
923 buffer_reserve(&wioc->rawoutput, want);
924 buffer_append(&wioc->rawoutput, iov[i].iov_base, want);
925 done += want;
926 if (want < iov[i].iov_len) {
927 break;
931 done:
932 ret = qio_channel_websock_write_wire(wioc, errp);
933 if (ret < 0 &&
934 ret != QIO_CHANNEL_ERR_BLOCK) {
935 qio_channel_websock_unset_watch(wioc);
936 return -1;
939 qio_channel_websock_set_watch(wioc);
941 if (done == 0) {
942 return QIO_CHANNEL_ERR_BLOCK;
945 return done;
948 static int qio_channel_websock_set_blocking(QIOChannel *ioc,
949 bool enabled,
950 Error **errp)
952 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
954 qio_channel_set_blocking(wioc->master, enabled, errp);
955 return 0;
958 static void qio_channel_websock_set_delay(QIOChannel *ioc,
959 bool enabled)
961 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
963 qio_channel_set_delay(tioc->master, enabled);
966 static void qio_channel_websock_set_cork(QIOChannel *ioc,
967 bool enabled)
969 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
971 qio_channel_set_cork(tioc->master, enabled);
974 static int qio_channel_websock_shutdown(QIOChannel *ioc,
975 QIOChannelShutdown how,
976 Error **errp)
978 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
980 return qio_channel_shutdown(tioc->master, how, errp);
983 static int qio_channel_websock_close(QIOChannel *ioc,
984 Error **errp)
986 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
988 return qio_channel_close(wioc->master, errp);
991 typedef struct QIOChannelWebsockSource QIOChannelWebsockSource;
992 struct QIOChannelWebsockSource {
993 GSource parent;
994 QIOChannelWebsock *wioc;
995 GIOCondition condition;
998 static gboolean
999 qio_channel_websock_source_prepare(GSource *source,
1000 gint *timeout)
1002 QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
1003 GIOCondition cond = 0;
1004 *timeout = -1;
1006 if (wsource->wioc->rawinput.offset) {
1007 cond |= G_IO_IN;
1009 if (wsource->wioc->rawoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
1010 cond |= G_IO_OUT;
1013 return cond & wsource->condition;
1016 static gboolean
1017 qio_channel_websock_source_check(GSource *source)
1019 QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
1020 GIOCondition cond = 0;
1022 if (wsource->wioc->rawinput.offset) {
1023 cond |= G_IO_IN;
1025 if (wsource->wioc->rawoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
1026 cond |= G_IO_OUT;
1029 return cond & wsource->condition;
1032 static gboolean
1033 qio_channel_websock_source_dispatch(GSource *source,
1034 GSourceFunc callback,
1035 gpointer user_data)
1037 QIOChannelFunc func = (QIOChannelFunc)callback;
1038 QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
1039 GIOCondition cond = 0;
1041 if (wsource->wioc->rawinput.offset) {
1042 cond |= G_IO_IN;
1044 if (wsource->wioc->rawoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
1045 cond |= G_IO_OUT;
1048 return (*func)(QIO_CHANNEL(wsource->wioc),
1049 (cond & wsource->condition),
1050 user_data);
1053 static void
1054 qio_channel_websock_source_finalize(GSource *source)
1056 QIOChannelWebsockSource *ssource = (QIOChannelWebsockSource *)source;
1058 object_unref(OBJECT(ssource->wioc));
1061 GSourceFuncs qio_channel_websock_source_funcs = {
1062 qio_channel_websock_source_prepare,
1063 qio_channel_websock_source_check,
1064 qio_channel_websock_source_dispatch,
1065 qio_channel_websock_source_finalize
1068 static GSource *qio_channel_websock_create_watch(QIOChannel *ioc,
1069 GIOCondition condition)
1071 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1072 QIOChannelWebsockSource *ssource;
1073 GSource *source;
1075 source = g_source_new(&qio_channel_websock_source_funcs,
1076 sizeof(QIOChannelWebsockSource));
1077 ssource = (QIOChannelWebsockSource *)source;
1079 ssource->wioc = wioc;
1080 object_ref(OBJECT(wioc));
1082 ssource->condition = condition;
1084 qio_channel_websock_set_watch(wioc);
1085 return source;
1088 static void qio_channel_websock_class_init(ObjectClass *klass,
1089 void *class_data G_GNUC_UNUSED)
1091 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
1093 ioc_klass->io_writev = qio_channel_websock_writev;
1094 ioc_klass->io_readv = qio_channel_websock_readv;
1095 ioc_klass->io_set_blocking = qio_channel_websock_set_blocking;
1096 ioc_klass->io_set_cork = qio_channel_websock_set_cork;
1097 ioc_klass->io_set_delay = qio_channel_websock_set_delay;
1098 ioc_klass->io_close = qio_channel_websock_close;
1099 ioc_klass->io_shutdown = qio_channel_websock_shutdown;
1100 ioc_klass->io_create_watch = qio_channel_websock_create_watch;
1103 static const TypeInfo qio_channel_websock_info = {
1104 .parent = TYPE_QIO_CHANNEL,
1105 .name = TYPE_QIO_CHANNEL_WEBSOCK,
1106 .instance_size = sizeof(QIOChannelWebsock),
1107 .instance_finalize = qio_channel_websock_finalize,
1108 .class_init = qio_channel_websock_class_init,
1111 static void qio_channel_websock_register_types(void)
1113 type_register_static(&qio_channel_websock_info);
1116 type_init(qio_channel_websock_register_types);