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"
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" \
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
{
100 QIOChannelWebsockMask m16
;
104 QIOChannelWebsockMask m64
;
106 QIOChannelWebsockMask m
;
110 typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader
;
112 struct QIOChannelWebsockHTTPHeader
{
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
127 qio_channel_websock_extract_headers(char *buffer
,
128 QIOChannelWebsockHTTPHeader
*hdrs
,
132 char *nl
, *sep
, *tmp
;
136 * First parse the HTTP protocol greeting of format:
138 * $METHOD $PATH $VERSION
145 nl
= strstr(buffer
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
147 error_setg(errp
, "Missing HTTP header delimiter");
152 tmp
= strchr(buffer
, ' ');
154 error_setg(errp
, "Missing HTTP path delimiter");
159 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_METHOD
)) {
160 error_setg(errp
, "Unsupported HTTP method %s", buffer
);
165 tmp
= strchr(buffer
, ' ');
167 error_setg(errp
, "Missing HTTP version delimiter");
172 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_PATH
)) {
173 error_setg(errp
, "Unexpected HTTP path %s", buffer
);
179 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_VERSION
)) {
180 error_setg(errp
, "Unsupported HTTP version %s", buffer
);
184 buffer
= nl
+ strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
187 * Now parse all the header fields of format
193 * Cache-control: no-cache
196 QIOChannelWebsockHTTPHeader
*hdr
;
198 nl
= strstr(buffer
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
203 sep
= strchr(buffer
, ':');
205 error_setg(errp
, "Malformed HTTP header");
210 while (*sep
== ' ') {
214 if (nhdrs
>= nhdrsalloc
) {
215 error_setg(errp
, "Too many HTTP headers");
219 hdr
= &hdrs
[nhdrs
++];
223 /* Canonicalize header name for easier identification later */
224 for (tmp
= hdr
->name
; *tmp
; tmp
++) {
225 *tmp
= g_ascii_tolower(*tmp
);
229 buffer
= nl
+ strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
231 } while (nl
!= NULL
);
237 qio_channel_websock_find_header(QIOChannelWebsockHTTPHeader
*hdrs
,
243 for (i
= 0; i
< nhdrs
; i
++) {
244 if (g_str_equal(hdrs
[i
].name
, name
)) {
245 return hdrs
[i
].value
;
253 static int qio_channel_websock_handshake_send_response(QIOChannelWebsock
*ioc
,
257 char combined_key
[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
258 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1];
259 char *accept
= NULL
, *response
= NULL
;
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
,
270 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
271 QIO_CHANNEL_WEBSOCK_GUID_LEN
,
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
);
288 static int qio_channel_websock_handshake_process(QIOChannelWebsock
*ioc
,
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
);
302 protocols
= qio_channel_websock_find_header(
303 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL
);
305 error_setg(errp
, "Missing websocket protocol header data");
309 version
= qio_channel_websock_find_header(
310 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_VERSION
);
312 error_setg(errp
, "Missing websocket version header data");
316 key
= qio_channel_websock_find_header(
317 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_KEY
);
319 error_setg(errp
, "Missing websocket key header data");
323 host
= qio_channel_websock_find_header(
324 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_HOST
);
326 error_setg(errp
, "Missing websocket host header data");
330 connection
= qio_channel_websock_find_header(
331 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION
);
333 error_setg(errp
, "Missing websocket connection header data");
337 upgrade
= qio_channel_websock_find_header(
338 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE
);
340 error_setg(errp
, "Missing websocket upgrade header data");
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
);
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
);
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
);
362 if (!g_strrstr(connection
, QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE
)) {
363 error_setg(errp
, "No connection upgrade requested '%s'", connection
);
367 if (!g_str_equal(upgrade
, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET
)) {
368 error_setg(errp
, "Incorrect upgrade method '%s'", upgrade
);
372 return qio_channel_websock_handshake_send_response(ioc
, key
, errp
);
375 static int qio_channel_websock_handshake_read(QIOChannelWebsock
*ioc
,
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
);
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) {
397 "End of headers not found in first 4096 bytes");
403 *handshake_end
= '\0';
405 if (qio_channel_websock_handshake_process(ioc
,
406 (char *)ioc
->encinput
.buffer
,
411 buffer_advance(&ioc
->encinput
,
412 handshake_end
- (char *)ioc
->encinput
.buffer
+
413 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
));
417 static gboolean
qio_channel_websock_handshake_send(QIOChannel
*ioc
,
418 GIOCondition condition
,
421 QIOTask
*task
= user_data
;
422 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
423 qio_task_get_source(task
));
427 ret
= qio_channel_write(wioc
->master
,
428 (char *)wioc
->encoutput
.buffer
,
429 wioc
->encoutput
.offset
,
433 trace_qio_channel_websock_handshake_fail(ioc
);
434 qio_task_set_error(task
, err
);
435 qio_task_complete(task
);
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
);
445 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_OUT
);
449 static gboolean
qio_channel_websock_handshake_io(QIOChannel
*ioc
,
450 GIOCondition condition
,
453 QIOTask
*task
= user_data
;
454 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
455 qio_task_get_source(task
));
459 ret
= qio_channel_websock_handshake_read(wioc
, &err
);
461 trace_qio_channel_websock_handshake_fail(ioc
);
462 qio_task_set_error(task
, err
);
463 qio_task_complete(task
);
467 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
468 /* need more data still */
472 trace_qio_channel_websock_handshake_reply(ioc
);
473 qio_channel_add_watch(
476 qio_channel_websock_handshake_send
,
483 static void qio_channel_websock_encode(QIOChannelWebsock
*ioc
)
487 char buf
[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
];
488 QIOChannelWebsockHeader ws
;
491 if (!ioc
->rawoutput
.offset
) {
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
;
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
,
525 unsigned char opcode
, fin
, has_mask
;
528 QIOChannelWebsockHeader
*header
=
529 (QIOChannelWebsockHeader
*)ioc
->encinput
.buffer
;
531 if (ioc
->payload_remain
) {
533 "Decoding header but %zu bytes of payload remain",
534 ioc
->payload_remain
);
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
) {
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.
560 error_setg(errp
, "websocket fragmentation is not supported");
564 error_setg(errp
, "websocket frames must be masked");
567 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
568 error_setg(errp
, "only binary websocket frames are supported");
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
;
587 /* header not complete */
588 return QIO_CHANNEL_ERR_BLOCK
;
591 buffer_advance(&ioc
->encinput
, header_size
);
596 static ssize_t
qio_channel_websock_decode_payload(QIOChannelWebsock
*ioc
,
603 if (!ioc
->payload_remain
) {
605 "Decoding payload but no bytes of payload remain");
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);
616 payload_len
= ioc
->payload_remain
;
618 if (payload_len
== 0) {
619 return QIO_CHANNEL_ERR_BLOCK
;
622 ioc
->payload_remain
-= payload_len
;
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
);
643 qio_channel_websock_new_server(QIOChannel
*master
)
645 QIOChannelWebsock
*wioc
;
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
);
661 void qio_channel_websock_handshake(QIOChannelWebsock
*ioc
,
664 GDestroyNotify destroy
)
668 task
= qio_task_new(OBJECT(ioc
),
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
,
677 qio_channel_websock_handshake_io
,
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
));
693 g_source_remove(ioc
->io_tag
);
696 error_free(ioc
->io_err
);
701 static ssize_t
qio_channel_websock_read_wire(QIOChannelWebsock
*ioc
,
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
,
719 ioc
->encinput
.offset
== 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
);
737 ret
= qio_channel_websock_decode_payload(ioc
, errp
);
746 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*ioc
,
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
,
759 if (ret
== QIO_CHANNEL_ERR_BLOCK
&&
766 buffer_advance(&ioc
->encoutput
, ret
);
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
,
785 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
788 if (condition
& G_IO_OUT
) {
789 ret
= qio_channel_websock_write_wire(wioc
, &wioc
->io_err
);
795 if (condition
& G_IO_IN
) {
796 ret
= qio_channel_websock_read_wire(wioc
, &wioc
->io_err
);
803 qio_channel_websock_set_watch(wioc
);
808 static void qio_channel_websock_unset_watch(QIOChannelWebsock
*ioc
)
811 g_source_remove(ioc
->io_tag
);
816 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
)
818 GIOCondition cond
= 0;
820 qio_channel_websock_unset_watch(ioc
);
826 if (ioc
->encoutput
.offset
) {
829 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
&&
835 object_ref(OBJECT(ioc
));
837 qio_channel_add_watch(ioc
->master
,
839 qio_channel_websock_flush
,
841 qio_channel_websock_flush_free
);
846 static ssize_t
qio_channel_websock_readv(QIOChannel
*ioc
,
847 const struct iovec
*iov
,
853 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
859 *errp
= error_copy(wioc
->io_err
);
863 if (!wioc
->rawinput
.offset
) {
864 ret
= qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc
), errp
);
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
,
881 if (want
< iov
[i
].iov_len
) {
886 buffer_advance(&wioc
->rawinput
, got
);
887 qio_channel_websock_set_watch(wioc
);
892 static ssize_t
qio_channel_websock_writev(QIOChannel
*ioc
,
893 const struct iovec
*iov
,
899 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
905 *errp
= error_copy(wioc
->io_err
);
910 error_setg(errp
, "%s", "Broken pipe");
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
);
923 buffer_reserve(&wioc
->rawoutput
, want
);
924 buffer_append(&wioc
->rawoutput
, iov
[i
].iov_base
, want
);
926 if (want
< iov
[i
].iov_len
) {
932 ret
= qio_channel_websock_write_wire(wioc
, errp
);
934 ret
!= QIO_CHANNEL_ERR_BLOCK
) {
935 qio_channel_websock_unset_watch(wioc
);
939 qio_channel_websock_set_watch(wioc
);
942 return QIO_CHANNEL_ERR_BLOCK
;
948 static int qio_channel_websock_set_blocking(QIOChannel
*ioc
,
952 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
954 qio_channel_set_blocking(wioc
->master
, enabled
, errp
);
958 static void qio_channel_websock_set_delay(QIOChannel
*ioc
,
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
,
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
,
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
,
986 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
988 return qio_channel_close(wioc
->master
, errp
);
991 typedef struct QIOChannelWebsockSource QIOChannelWebsockSource
;
992 struct QIOChannelWebsockSource
{
994 QIOChannelWebsock
*wioc
;
995 GIOCondition condition
;
999 qio_channel_websock_source_prepare(GSource
*source
,
1002 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1003 GIOCondition cond
= 0;
1006 if (wsource
->wioc
->rawinput
.offset
) {
1009 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
1013 return cond
& wsource
->condition
;
1017 qio_channel_websock_source_check(GSource
*source
)
1019 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1020 GIOCondition cond
= 0;
1022 if (wsource
->wioc
->rawinput
.offset
) {
1025 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
1029 return cond
& wsource
->condition
;
1033 qio_channel_websock_source_dispatch(GSource
*source
,
1034 GSourceFunc callback
,
1037 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
1038 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1039 GIOCondition cond
= 0;
1041 if (wsource
->wioc
->rawinput
.offset
) {
1044 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
1048 return (*func
)(QIO_CHANNEL(wsource
->wioc
),
1049 (cond
& wsource
->condition
),
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
;
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
);
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
);