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 "io/channel-websock.h"
23 #include "crypto/hash.h"
27 /* Max amount to allow in rawinput/rawoutput buffers */
28 #define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
30 #define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
31 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
32 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
34 #define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "Sec-WebSocket-Protocol"
35 #define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "Sec-WebSocket-Version"
36 #define QIO_CHANNEL_WEBSOCK_HEADER_KEY "Sec-WebSocket-Key"
38 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
40 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RESPONSE \
41 "HTTP/1.1 101 Switching Protocols\r\n" \
42 "Upgrade: websocket\r\n" \
43 "Connection: Upgrade\r\n" \
44 "Sec-WebSocket-Accept: %s\r\n" \
45 "Sec-WebSocket-Protocol: binary\r\n" \
47 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
48 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
49 #define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
51 /* The websockets packet header is variable length
52 * depending on the size of the payload... */
54 /* ...length when using 7-bit payload length */
55 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
56 /* ...length when using 16-bit payload length */
57 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
58 /* ...length when using 64-bit payload length */
59 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
61 /* Length of the optional data mask field in header */
62 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
64 /* Maximum length that can fit in 7-bit payload size */
65 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
66 /* Maximum length that can fit in 16-bit payload size */
67 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
69 /* Magic 7-bit length to indicate use of 16-bit payload length */
70 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
71 /* Magic 7-bit length to indicate use of 64-bit payload length */
72 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
74 /* Bitmasks & shifts for accessing header fields */
75 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
76 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
77 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
78 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
79 #define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN 7
80 #define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK 7
82 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader
;
84 struct QEMU_PACKED QIOChannelWebsockHeader
{
90 QIOChannelWebsockMask m16
;
94 QIOChannelWebsockMask m64
;
96 QIOChannelWebsockMask m
;
101 QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION
= 0x0,
102 QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME
= 0x1,
103 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
= 0x2,
104 QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
= 0x8,
105 QIO_CHANNEL_WEBSOCK_OPCODE_PING
= 0x9,
106 QIO_CHANNEL_WEBSOCK_OPCODE_PONG
= 0xA
109 static char *qio_channel_websock_handshake_entry(const char *handshake
,
110 size_t handshake_len
,
113 char *begin
, *end
, *ret
= NULL
;
114 char *line
= g_strdup_printf("%s%s: ",
115 QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
,
117 begin
= g_strstr_len(handshake
, handshake_len
, line
);
119 begin
+= strlen(line
);
120 end
= g_strstr_len(begin
, handshake_len
- (begin
- handshake
),
121 QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
123 ret
= g_strndup(begin
, end
- begin
);
131 static int qio_channel_websock_handshake_send_response(QIOChannelWebsock
*ioc
,
135 char combined_key
[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
136 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1];
137 char *accept
= NULL
, *response
= NULL
;
140 g_strlcpy(combined_key
, key
, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+ 1);
141 g_strlcat(combined_key
, QIO_CHANNEL_WEBSOCK_GUID
,
142 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
143 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1);
145 /* hash and encode it */
146 if (qcrypto_hash_base64(QCRYPTO_HASH_ALG_SHA1
,
148 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
149 QIO_CHANNEL_WEBSOCK_GUID_LEN
,
155 response
= g_strdup_printf(QIO_CHANNEL_WEBSOCK_HANDSHAKE_RESPONSE
, accept
);
156 responselen
= strlen(response
);
157 buffer_reserve(&ioc
->encoutput
, responselen
);
158 buffer_append(&ioc
->encoutput
, response
, responselen
);
166 static int qio_channel_websock_handshake_process(QIOChannelWebsock
*ioc
,
172 char *protocols
= qio_channel_websock_handshake_entry(
173 line
, size
, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL
);
174 char *version
= qio_channel_websock_handshake_entry(
175 line
, size
, QIO_CHANNEL_WEBSOCK_HEADER_VERSION
);
176 char *key
= qio_channel_websock_handshake_entry(
177 line
, size
, QIO_CHANNEL_WEBSOCK_HEADER_KEY
);
180 error_setg(errp
, "Missing websocket protocol header data");
185 error_setg(errp
, "Missing websocket version header data");
190 error_setg(errp
, "Missing websocket key header data");
194 if (!g_strrstr(protocols
, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
)) {
195 error_setg(errp
, "No '%s' protocol is supported by client '%s'",
196 QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
, protocols
);
200 if (!g_str_equal(version
, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
)) {
201 error_setg(errp
, "Version '%s' is not supported by client '%s'",
202 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
, version
);
206 if (strlen(key
) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
) {
207 error_setg(errp
, "Key length '%zu' was not as expected '%d'",
208 strlen(key
), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
);
212 ret
= qio_channel_websock_handshake_send_response(ioc
, key
, errp
);
221 static int qio_channel_websock_handshake_read(QIOChannelWebsock
*ioc
,
226 /* Typical HTTP headers from novnc are 512 bytes, so limiting
227 * total header size to 4096 is easily enough. */
228 size_t want
= 4096 - ioc
->encinput
.offset
;
229 buffer_reserve(&ioc
->encinput
, want
);
230 ret
= qio_channel_read(ioc
->master
,
231 (char *)buffer_end(&ioc
->encinput
), want
, errp
);
235 ioc
->encinput
.offset
+= ret
;
237 handshake_end
= g_strstr_len((char *)ioc
->encinput
.buffer
,
238 ioc
->encinput
.offset
,
239 QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
);
240 if (!handshake_end
) {
241 if (ioc
->encinput
.offset
>= 4096) {
243 "End of headers not found in first 4096 bytes");
250 if (qio_channel_websock_handshake_process(ioc
,
251 (char *)ioc
->encinput
.buffer
,
252 ioc
->encinput
.offset
,
257 buffer_advance(&ioc
->encinput
,
258 handshake_end
- (char *)ioc
->encinput
.buffer
+
259 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
));
263 static gboolean
qio_channel_websock_handshake_send(QIOChannel
*ioc
,
264 GIOCondition condition
,
267 QIOTask
*task
= user_data
;
268 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
269 qio_task_get_source(task
));
273 ret
= qio_channel_write(wioc
->master
,
274 (char *)wioc
->encoutput
.buffer
,
275 wioc
->encoutput
.offset
,
279 trace_qio_channel_websock_handshake_fail(ioc
);
280 qio_task_abort(task
, err
);
285 buffer_advance(&wioc
->encoutput
, ret
);
286 if (wioc
->encoutput
.offset
== 0) {
287 trace_qio_channel_websock_handshake_complete(ioc
);
288 qio_task_complete(task
);
291 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_OUT
);
295 static gboolean
qio_channel_websock_handshake_io(QIOChannel
*ioc
,
296 GIOCondition condition
,
299 QIOTask
*task
= user_data
;
300 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
301 qio_task_get_source(task
));
305 ret
= qio_channel_websock_handshake_read(wioc
, &err
);
307 trace_qio_channel_websock_handshake_fail(ioc
);
308 qio_task_abort(task
, err
);
313 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
314 /* need more data still */
318 object_ref(OBJECT(task
));
319 trace_qio_channel_websock_handshake_reply(ioc
);
320 qio_channel_add_watch(
323 qio_channel_websock_handshake_send
,
325 (GDestroyNotify
)object_unref
);
330 static void qio_channel_websock_encode(QIOChannelWebsock
*ioc
)
334 char buf
[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
];
335 QIOChannelWebsockHeader ws
;
338 if (!ioc
->rawoutput
.offset
) {
342 header
.ws
.b0
= (1 << QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN
) |
343 (QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
&
344 QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
);
345 if (ioc
->rawoutput
.offset
<
346 QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT
) {
347 header
.ws
.b1
= (uint8_t)ioc
->rawoutput
.offset
;
348 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
349 } else if (ioc
->rawoutput
.offset
<
350 QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT
) {
351 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
;
352 header
.ws
.u
.s16
.l16
= cpu_to_be16((uint16_t)ioc
->rawoutput
.offset
);
353 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
355 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
;
356 header
.ws
.u
.s64
.l64
= cpu_to_be64(ioc
->rawoutput
.offset
);
357 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
359 header_size
-= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK
;
361 buffer_reserve(&ioc
->encoutput
, header_size
+ ioc
->rawoutput
.offset
);
362 buffer_append(&ioc
->encoutput
, header
.buf
, header_size
);
363 buffer_append(&ioc
->encoutput
, ioc
->rawoutput
.buffer
,
364 ioc
->rawoutput
.offset
);
365 buffer_reset(&ioc
->rawoutput
);
369 static ssize_t
qio_channel_websock_decode_header(QIOChannelWebsock
*ioc
,
372 unsigned char opcode
, fin
, has_mask
;
375 QIOChannelWebsockHeader
*header
=
376 (QIOChannelWebsockHeader
*)ioc
->encinput
.buffer
;
378 if (ioc
->payload_remain
) {
380 "Decoding header but %zu bytes of payload remain",
381 ioc
->payload_remain
);
384 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
) {
385 /* header not complete */
386 return QIO_CHANNEL_ERR_BLOCK
;
389 fin
= (header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN
) >>
390 QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN
;
391 opcode
= header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
;
392 has_mask
= (header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK
) >>
393 QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK
;
394 payload_len
= header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN
;
396 if (opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
) {
401 /* Websocket frame sanity check:
402 * * Websocket fragmentation is not supported.
403 * * All websockets frames sent by a client have to be masked.
404 * * Only binary encoding is supported.
407 error_setg(errp
, "websocket fragmentation is not supported");
411 error_setg(errp
, "websocket frames must be masked");
414 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
415 error_setg(errp
, "only binary websocket frames are supported");
419 if (payload_len
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
) {
420 ioc
->payload_remain
= payload_len
;
421 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
422 ioc
->mask
= header
->u
.m
;
423 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
&&
424 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
) {
425 ioc
->payload_remain
= be16_to_cpu(header
->u
.s16
.l16
);
426 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
427 ioc
->mask
= header
->u
.s16
.m16
;
428 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
&&
429 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
) {
430 ioc
->payload_remain
= be64_to_cpu(header
->u
.s64
.l64
);
431 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
432 ioc
->mask
= header
->u
.s64
.m64
;
434 /* header not complete */
435 return QIO_CHANNEL_ERR_BLOCK
;
438 buffer_advance(&ioc
->encinput
, header_size
);
443 static ssize_t
qio_channel_websock_decode_payload(QIOChannelWebsock
*ioc
,
450 if (!ioc
->payload_remain
) {
452 "Decoding payload but no bytes of payload remain");
456 /* If we aren't at the end of the payload, then drop
457 * off the last bytes, so we're always multiple of 4
458 * for purpose of unmasking, except at end of payload
460 if (ioc
->encinput
.offset
< ioc
->payload_remain
) {
461 payload_len
= ioc
->encinput
.offset
- (ioc
->encinput
.offset
% 4);
463 payload_len
= ioc
->payload_remain
;
465 if (payload_len
== 0) {
466 return QIO_CHANNEL_ERR_BLOCK
;
469 ioc
->payload_remain
-= payload_len
;
472 /* process 1 frame (32 bit op) */
473 payload32
= (uint32_t *)ioc
->encinput
.buffer
;
474 for (i
= 0; i
< payload_len
/ 4; i
++) {
475 payload32
[i
] ^= ioc
->mask
.u
;
477 /* process the remaining bytes (if any) */
478 for (i
*= 4; i
< payload_len
; i
++) {
479 ioc
->encinput
.buffer
[i
] ^= ioc
->mask
.c
[i
% 4];
482 buffer_reserve(&ioc
->rawinput
, payload_len
);
483 buffer_append(&ioc
->rawinput
, ioc
->encinput
.buffer
, payload_len
);
484 buffer_advance(&ioc
->encinput
, payload_len
);
490 qio_channel_websock_new_server(QIOChannel
*master
)
492 QIOChannelWebsock
*wioc
;
495 wioc
= QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK
));
496 ioc
= QIO_CHANNEL(wioc
);
498 wioc
->master
= master
;
499 if (master
->features
& (1 << QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
500 ioc
->features
|= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN
);
502 object_ref(OBJECT(master
));
504 trace_qio_channel_websock_new_server(wioc
, master
);
508 void qio_channel_websock_handshake(QIOChannelWebsock
*ioc
,
511 GDestroyNotify destroy
)
515 task
= qio_task_new(OBJECT(ioc
),
520 trace_qio_channel_websock_handshake_start(ioc
);
521 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
522 qio_channel_add_watch(ioc
->master
,
524 qio_channel_websock_handshake_io
,
530 static void qio_channel_websock_finalize(Object
*obj
)
532 QIOChannelWebsock
*ioc
= QIO_CHANNEL_WEBSOCK(obj
);
534 buffer_free(&ioc
->encinput
);
535 buffer_free(&ioc
->encoutput
);
536 buffer_free(&ioc
->rawinput
);
537 buffer_free(&ioc
->rawoutput
);
538 object_unref(OBJECT(ioc
->master
));
540 g_source_remove(ioc
->io_tag
);
543 error_free(ioc
->io_err
);
548 static ssize_t
qio_channel_websock_read_wire(QIOChannelWebsock
*ioc
,
553 if (ioc
->encinput
.offset
< 4096) {
554 size_t want
= 4096 - ioc
->encinput
.offset
;
556 buffer_reserve(&ioc
->encinput
, want
);
557 ret
= qio_channel_read(ioc
->master
,
558 (char *)ioc
->encinput
.buffer
+
559 ioc
->encinput
.offset
,
566 ioc
->encinput
.offset
== 0) {
569 ioc
->encinput
.offset
+= ret
;
572 if (ioc
->payload_remain
== 0) {
573 ret
= qio_channel_websock_decode_header(ioc
, errp
);
582 ret
= qio_channel_websock_decode_payload(ioc
, errp
);
590 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*ioc
,
595 qio_channel_websock_encode(ioc
);
597 while (ioc
->encoutput
.offset
> 0) {
598 ret
= qio_channel_write(ioc
->master
,
599 (char *)ioc
->encoutput
.buffer
,
600 ioc
->encoutput
.offset
,
603 if (ret
== QIO_CHANNEL_ERR_BLOCK
&&
610 buffer_advance(&ioc
->encoutput
, ret
);
617 static void qio_channel_websock_flush_free(gpointer user_data
)
619 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
620 object_unref(OBJECT(wioc
));
623 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
);
625 static gboolean
qio_channel_websock_flush(QIOChannel
*ioc
,
626 GIOCondition condition
,
629 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
632 if (condition
& G_IO_OUT
) {
633 ret
= qio_channel_websock_write_wire(wioc
, &wioc
->io_err
);
639 if (condition
& G_IO_IN
) {
640 ret
= qio_channel_websock_read_wire(wioc
, &wioc
->io_err
);
650 qio_channel_websock_set_watch(wioc
);
655 static void qio_channel_websock_unset_watch(QIOChannelWebsock
*ioc
)
658 g_source_remove(ioc
->io_tag
);
663 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
)
665 GIOCondition cond
= 0;
667 qio_channel_websock_unset_watch(ioc
);
673 if (ioc
->encoutput
.offset
) {
676 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
&&
682 object_ref(OBJECT(ioc
));
684 qio_channel_add_watch(ioc
->master
,
686 qio_channel_websock_flush
,
688 qio_channel_websock_flush_free
);
693 static ssize_t
qio_channel_websock_readv(QIOChannel
*ioc
,
694 const struct iovec
*iov
,
700 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
706 *errp
= error_copy(wioc
->io_err
);
710 if (!wioc
->rawinput
.offset
) {
711 ret
= qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc
), errp
);
717 for (i
= 0 ; i
< niov
; i
++) {
718 size_t want
= iov
[i
].iov_len
;
719 if (want
> (wioc
->rawinput
.offset
- got
)) {
720 want
= (wioc
->rawinput
.offset
- got
);
723 memcpy(iov
[i
].iov_base
,
724 wioc
->rawinput
.buffer
+ got
,
728 if (want
< iov
[i
].iov_len
) {
733 buffer_advance(&wioc
->rawinput
, got
);
734 qio_channel_websock_set_watch(wioc
);
739 static ssize_t
qio_channel_websock_writev(QIOChannel
*ioc
,
740 const struct iovec
*iov
,
746 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
752 *errp
= error_copy(wioc
->io_err
);
757 error_setg(errp
, "%s", "Broken pipe");
761 for (i
= 0; i
< niov
; i
++) {
762 size_t want
= iov
[i
].iov_len
;
763 if ((want
+ wioc
->rawoutput
.offset
) > QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
764 want
= (QIO_CHANNEL_WEBSOCK_MAX_BUFFER
- wioc
->rawoutput
.offset
);
770 buffer_reserve(&wioc
->rawoutput
, want
);
771 buffer_append(&wioc
->rawoutput
, iov
[i
].iov_base
, want
);
773 if (want
< iov
[i
].iov_len
) {
779 ret
= qio_channel_websock_write_wire(wioc
, errp
);
781 ret
!= QIO_CHANNEL_ERR_BLOCK
) {
782 qio_channel_websock_unset_watch(wioc
);
786 qio_channel_websock_set_watch(wioc
);
789 return QIO_CHANNEL_ERR_BLOCK
;
795 static int qio_channel_websock_set_blocking(QIOChannel
*ioc
,
799 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
801 qio_channel_set_blocking(wioc
->master
, enabled
, errp
);
805 static void qio_channel_websock_set_delay(QIOChannel
*ioc
,
808 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
810 qio_channel_set_delay(tioc
->master
, enabled
);
813 static void qio_channel_websock_set_cork(QIOChannel
*ioc
,
816 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
818 qio_channel_set_cork(tioc
->master
, enabled
);
821 static int qio_channel_websock_shutdown(QIOChannel
*ioc
,
822 QIOChannelShutdown how
,
825 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
827 return qio_channel_shutdown(tioc
->master
, how
, errp
);
830 static int qio_channel_websock_close(QIOChannel
*ioc
,
833 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
835 return qio_channel_close(wioc
->master
, errp
);
838 typedef struct QIOChannelWebsockSource QIOChannelWebsockSource
;
839 struct QIOChannelWebsockSource
{
841 QIOChannelWebsock
*wioc
;
842 GIOCondition condition
;
846 qio_channel_websock_source_prepare(GSource
*source
,
849 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
850 GIOCondition cond
= 0;
853 if (wsource
->wioc
->rawinput
.offset
) {
856 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
860 return cond
& wsource
->condition
;
864 qio_channel_websock_source_check(GSource
*source
)
866 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
867 GIOCondition cond
= 0;
869 if (wsource
->wioc
->rawinput
.offset
) {
872 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
876 return cond
& wsource
->condition
;
880 qio_channel_websock_source_dispatch(GSource
*source
,
881 GSourceFunc callback
,
884 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
885 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
886 GIOCondition cond
= 0;
888 if (wsource
->wioc
->rawinput
.offset
) {
891 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
895 return (*func
)(QIO_CHANNEL(wsource
->wioc
),
896 (cond
& wsource
->condition
),
901 qio_channel_websock_source_finalize(GSource
*source
)
903 QIOChannelWebsockSource
*ssource
= (QIOChannelWebsockSource
*)source
;
905 object_unref(OBJECT(ssource
->wioc
));
908 GSourceFuncs qio_channel_websock_source_funcs
= {
909 qio_channel_websock_source_prepare
,
910 qio_channel_websock_source_check
,
911 qio_channel_websock_source_dispatch
,
912 qio_channel_websock_source_finalize
915 static GSource
*qio_channel_websock_create_watch(QIOChannel
*ioc
,
916 GIOCondition condition
)
918 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
919 QIOChannelWebsockSource
*ssource
;
922 source
= g_source_new(&qio_channel_websock_source_funcs
,
923 sizeof(QIOChannelWebsockSource
));
924 ssource
= (QIOChannelWebsockSource
*)source
;
926 ssource
->wioc
= wioc
;
927 object_ref(OBJECT(wioc
));
929 ssource
->condition
= condition
;
931 qio_channel_websock_set_watch(wioc
);
935 static void qio_channel_websock_class_init(ObjectClass
*klass
,
936 void *class_data G_GNUC_UNUSED
)
938 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
940 ioc_klass
->io_writev
= qio_channel_websock_writev
;
941 ioc_klass
->io_readv
= qio_channel_websock_readv
;
942 ioc_klass
->io_set_blocking
= qio_channel_websock_set_blocking
;
943 ioc_klass
->io_set_cork
= qio_channel_websock_set_cork
;
944 ioc_klass
->io_set_delay
= qio_channel_websock_set_delay
;
945 ioc_klass
->io_close
= qio_channel_websock_close
;
946 ioc_klass
->io_shutdown
= qio_channel_websock_shutdown
;
947 ioc_klass
->io_create_watch
= qio_channel_websock_create_watch
;
950 static const TypeInfo qio_channel_websock_info
= {
951 .parent
= TYPE_QIO_CHANNEL
,
952 .name
= TYPE_QIO_CHANNEL_WEBSOCK
,
953 .instance_size
= sizeof(QIOChannelWebsock
),
954 .instance_finalize
= qio_channel_websock_finalize
,
955 .class_init
= qio_channel_websock_class_init
,
958 static void qio_channel_websock_register_types(void)
960 type_register_static(&qio_channel_websock_info
);
963 type_init(qio_channel_websock_register_types
);