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 "io/channel-websock.h"
22 #include "crypto/hash.h"
26 /* Max amount to allow in rawinput/rawoutput buffers */
27 #define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
29 #define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
30 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
31 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
33 #define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "Sec-WebSocket-Protocol"
34 #define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "Sec-WebSocket-Version"
35 #define QIO_CHANNEL_WEBSOCK_HEADER_KEY "Sec-WebSocket-Key"
37 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
39 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RESPONSE \
40 "HTTP/1.1 101 Switching Protocols\r\n" \
41 "Upgrade: websocket\r\n" \
42 "Connection: Upgrade\r\n" \
43 "Sec-WebSocket-Accept: %s\r\n" \
44 "Sec-WebSocket-Protocol: binary\r\n" \
46 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
47 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
48 #define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
50 /* The websockets packet header is variable length
51 * depending on the size of the payload... */
53 /* ...length when using 7-bit payload length */
54 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
55 /* ...length when using 16-bit payload length */
56 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
57 /* ...length when using 64-bit payload length */
58 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
60 /* Length of the optional data mask field in header */
61 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
63 /* Maximum length that can fit in 7-bit payload size */
64 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
65 /* Maximum length that can fit in 16-bit payload size */
66 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
68 /* Magic 7-bit length to indicate use of 16-bit payload length */
69 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
70 /* Magic 7-bit length to indicate use of 64-bit payload length */
71 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
73 /* Bitmasks & shifts for accessing header fields */
74 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
75 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
76 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
77 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
78 #define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN 7
79 #define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK 7
81 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader
;
83 struct QEMU_PACKED QIOChannelWebsockHeader
{
89 QIOChannelWebsockMask m16
;
93 QIOChannelWebsockMask m64
;
95 QIOChannelWebsockMask m
;
100 QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION
= 0x0,
101 QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME
= 0x1,
102 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
= 0x2,
103 QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
= 0x8,
104 QIO_CHANNEL_WEBSOCK_OPCODE_PING
= 0x9,
105 QIO_CHANNEL_WEBSOCK_OPCODE_PONG
= 0xA
108 static char *qio_channel_websock_handshake_entry(const char *handshake
,
109 size_t handshake_len
,
112 char *begin
, *end
, *ret
= NULL
;
113 char *line
= g_strdup_printf("%s%s: ",
114 QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
,
116 begin
= g_strstr_len(handshake
, handshake_len
, line
);
118 begin
+= strlen(line
);
119 end
= g_strstr_len(begin
, handshake_len
- (begin
- handshake
),
120 QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
122 ret
= g_strndup(begin
, end
- begin
);
130 static int qio_channel_websock_handshake_send_response(QIOChannelWebsock
*ioc
,
134 char combined_key
[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
135 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1];
136 char *accept
= NULL
, *response
= NULL
;
139 g_strlcpy(combined_key
, key
, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+ 1);
140 g_strlcat(combined_key
, QIO_CHANNEL_WEBSOCK_GUID
,
141 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
142 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1);
144 /* hash and encode it */
145 if (qcrypto_hash_base64(QCRYPTO_HASH_ALG_SHA1
,
147 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
148 QIO_CHANNEL_WEBSOCK_GUID_LEN
,
154 response
= g_strdup_printf(QIO_CHANNEL_WEBSOCK_HANDSHAKE_RESPONSE
, accept
);
155 responselen
= strlen(response
);
156 buffer_reserve(&ioc
->encoutput
, responselen
);
157 buffer_append(&ioc
->encoutput
, response
, responselen
);
165 static int qio_channel_websock_handshake_process(QIOChannelWebsock
*ioc
,
171 char *protocols
= qio_channel_websock_handshake_entry(
172 line
, size
, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL
);
173 char *version
= qio_channel_websock_handshake_entry(
174 line
, size
, QIO_CHANNEL_WEBSOCK_HEADER_VERSION
);
175 char *key
= qio_channel_websock_handshake_entry(
176 line
, size
, QIO_CHANNEL_WEBSOCK_HEADER_KEY
);
179 error_setg(errp
, "Missing websocket protocol header data");
184 error_setg(errp
, "Missing websocket version header data");
189 error_setg(errp
, "Missing websocket key header data");
193 if (!g_strrstr(protocols
, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
)) {
194 error_setg(errp
, "No '%s' protocol is supported by client '%s'",
195 QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
, protocols
);
199 if (!g_str_equal(version
, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
)) {
200 error_setg(errp
, "Version '%s' is not supported by client '%s'",
201 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
, version
);
205 if (strlen(key
) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
) {
206 error_setg(errp
, "Key length '%zu' was not as expected '%d'",
207 strlen(key
), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
);
211 ret
= qio_channel_websock_handshake_send_response(ioc
, key
, errp
);
220 static int qio_channel_websock_handshake_read(QIOChannelWebsock
*ioc
,
225 /* Typical HTTP headers from novnc are 512 bytes, so limiting
226 * total header size to 4096 is easily enough. */
227 size_t want
= 4096 - ioc
->encinput
.offset
;
228 buffer_reserve(&ioc
->encinput
, want
);
229 ret
= qio_channel_read(ioc
->master
,
230 (char *)buffer_end(&ioc
->encinput
), want
, errp
);
234 ioc
->encinput
.offset
+= ret
;
236 handshake_end
= g_strstr_len((char *)ioc
->encinput
.buffer
,
237 ioc
->encinput
.offset
,
238 QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
);
239 if (!handshake_end
) {
240 if (ioc
->encinput
.offset
>= 4096) {
242 "End of headers not found in first 4096 bytes");
249 if (qio_channel_websock_handshake_process(ioc
,
250 (char *)ioc
->encinput
.buffer
,
251 ioc
->encinput
.offset
,
256 buffer_advance(&ioc
->encinput
,
257 handshake_end
- (char *)ioc
->encinput
.buffer
+
258 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
));
262 static gboolean
qio_channel_websock_handshake_send(QIOChannel
*ioc
,
263 GIOCondition condition
,
266 QIOTask
*task
= user_data
;
267 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
268 qio_task_get_source(task
));
272 ret
= qio_channel_write(wioc
->master
,
273 (char *)wioc
->encoutput
.buffer
,
274 wioc
->encoutput
.offset
,
278 trace_qio_channel_websock_handshake_fail(ioc
);
279 qio_task_abort(task
, err
);
284 buffer_advance(&wioc
->encoutput
, ret
);
285 if (wioc
->encoutput
.offset
== 0) {
286 trace_qio_channel_websock_handshake_complete(ioc
);
287 qio_task_complete(task
);
290 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_OUT
);
294 static gboolean
qio_channel_websock_handshake_io(QIOChannel
*ioc
,
295 GIOCondition condition
,
298 QIOTask
*task
= user_data
;
299 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
300 qio_task_get_source(task
));
304 ret
= qio_channel_websock_handshake_read(wioc
, &err
);
306 trace_qio_channel_websock_handshake_fail(ioc
);
307 qio_task_abort(task
, err
);
312 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
313 /* need more data still */
317 object_ref(OBJECT(task
));
318 trace_qio_channel_websock_handshake_reply(ioc
);
319 qio_channel_add_watch(
322 qio_channel_websock_handshake_send
,
324 (GDestroyNotify
)object_unref
);
329 static void qio_channel_websock_encode(QIOChannelWebsock
*ioc
)
333 char buf
[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
];
334 QIOChannelWebsockHeader ws
;
337 if (!ioc
->rawoutput
.offset
) {
341 header
.ws
.b0
= (1 << QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN
) |
342 (QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
&
343 QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
);
344 if (ioc
->rawoutput
.offset
<
345 QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT
) {
346 header
.ws
.b1
= (uint8_t)ioc
->rawoutput
.offset
;
347 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
348 } else if (ioc
->rawoutput
.offset
<
349 QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT
) {
350 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
;
351 header
.ws
.u
.s16
.l16
= cpu_to_be16((uint16_t)ioc
->rawoutput
.offset
);
352 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
354 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
;
355 header
.ws
.u
.s64
.l64
= cpu_to_be64(ioc
->rawoutput
.offset
);
356 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
358 header_size
-= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK
;
360 buffer_reserve(&ioc
->encoutput
, header_size
+ ioc
->rawoutput
.offset
);
361 buffer_append(&ioc
->encoutput
, header
.buf
, header_size
);
362 buffer_append(&ioc
->encoutput
, ioc
->rawoutput
.buffer
,
363 ioc
->rawoutput
.offset
);
364 buffer_reset(&ioc
->rawoutput
);
368 static ssize_t
qio_channel_websock_decode_header(QIOChannelWebsock
*ioc
,
371 unsigned char opcode
, fin
, has_mask
;
374 QIOChannelWebsockHeader
*header
=
375 (QIOChannelWebsockHeader
*)ioc
->encinput
.buffer
;
377 if (ioc
->payload_remain
) {
379 "Decoding header but %zu bytes of payload remain",
380 ioc
->payload_remain
);
383 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
) {
384 /* header not complete */
385 return QIO_CHANNEL_ERR_BLOCK
;
388 fin
= (header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN
) >>
389 QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN
;
390 opcode
= header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
;
391 has_mask
= (header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK
) >>
392 QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK
;
393 payload_len
= header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN
;
395 if (opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
) {
400 /* Websocket frame sanity check:
401 * * Websocket fragmentation is not supported.
402 * * All websockets frames sent by a client have to be masked.
403 * * Only binary encoding is supported.
406 error_setg(errp
, "websocket fragmentation is not supported");
410 error_setg(errp
, "websocket frames must be masked");
413 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
414 error_setg(errp
, "only binary websocket frames are supported");
418 if (payload_len
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
) {
419 ioc
->payload_remain
= payload_len
;
420 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
421 ioc
->mask
= header
->u
.m
;
422 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
&&
423 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
) {
424 ioc
->payload_remain
= be16_to_cpu(header
->u
.s16
.l16
);
425 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
426 ioc
->mask
= header
->u
.s16
.m16
;
427 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
&&
428 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
) {
429 ioc
->payload_remain
= be64_to_cpu(header
->u
.s64
.l64
);
430 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
431 ioc
->mask
= header
->u
.s64
.m64
;
433 /* header not complete */
434 return QIO_CHANNEL_ERR_BLOCK
;
437 buffer_advance(&ioc
->encinput
, header_size
);
442 static ssize_t
qio_channel_websock_decode_payload(QIOChannelWebsock
*ioc
,
449 if (!ioc
->payload_remain
) {
451 "Decoding payload but no bytes of payload remain");
455 /* If we aren't at the end of the payload, then drop
456 * off the last bytes, so we're always multiple of 4
457 * for purpose of unmasking, except at end of payload
459 if (ioc
->encinput
.offset
< ioc
->payload_remain
) {
460 payload_len
= ioc
->encinput
.offset
- (ioc
->encinput
.offset
% 4);
462 payload_len
= ioc
->payload_remain
;
464 if (payload_len
== 0) {
465 return QIO_CHANNEL_ERR_BLOCK
;
468 ioc
->payload_remain
-= payload_len
;
471 /* process 1 frame (32 bit op) */
472 payload32
= (uint32_t *)ioc
->encinput
.buffer
;
473 for (i
= 0; i
< payload_len
/ 4; i
++) {
474 payload32
[i
] ^= ioc
->mask
.u
;
476 /* process the remaining bytes (if any) */
477 for (i
*= 4; i
< payload_len
; i
++) {
478 ioc
->encinput
.buffer
[i
] ^= ioc
->mask
.c
[i
% 4];
481 buffer_reserve(&ioc
->rawinput
, payload_len
);
482 buffer_append(&ioc
->rawinput
, ioc
->encinput
.buffer
, payload_len
);
483 buffer_advance(&ioc
->encinput
, payload_len
);
489 qio_channel_websock_new_server(QIOChannel
*master
)
491 QIOChannelWebsock
*wioc
;
494 wioc
= QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK
));
495 ioc
= QIO_CHANNEL(wioc
);
497 wioc
->master
= master
;
498 if (master
->features
& (1 << QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
499 ioc
->features
|= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN
);
501 object_ref(OBJECT(master
));
503 trace_qio_channel_websock_new_server(wioc
, master
);
507 void qio_channel_websock_handshake(QIOChannelWebsock
*ioc
,
510 GDestroyNotify destroy
)
514 task
= qio_task_new(OBJECT(ioc
),
519 trace_qio_channel_websock_handshake_start(ioc
);
520 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
521 qio_channel_add_watch(ioc
->master
,
523 qio_channel_websock_handshake_io
,
529 static void qio_channel_websock_finalize(Object
*obj
)
531 QIOChannelWebsock
*ioc
= QIO_CHANNEL_WEBSOCK(obj
);
533 buffer_free(&ioc
->encinput
);
534 buffer_free(&ioc
->encoutput
);
535 buffer_free(&ioc
->rawinput
);
536 buffer_free(&ioc
->rawoutput
);
537 object_unref(OBJECT(ioc
->master
));
539 g_source_remove(ioc
->io_tag
);
542 error_free(ioc
->io_err
);
547 static ssize_t
qio_channel_websock_read_wire(QIOChannelWebsock
*ioc
,
552 if (ioc
->encinput
.offset
< 4096) {
553 size_t want
= 4096 - ioc
->encinput
.offset
;
555 buffer_reserve(&ioc
->encinput
, want
);
556 ret
= qio_channel_read(ioc
->master
,
557 (char *)ioc
->encinput
.buffer
+
558 ioc
->encinput
.offset
,
565 ioc
->encinput
.offset
== 0) {
568 ioc
->encinput
.offset
+= ret
;
571 if (ioc
->payload_remain
== 0) {
572 ret
= qio_channel_websock_decode_header(ioc
, errp
);
581 ret
= qio_channel_websock_decode_payload(ioc
, errp
);
589 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*ioc
,
594 qio_channel_websock_encode(ioc
);
596 while (ioc
->encoutput
.offset
> 0) {
597 ret
= qio_channel_write(ioc
->master
,
598 (char *)ioc
->encoutput
.buffer
,
599 ioc
->encoutput
.offset
,
602 if (ret
== QIO_CHANNEL_ERR_BLOCK
&&
609 buffer_advance(&ioc
->encoutput
, ret
);
616 static void qio_channel_websock_flush_free(gpointer user_data
)
618 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
619 object_unref(OBJECT(wioc
));
622 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
);
624 static gboolean
qio_channel_websock_flush(QIOChannel
*ioc
,
625 GIOCondition condition
,
628 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
631 if (condition
& G_IO_OUT
) {
632 ret
= qio_channel_websock_write_wire(wioc
, &wioc
->io_err
);
638 if (condition
& G_IO_IN
) {
639 ret
= qio_channel_websock_read_wire(wioc
, &wioc
->io_err
);
649 qio_channel_websock_set_watch(wioc
);
654 static void qio_channel_websock_unset_watch(QIOChannelWebsock
*ioc
)
657 g_source_remove(ioc
->io_tag
);
662 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
)
664 GIOCondition cond
= 0;
666 qio_channel_websock_unset_watch(ioc
);
672 if (ioc
->encoutput
.offset
) {
675 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
&&
681 object_ref(OBJECT(ioc
));
683 qio_channel_add_watch(ioc
->master
,
685 qio_channel_websock_flush
,
687 qio_channel_websock_flush_free
);
692 static ssize_t
qio_channel_websock_readv(QIOChannel
*ioc
,
693 const struct iovec
*iov
,
699 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
705 *errp
= error_copy(wioc
->io_err
);
709 if (!wioc
->rawinput
.offset
) {
710 ret
= qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc
), errp
);
716 for (i
= 0 ; i
< niov
; i
++) {
717 size_t want
= iov
[i
].iov_len
;
718 if (want
> (wioc
->rawinput
.offset
- got
)) {
719 want
= (wioc
->rawinput
.offset
- got
);
722 memcpy(iov
[i
].iov_base
,
723 wioc
->rawinput
.buffer
+ got
,
727 if (want
< iov
[i
].iov_len
) {
732 buffer_advance(&wioc
->rawinput
, got
);
733 qio_channel_websock_set_watch(wioc
);
738 static ssize_t
qio_channel_websock_writev(QIOChannel
*ioc
,
739 const struct iovec
*iov
,
745 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
751 *errp
= error_copy(wioc
->io_err
);
756 error_setg(errp
, "%s", "Broken pipe");
760 for (i
= 0; i
< niov
; i
++) {
761 size_t want
= iov
[i
].iov_len
;
762 if ((want
+ wioc
->rawoutput
.offset
) > QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
763 want
= (QIO_CHANNEL_WEBSOCK_MAX_BUFFER
- wioc
->rawoutput
.offset
);
769 buffer_reserve(&wioc
->rawoutput
, want
);
770 buffer_append(&wioc
->rawoutput
, iov
[i
].iov_base
, want
);
772 if (want
< iov
[i
].iov_len
) {
778 ret
= qio_channel_websock_write_wire(wioc
, errp
);
780 ret
!= QIO_CHANNEL_ERR_BLOCK
) {
781 qio_channel_websock_unset_watch(wioc
);
785 qio_channel_websock_set_watch(wioc
);
788 return QIO_CHANNEL_ERR_BLOCK
;
794 static int qio_channel_websock_set_blocking(QIOChannel
*ioc
,
798 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
800 qio_channel_set_blocking(wioc
->master
, enabled
, errp
);
804 static void qio_channel_websock_set_delay(QIOChannel
*ioc
,
807 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
809 qio_channel_set_delay(tioc
->master
, enabled
);
812 static void qio_channel_websock_set_cork(QIOChannel
*ioc
,
815 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
817 qio_channel_set_cork(tioc
->master
, enabled
);
820 static int qio_channel_websock_shutdown(QIOChannel
*ioc
,
821 QIOChannelShutdown how
,
824 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
826 return qio_channel_shutdown(tioc
->master
, how
, errp
);
829 static int qio_channel_websock_close(QIOChannel
*ioc
,
832 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
834 return qio_channel_close(wioc
->master
, errp
);
837 typedef struct QIOChannelWebsockSource QIOChannelWebsockSource
;
838 struct QIOChannelWebsockSource
{
840 QIOChannelWebsock
*wioc
;
841 GIOCondition condition
;
845 qio_channel_websock_source_prepare(GSource
*source
,
848 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
849 GIOCondition cond
= 0;
852 if (wsource
->wioc
->rawinput
.offset
) {
855 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
859 return cond
& wsource
->condition
;
863 qio_channel_websock_source_check(GSource
*source
)
865 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
866 GIOCondition cond
= 0;
868 if (wsource
->wioc
->rawinput
.offset
) {
871 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
875 return cond
& wsource
->condition
;
879 qio_channel_websock_source_dispatch(GSource
*source
,
880 GSourceFunc callback
,
883 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
884 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
885 GIOCondition cond
= 0;
887 if (wsource
->wioc
->rawinput
.offset
) {
890 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
894 return (*func
)(QIO_CHANNEL(wsource
->wioc
),
895 (cond
& wsource
->condition
),
900 qio_channel_websock_source_finalize(GSource
*source
)
902 QIOChannelWebsockSource
*ssource
= (QIOChannelWebsockSource
*)source
;
904 object_unref(OBJECT(ssource
->wioc
));
907 GSourceFuncs qio_channel_websock_source_funcs
= {
908 qio_channel_websock_source_prepare
,
909 qio_channel_websock_source_check
,
910 qio_channel_websock_source_dispatch
,
911 qio_channel_websock_source_finalize
914 static GSource
*qio_channel_websock_create_watch(QIOChannel
*ioc
,
915 GIOCondition condition
)
917 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
918 QIOChannelWebsockSource
*ssource
;
921 source
= g_source_new(&qio_channel_websock_source_funcs
,
922 sizeof(QIOChannelWebsockSource
));
923 ssource
= (QIOChannelWebsockSource
*)source
;
925 ssource
->wioc
= wioc
;
926 object_ref(OBJECT(wioc
));
928 ssource
->condition
= condition
;
930 qio_channel_websock_set_watch(wioc
);
934 static void qio_channel_websock_class_init(ObjectClass
*klass
,
935 void *class_data G_GNUC_UNUSED
)
937 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
939 ioc_klass
->io_writev
= qio_channel_websock_writev
;
940 ioc_klass
->io_readv
= qio_channel_websock_readv
;
941 ioc_klass
->io_set_blocking
= qio_channel_websock_set_blocking
;
942 ioc_klass
->io_set_cork
= qio_channel_websock_set_cork
;
943 ioc_klass
->io_set_delay
= qio_channel_websock_set_delay
;
944 ioc_klass
->io_close
= qio_channel_websock_close
;
945 ioc_klass
->io_shutdown
= qio_channel_websock_shutdown
;
946 ioc_klass
->io_create_watch
= qio_channel_websock_create_watch
;
949 static const TypeInfo qio_channel_websock_info
= {
950 .parent
= TYPE_QIO_CHANNEL
,
951 .name
= TYPE_QIO_CHANNEL_WEBSOCK
,
952 .instance_size
= sizeof(QIOChannelWebsock
),
953 .instance_finalize
= qio_channel_websock_finalize
,
954 .class_init
= qio_channel_websock_class_init
,
957 static void qio_channel_websock_register_types(void)
959 type_register_static(&qio_channel_websock_info
);
962 type_init(qio_channel_websock_register_types
);