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"
28 #include "qemu/module.h"
30 /* Max amount to allow in rawinput/encoutput buffers */
31 #define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
33 #define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
34 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
35 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
37 #define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
38 #define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
39 #define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
40 #define QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE "upgrade"
41 #define QIO_CHANNEL_WEBSOCK_HEADER_HOST "host"
42 #define QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION "connection"
44 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
45 #define QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE "Upgrade"
46 #define QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET "websocket"
48 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
49 "Server: QEMU VNC\r\n" \
52 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_WITH_PROTO_RES_OK \
53 "HTTP/1.1 101 Switching Protocols\r\n" \
54 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
55 "Upgrade: websocket\r\n" \
56 "Connection: Upgrade\r\n" \
57 "Sec-WebSocket-Accept: %s\r\n" \
58 "Sec-WebSocket-Protocol: binary\r\n" \
60 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK \
61 "HTTP/1.1 101 Switching Protocols\r\n" \
62 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
63 "Upgrade: websocket\r\n" \
64 "Connection: Upgrade\r\n" \
65 "Sec-WebSocket-Accept: %s\r\n" \
67 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND \
68 "HTTP/1.1 404 Not Found\r\n" \
69 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
70 "Connection: close\r\n" \
72 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST \
73 "HTTP/1.1 400 Bad Request\r\n" \
74 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
75 "Connection: close\r\n" \
76 "Sec-WebSocket-Version: " \
77 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION \
79 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR \
80 "HTTP/1.1 500 Internal Server Error\r\n" \
81 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
82 "Connection: close\r\n" \
84 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE \
85 "HTTP/1.1 403 Request Entity Too Large\r\n" \
86 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
87 "Connection: close\r\n" \
89 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
90 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
91 #define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
92 #define QIO_CHANNEL_WEBSOCK_HTTP_METHOD "GET"
93 #define QIO_CHANNEL_WEBSOCK_HTTP_PATH "/"
94 #define QIO_CHANNEL_WEBSOCK_HTTP_VERSION "HTTP/1.1"
96 /* The websockets packet header is variable length
97 * depending on the size of the payload... */
99 /* ...length when using 7-bit payload length */
100 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
101 /* ...length when using 16-bit payload length */
102 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
103 /* ...length when using 64-bit payload length */
104 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
106 /* Length of the optional data mask field in header */
107 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
109 /* Maximum length that can fit in 7-bit payload size */
110 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
111 /* Maximum length that can fit in 16-bit payload size */
112 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
114 /* Magic 7-bit length to indicate use of 16-bit payload length */
115 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
116 /* Magic 7-bit length to indicate use of 64-bit payload length */
117 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
119 /* Bitmasks for accessing header fields */
120 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
121 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
122 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
123 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
124 #define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8
126 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader
;
128 struct QEMU_PACKED QIOChannelWebsockHeader
{
134 QIOChannelWebsockMask m16
;
138 QIOChannelWebsockMask m64
;
140 QIOChannelWebsockMask m
;
144 typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader
;
146 struct QIOChannelWebsockHTTPHeader
{
152 QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION
= 0x0,
153 QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME
= 0x1,
154 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
= 0x2,
155 QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
= 0x8,
156 QIO_CHANNEL_WEBSOCK_OPCODE_PING
= 0x9,
157 QIO_CHANNEL_WEBSOCK_OPCODE_PONG
= 0xA
160 static void GCC_FMT_ATTR(2, 3)
161 qio_channel_websock_handshake_send_res(QIOChannelWebsock
*ioc
,
169 va_start(vargs
, resmsg
);
170 response
= g_strdup_vprintf(resmsg
, vargs
);
171 responselen
= strlen(response
);
172 buffer_reserve(&ioc
->encoutput
, responselen
);
173 buffer_append(&ioc
->encoutput
, response
, responselen
);
178 static gchar
*qio_channel_websock_date_str(void)
181 time_t now
= time(NULL
);
186 strftime(datebuf
, sizeof(datebuf
), "%a, %d %b %Y %H:%M:%S GMT", &tm
);
188 return g_strdup(datebuf
);
191 static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock
*ioc
,
194 char *date
= qio_channel_websock_date_str();
195 qio_channel_websock_handshake_send_res(ioc
, resdata
, date
);
200 QIO_CHANNEL_WEBSOCK_STATUS_NORMAL
= 1000,
201 QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
= 1002,
202 QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA
= 1003,
203 QIO_CHANNEL_WEBSOCK_STATUS_POLICY
= 1008,
204 QIO_CHANNEL_WEBSOCK_STATUS_TOO_LARGE
= 1009,
205 QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR
= 1011,
209 qio_channel_websock_extract_headers(QIOChannelWebsock
*ioc
,
211 QIOChannelWebsockHTTPHeader
*hdrs
,
215 char *nl
, *sep
, *tmp
;
219 * First parse the HTTP protocol greeting of format:
221 * $METHOD $PATH $VERSION
228 nl
= strstr(buffer
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
230 error_setg(errp
, "Missing HTTP header delimiter");
234 trace_qio_channel_websock_http_greeting(ioc
, buffer
);
236 tmp
= strchr(buffer
, ' ');
238 error_setg(errp
, "Missing HTTP path delimiter");
243 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_METHOD
)) {
244 error_setg(errp
, "Unsupported HTTP method %s", buffer
);
249 tmp
= strchr(buffer
, ' ');
251 error_setg(errp
, "Missing HTTP version delimiter");
256 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_PATH
)) {
257 qio_channel_websock_handshake_send_res_err(
258 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND
);
259 error_setg(errp
, "Unexpected HTTP path %s", buffer
);
265 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_VERSION
)) {
266 error_setg(errp
, "Unsupported HTTP version %s", buffer
);
270 buffer
= nl
+ strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
273 * Now parse all the header fields of format
279 * Cache-control: no-cache
282 QIOChannelWebsockHTTPHeader
*hdr
;
284 nl
= strstr(buffer
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
289 sep
= strchr(buffer
, ':');
291 error_setg(errp
, "Malformed HTTP header");
296 while (*sep
== ' ') {
300 if (nhdrs
>= nhdrsalloc
) {
301 error_setg(errp
, "Too many HTTP headers");
305 hdr
= &hdrs
[nhdrs
++];
309 /* Canonicalize header name for easier identification later */
310 for (tmp
= hdr
->name
; *tmp
; tmp
++) {
311 *tmp
= g_ascii_tolower(*tmp
);
315 buffer
= nl
+ strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
317 } while (nl
!= NULL
);
322 qio_channel_websock_handshake_send_res_err(
323 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST
);
328 qio_channel_websock_find_header(QIOChannelWebsockHTTPHeader
*hdrs
,
334 for (i
= 0; i
< nhdrs
; i
++) {
335 if (g_str_equal(hdrs
[i
].name
, name
)) {
336 return hdrs
[i
].value
;
344 static void qio_channel_websock_handshake_send_res_ok(QIOChannelWebsock
*ioc
,
346 const bool use_protocols
,
349 char combined_key
[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
350 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1];
354 g_strlcpy(combined_key
, key
, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+ 1);
355 g_strlcat(combined_key
, QIO_CHANNEL_WEBSOCK_GUID
,
356 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
357 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1);
359 /* hash and encode it */
360 if (qcrypto_hash_base64(QCRYPTO_HASH_ALG_SHA1
,
362 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
363 QIO_CHANNEL_WEBSOCK_GUID_LEN
,
366 qio_channel_websock_handshake_send_res_err(
367 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR
);
371 date
= qio_channel_websock_date_str();
373 qio_channel_websock_handshake_send_res(
374 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_WITH_PROTO_RES_OK
,
377 qio_channel_websock_handshake_send_res(
378 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK
, date
, accept
);
385 static void qio_channel_websock_handshake_process(QIOChannelWebsock
*ioc
,
389 QIOChannelWebsockHTTPHeader hdrs
[32];
390 size_t nhdrs
= G_N_ELEMENTS(hdrs
);
391 const char *protocols
= NULL
, *version
= NULL
, *key
= NULL
,
392 *host
= NULL
, *connection
= NULL
, *upgrade
= NULL
;
394 bool upgraded
= false;
397 nhdrs
= qio_channel_websock_extract_headers(ioc
, buffer
, hdrs
, nhdrs
, errp
);
402 protocols
= qio_channel_websock_find_header(
403 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL
);
405 version
= qio_channel_websock_find_header(
406 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_VERSION
);
408 error_setg(errp
, "Missing websocket version header data");
412 key
= qio_channel_websock_find_header(
413 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_KEY
);
415 error_setg(errp
, "Missing websocket key header data");
419 host
= qio_channel_websock_find_header(
420 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_HOST
);
422 error_setg(errp
, "Missing websocket host header data");
426 connection
= qio_channel_websock_find_header(
427 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION
);
429 error_setg(errp
, "Missing websocket connection header data");
433 upgrade
= qio_channel_websock_find_header(
434 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE
);
436 error_setg(errp
, "Missing websocket upgrade header data");
440 trace_qio_channel_websock_http_request(ioc
, protocols
, version
,
441 host
, connection
, upgrade
, key
);
444 if (!g_strrstr(protocols
, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
)) {
445 error_setg(errp
, "No '%s' protocol is supported by client '%s'",
446 QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
, protocols
);
451 if (!g_str_equal(version
, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
)) {
452 error_setg(errp
, "Version '%s' is not supported by client '%s'",
453 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
, version
);
457 if (strlen(key
) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
) {
458 error_setg(errp
, "Key length '%zu' was not as expected '%d'",
459 strlen(key
), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
);
463 connectionv
= g_strsplit(connection
, ",", 0);
464 for (i
= 0; connectionv
!= NULL
&& connectionv
[i
] != NULL
; i
++) {
465 g_strstrip(connectionv
[i
]);
466 if (strcasecmp(connectionv
[i
],
467 QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE
) == 0) {
471 g_strfreev(connectionv
);
473 error_setg(errp
, "No connection upgrade requested '%s'", connection
);
477 if (strcasecmp(upgrade
, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET
) != 0) {
478 error_setg(errp
, "Incorrect upgrade method '%s'", upgrade
);
482 qio_channel_websock_handshake_send_res_ok(ioc
, key
, !!protocols
, errp
);
486 qio_channel_websock_handshake_send_res_err(
487 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST
);
490 static int qio_channel_websock_handshake_read(QIOChannelWebsock
*ioc
,
495 /* Typical HTTP headers from novnc are 512 bytes, so limiting
496 * total header size to 4096 is easily enough. */
497 size_t want
= 4096 - ioc
->encinput
.offset
;
498 buffer_reserve(&ioc
->encinput
, want
);
499 ret
= qio_channel_read(ioc
->master
,
500 (char *)buffer_end(&ioc
->encinput
), want
, errp
);
504 ioc
->encinput
.offset
+= ret
;
506 handshake_end
= g_strstr_len((char *)ioc
->encinput
.buffer
,
507 ioc
->encinput
.offset
,
508 QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
);
509 if (!handshake_end
) {
510 if (ioc
->encinput
.offset
>= 4096) {
511 qio_channel_websock_handshake_send_res_err(
512 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE
);
514 "End of headers not found in first 4096 bytes");
516 } else if (ret
== 0) {
518 "End of headers not found before connection closed");
523 *handshake_end
= '\0';
525 qio_channel_websock_handshake_process(ioc
,
526 (char *)ioc
->encinput
.buffer
,
529 buffer_advance(&ioc
->encinput
,
530 handshake_end
- (char *)ioc
->encinput
.buffer
+
531 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
));
535 static gboolean
qio_channel_websock_handshake_send(QIOChannel
*ioc
,
536 GIOCondition condition
,
539 QIOTask
*task
= user_data
;
540 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
541 qio_task_get_source(task
));
545 ret
= qio_channel_write(wioc
->master
,
546 (char *)wioc
->encoutput
.buffer
,
547 wioc
->encoutput
.offset
,
551 trace_qio_channel_websock_handshake_fail(ioc
, error_get_pretty(err
));
552 qio_task_set_error(task
, err
);
553 qio_task_complete(task
);
557 buffer_advance(&wioc
->encoutput
, ret
);
558 if (wioc
->encoutput
.offset
== 0) {
560 trace_qio_channel_websock_handshake_fail(
561 ioc
, error_get_pretty(wioc
->io_err
));
562 qio_task_set_error(task
, wioc
->io_err
);
564 qio_task_complete(task
);
566 trace_qio_channel_websock_handshake_complete(ioc
);
567 qio_task_complete(task
);
571 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_OUT
);
575 static gboolean
qio_channel_websock_handshake_io(QIOChannel
*ioc
,
576 GIOCondition condition
,
579 QIOTask
*task
= user_data
;
580 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
581 qio_task_get_source(task
));
585 ret
= qio_channel_websock_handshake_read(wioc
, &err
);
588 * We only take this path on a fatal I/O error reading from
589 * client connection, as most of the time we have an
590 * HTTP 4xx err response to send instead
592 trace_qio_channel_websock_handshake_fail(ioc
, error_get_pretty(err
));
593 qio_task_set_error(task
, err
);
594 qio_task_complete(task
);
598 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
599 /* need more data still */
603 error_propagate(&wioc
->io_err
, err
);
605 trace_qio_channel_websock_handshake_reply(ioc
);
606 qio_channel_add_watch(
609 qio_channel_websock_handshake_send
,
616 static void qio_channel_websock_encode(QIOChannelWebsock
*ioc
,
618 const struct iovec
*iov
,
625 char buf
[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
];
626 QIOChannelWebsockHeader ws
;
629 assert(size
<= iov_size(iov
, niov
));
631 header
.ws
.b0
= QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN
|
632 (opcode
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
);
633 if (size
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT
) {
634 header
.ws
.b1
= (uint8_t)size
;
635 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
636 } else if (size
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT
) {
637 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
;
638 header
.ws
.u
.s16
.l16
= cpu_to_be16((uint16_t)size
);
639 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
641 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
;
642 header
.ws
.u
.s64
.l64
= cpu_to_be64(size
);
643 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
645 header_size
-= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK
;
647 trace_qio_channel_websock_encode(ioc
, opcode
, header_size
, size
);
648 buffer_reserve(&ioc
->encoutput
, header_size
+ size
);
649 buffer_append(&ioc
->encoutput
, header
.buf
, header_size
);
650 for (i
= 0; i
< niov
&& size
!= 0; i
++) {
651 size_t want
= iov
[i
].iov_len
;
655 buffer_append(&ioc
->encoutput
, iov
[i
].iov_base
, want
);
661 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*, Error
**);
664 static void qio_channel_websock_write_close(QIOChannelWebsock
*ioc
,
665 uint16_t code
, const char *reason
)
667 struct iovec iov
[2] = {
668 { .iov_base
= &code
, .iov_len
= sizeof(code
) },
671 size_t size
= iov
[0].iov_len
;
676 iov
[1].iov_base
= (void *)reason
;
677 iov
[1].iov_len
= strlen(reason
);
678 size
+= iov
[1].iov_len
;
681 qio_channel_websock_encode(ioc
, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
,
683 qio_channel_websock_write_wire(ioc
, NULL
);
684 qio_channel_shutdown(ioc
->master
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
688 static int qio_channel_websock_decode_header(QIOChannelWebsock
*ioc
,
691 unsigned char opcode
, fin
, has_mask
;
694 QIOChannelWebsockHeader
*header
=
695 (QIOChannelWebsockHeader
*)ioc
->encinput
.buffer
;
697 if (ioc
->payload_remain
) {
699 "Decoding header but %zu bytes of payload remain",
700 ioc
->payload_remain
);
701 qio_channel_websock_write_close(
702 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR
,
703 "internal server error");
706 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
) {
707 /* header not complete */
708 return QIO_CHANNEL_ERR_BLOCK
;
711 fin
= header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN
;
712 opcode
= header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
;
713 has_mask
= header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK
;
714 payload_len
= header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN
;
716 /* Save or restore opcode. */
718 ioc
->opcode
= opcode
;
720 opcode
= ioc
->opcode
;
723 trace_qio_channel_websock_header_partial_decode(ioc
, payload_len
,
724 fin
, opcode
, (int)has_mask
);
726 if (opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
) {
731 /* Websocket frame sanity check:
732 * * Fragmentation is only supported for binary frames.
733 * * All frames sent by a client MUST be masked.
734 * * Only binary and ping/pong encoding is supported.
737 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
738 error_setg(errp
, "only binary websocket frames may be fragmented");
739 qio_channel_websock_write_close(
740 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_POLICY
,
741 "only binary frames may be fragmented");
745 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
&&
746 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
&&
747 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_PING
&&
748 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_PONG
) {
749 error_setg(errp
, "unsupported opcode: %#04x; only binary, close, "
750 "ping, and pong websocket frames are supported", opcode
);
751 qio_channel_websock_write_close(
752 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA
,
753 "only binary, close, ping, and pong frames are supported");
758 error_setg(errp
, "client websocket frames must be masked");
759 qio_channel_websock_write_close(
760 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
,
761 "client frames must be masked");
765 if (payload_len
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
) {
766 ioc
->payload_remain
= payload_len
;
767 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
768 ioc
->mask
= header
->u
.m
;
769 } else if (opcode
& QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK
) {
770 error_setg(errp
, "websocket control frame is too large");
771 qio_channel_websock_write_close(
772 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
,
773 "control frame is too large");
775 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
&&
776 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
) {
777 ioc
->payload_remain
= be16_to_cpu(header
->u
.s16
.l16
);
778 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
779 ioc
->mask
= header
->u
.s16
.m16
;
780 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
&&
781 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
) {
782 ioc
->payload_remain
= be64_to_cpu(header
->u
.s64
.l64
);
783 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
784 ioc
->mask
= header
->u
.s64
.m64
;
786 /* header not complete */
787 return QIO_CHANNEL_ERR_BLOCK
;
790 trace_qio_channel_websock_header_full_decode(
791 ioc
, header_size
, ioc
->payload_remain
, ioc
->mask
.u
);
792 buffer_advance(&ioc
->encinput
, header_size
);
797 static int qio_channel_websock_decode_payload(QIOChannelWebsock
*ioc
,
801 size_t payload_len
= 0;
804 if (ioc
->payload_remain
) {
805 /* If we aren't at the end of the payload, then drop
806 * off the last bytes, so we're always multiple of 4
807 * for purpose of unmasking, except at end of payload
809 if (ioc
->encinput
.offset
< ioc
->payload_remain
) {
810 /* Wait for the entire payload before processing control frames
811 * because the payload will most likely be echoed back. */
812 if (ioc
->opcode
& QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK
) {
813 return QIO_CHANNEL_ERR_BLOCK
;
815 payload_len
= ioc
->encinput
.offset
- (ioc
->encinput
.offset
% 4);
817 payload_len
= ioc
->payload_remain
;
819 if (payload_len
== 0) {
820 return QIO_CHANNEL_ERR_BLOCK
;
823 ioc
->payload_remain
-= payload_len
;
826 /* process 1 frame (32 bit op) */
827 payload32
= (uint32_t *)ioc
->encinput
.buffer
;
828 for (i
= 0; i
< payload_len
/ 4; i
++) {
829 payload32
[i
] ^= ioc
->mask
.u
;
831 /* process the remaining bytes (if any) */
832 for (i
*= 4; i
< payload_len
; i
++) {
833 ioc
->encinput
.buffer
[i
] ^= ioc
->mask
.c
[i
% 4];
837 trace_qio_channel_websock_payload_decode(
838 ioc
, ioc
->opcode
, ioc
->payload_remain
);
840 if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
842 /* binary frames are passed on */
843 buffer_reserve(&ioc
->rawinput
, payload_len
);
844 buffer_append(&ioc
->rawinput
, ioc
->encinput
.buffer
, payload_len
);
846 } else if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
) {
847 /* close frames are echoed back */
848 error_setg(errp
, "websocket closed by peer");
850 /* echo client status */
851 struct iovec iov
= { .iov_base
= ioc
->encinput
.buffer
,
852 .iov_len
= ioc
->encinput
.offset
};
853 qio_channel_websock_encode(ioc
, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
,
854 &iov
, 1, iov
.iov_len
);
855 qio_channel_websock_write_wire(ioc
, NULL
);
856 qio_channel_shutdown(ioc
->master
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
858 /* send our own status */
859 qio_channel_websock_write_close(
860 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_NORMAL
, "peer requested close");
863 } else if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_PING
) {
864 /* ping frames produce an immediate reply, as long as we've not still
865 * got a previous pong queued, in which case we drop the new pong */
866 if (ioc
->pong_remain
== 0) {
867 struct iovec iov
= { .iov_base
= ioc
->encinput
.buffer
,
868 .iov_len
= ioc
->encinput
.offset
};
869 qio_channel_websock_encode(ioc
, QIO_CHANNEL_WEBSOCK_OPCODE_PONG
,
870 &iov
, 1, iov
.iov_len
);
871 ioc
->pong_remain
= ioc
->encoutput
.offset
;
873 } /* pong frames are ignored */
876 buffer_advance(&ioc
->encinput
, payload_len
);
883 qio_channel_websock_new_server(QIOChannel
*master
)
885 QIOChannelWebsock
*wioc
;
888 wioc
= QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK
));
889 ioc
= QIO_CHANNEL(wioc
);
891 wioc
->master
= master
;
892 if (qio_channel_has_feature(master
, QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
893 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
895 object_ref(OBJECT(master
));
897 trace_qio_channel_websock_new_server(wioc
, master
);
901 void qio_channel_websock_handshake(QIOChannelWebsock
*ioc
,
904 GDestroyNotify destroy
)
908 task
= qio_task_new(OBJECT(ioc
),
913 trace_qio_channel_websock_handshake_start(ioc
);
914 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
915 qio_channel_add_watch(ioc
->master
,
917 qio_channel_websock_handshake_io
,
923 static void qio_channel_websock_finalize(Object
*obj
)
925 QIOChannelWebsock
*ioc
= QIO_CHANNEL_WEBSOCK(obj
);
927 buffer_free(&ioc
->encinput
);
928 buffer_free(&ioc
->encoutput
);
929 buffer_free(&ioc
->rawinput
);
930 object_unref(OBJECT(ioc
->master
));
932 g_source_remove(ioc
->io_tag
);
935 error_free(ioc
->io_err
);
940 static ssize_t
qio_channel_websock_read_wire(QIOChannelWebsock
*ioc
,
945 if (ioc
->encinput
.offset
< 4096) {
946 size_t want
= 4096 - ioc
->encinput
.offset
;
948 buffer_reserve(&ioc
->encinput
, want
);
949 ret
= qio_channel_read(ioc
->master
,
950 (char *)ioc
->encinput
.buffer
+
951 ioc
->encinput
.offset
,
957 if (ret
== 0 && ioc
->encinput
.offset
== 0) {
961 ioc
->encinput
.offset
+= ret
;
964 while (ioc
->encinput
.offset
!= 0) {
965 if (ioc
->payload_remain
== 0) {
966 ret
= qio_channel_websock_decode_header(ioc
, errp
);
972 ret
= qio_channel_websock_decode_payload(ioc
, errp
);
981 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*ioc
,
987 while (ioc
->encoutput
.offset
> 0) {
988 ret
= qio_channel_write(ioc
->master
,
989 (char *)ioc
->encoutput
.buffer
,
990 ioc
->encoutput
.offset
,
993 if (ret
== QIO_CHANNEL_ERR_BLOCK
&&
1000 buffer_advance(&ioc
->encoutput
, ret
);
1002 if (ioc
->pong_remain
< ret
) {
1003 ioc
->pong_remain
= 0;
1005 ioc
->pong_remain
-= ret
;
1012 static void qio_channel_websock_flush_free(gpointer user_data
)
1014 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
1015 object_unref(OBJECT(wioc
));
1018 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
);
1020 static gboolean
qio_channel_websock_flush(QIOChannel
*ioc
,
1021 GIOCondition condition
,
1024 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
1027 if (condition
& G_IO_OUT
) {
1028 ret
= qio_channel_websock_write_wire(wioc
, &wioc
->io_err
);
1034 if (condition
& G_IO_IN
) {
1035 ret
= qio_channel_websock_read_wire(wioc
, &wioc
->io_err
);
1042 qio_channel_websock_set_watch(wioc
);
1047 static void qio_channel_websock_unset_watch(QIOChannelWebsock
*ioc
)
1050 g_source_remove(ioc
->io_tag
);
1055 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
)
1057 GIOCondition cond
= 0;
1059 qio_channel_websock_unset_watch(ioc
);
1065 if (ioc
->encoutput
.offset
) {
1068 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
&&
1074 object_ref(OBJECT(ioc
));
1076 qio_channel_add_watch(ioc
->master
,
1078 qio_channel_websock_flush
,
1080 qio_channel_websock_flush_free
);
1085 static ssize_t
qio_channel_websock_readv(QIOChannel
*ioc
,
1086 const struct iovec
*iov
,
1092 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1098 error_propagate(errp
, error_copy(wioc
->io_err
));
1102 if (!wioc
->rawinput
.offset
) {
1103 ret
= qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc
), errp
);
1109 for (i
= 0 ; i
< niov
; i
++) {
1110 size_t want
= iov
[i
].iov_len
;
1111 if (want
> (wioc
->rawinput
.offset
- got
)) {
1112 want
= (wioc
->rawinput
.offset
- got
);
1115 memcpy(iov
[i
].iov_base
,
1116 wioc
->rawinput
.buffer
+ got
,
1120 if (want
< iov
[i
].iov_len
) {
1125 buffer_advance(&wioc
->rawinput
, got
);
1126 qio_channel_websock_set_watch(wioc
);
1131 static ssize_t
qio_channel_websock_writev(QIOChannel
*ioc
,
1132 const struct iovec
*iov
,
1138 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1139 ssize_t want
= iov_size(iov
, niov
);
1144 error_propagate(errp
, error_copy(wioc
->io_err
));
1149 error_setg(errp
, "%s", "Broken pipe");
1153 avail
= wioc
->encoutput
.offset
>= QIO_CHANNEL_WEBSOCK_MAX_BUFFER
?
1154 0 : (QIO_CHANNEL_WEBSOCK_MAX_BUFFER
- wioc
->encoutput
.offset
);
1160 qio_channel_websock_encode(wioc
,
1161 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
,
1165 /* Even if want == 0, we'll try write_wire in case there's
1166 * pending data we could usefully flush out
1168 ret
= qio_channel_websock_write_wire(wioc
, errp
);
1170 ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1171 qio_channel_websock_unset_watch(wioc
);
1175 qio_channel_websock_set_watch(wioc
);
1178 return QIO_CHANNEL_ERR_BLOCK
;
1184 static int qio_channel_websock_set_blocking(QIOChannel
*ioc
,
1188 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1190 qio_channel_set_blocking(wioc
->master
, enabled
, errp
);
1194 static void qio_channel_websock_set_delay(QIOChannel
*ioc
,
1197 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1199 qio_channel_set_delay(tioc
->master
, enabled
);
1202 static void qio_channel_websock_set_cork(QIOChannel
*ioc
,
1205 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1207 qio_channel_set_cork(tioc
->master
, enabled
);
1210 static int qio_channel_websock_shutdown(QIOChannel
*ioc
,
1211 QIOChannelShutdown how
,
1214 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1216 return qio_channel_shutdown(tioc
->master
, how
, errp
);
1219 static int qio_channel_websock_close(QIOChannel
*ioc
,
1222 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1224 trace_qio_channel_websock_close(ioc
);
1225 return qio_channel_close(wioc
->master
, errp
);
1228 typedef struct QIOChannelWebsockSource QIOChannelWebsockSource
;
1229 struct QIOChannelWebsockSource
{
1231 QIOChannelWebsock
*wioc
;
1232 GIOCondition condition
;
1236 qio_channel_websock_source_check(GSource
*source
)
1238 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1239 GIOCondition cond
= 0;
1241 if (wsource
->wioc
->rawinput
.offset
) {
1244 if (wsource
->wioc
->encoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
1247 if (wsource
->wioc
->io_eof
) {
1250 if (wsource
->wioc
->io_err
) {
1254 return cond
& wsource
->condition
;
1258 qio_channel_websock_source_prepare(GSource
*source
,
1262 return qio_channel_websock_source_check(source
);
1266 qio_channel_websock_source_dispatch(GSource
*source
,
1267 GSourceFunc callback
,
1270 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
1271 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1273 return (*func
)(QIO_CHANNEL(wsource
->wioc
),
1274 qio_channel_websock_source_check(source
),
1279 qio_channel_websock_source_finalize(GSource
*source
)
1281 QIOChannelWebsockSource
*ssource
= (QIOChannelWebsockSource
*)source
;
1283 object_unref(OBJECT(ssource
->wioc
));
1286 GSourceFuncs qio_channel_websock_source_funcs
= {
1287 qio_channel_websock_source_prepare
,
1288 qio_channel_websock_source_check
,
1289 qio_channel_websock_source_dispatch
,
1290 qio_channel_websock_source_finalize
1293 static GSource
*qio_channel_websock_create_watch(QIOChannel
*ioc
,
1294 GIOCondition condition
)
1296 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1297 QIOChannelWebsockSource
*ssource
;
1300 source
= g_source_new(&qio_channel_websock_source_funcs
,
1301 sizeof(QIOChannelWebsockSource
));
1302 ssource
= (QIOChannelWebsockSource
*)source
;
1304 ssource
->wioc
= wioc
;
1305 object_ref(OBJECT(wioc
));
1307 ssource
->condition
= condition
;
1309 qio_channel_websock_set_watch(wioc
);
1313 static void qio_channel_websock_class_init(ObjectClass
*klass
,
1314 void *class_data G_GNUC_UNUSED
)
1316 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
1318 ioc_klass
->io_writev
= qio_channel_websock_writev
;
1319 ioc_klass
->io_readv
= qio_channel_websock_readv
;
1320 ioc_klass
->io_set_blocking
= qio_channel_websock_set_blocking
;
1321 ioc_klass
->io_set_cork
= qio_channel_websock_set_cork
;
1322 ioc_klass
->io_set_delay
= qio_channel_websock_set_delay
;
1323 ioc_klass
->io_close
= qio_channel_websock_close
;
1324 ioc_klass
->io_shutdown
= qio_channel_websock_shutdown
;
1325 ioc_klass
->io_create_watch
= qio_channel_websock_create_watch
;
1328 static const TypeInfo qio_channel_websock_info
= {
1329 .parent
= TYPE_QIO_CHANNEL
,
1330 .name
= TYPE_QIO_CHANNEL_WEBSOCK
,
1331 .instance_size
= sizeof(QIOChannelWebsock
),
1332 .instance_finalize
= qio_channel_websock_finalize
,
1333 .class_init
= qio_channel_websock_class_init
,
1336 static void qio_channel_websock_register_types(void)
1338 type_register_static(&qio_channel_websock_info
);
1341 type_init(qio_channel_websock_register_types
);