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"
31 /* Max amount to allow in rawinput/rawoutput buffers */
32 #define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
34 #define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
35 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
36 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
38 #define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
39 #define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
40 #define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
41 #define QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE "upgrade"
42 #define QIO_CHANNEL_WEBSOCK_HEADER_HOST "host"
43 #define QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION "connection"
45 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
46 #define QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE "Upgrade"
47 #define QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET "websocket"
49 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
50 "Server: QEMU VNC\r\n" \
53 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK \
54 "HTTP/1.1 101 Switching Protocols\r\n" \
55 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
56 "Upgrade: websocket\r\n" \
57 "Connection: Upgrade\r\n" \
58 "Sec-WebSocket-Accept: %s\r\n" \
59 "Sec-WebSocket-Protocol: binary\r\n" \
61 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND \
62 "HTTP/1.1 404 Not Found\r\n" \
63 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
64 "Connection: close\r\n" \
66 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST \
67 "HTTP/1.1 400 Bad Request\r\n" \
68 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
69 "Connection: close\r\n" \
70 "Sec-WebSocket-Version: " \
71 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION \
73 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR \
74 "HTTP/1.1 500 Internal Server Error\r\n" \
75 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
76 "Connection: close\r\n" \
78 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE \
79 "HTTP/1.1 403 Request Entity Too Large\r\n" \
80 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
81 "Connection: close\r\n" \
83 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
84 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
85 #define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
86 #define QIO_CHANNEL_WEBSOCK_HTTP_METHOD "GET"
87 #define QIO_CHANNEL_WEBSOCK_HTTP_PATH "/"
88 #define QIO_CHANNEL_WEBSOCK_HTTP_VERSION "HTTP/1.1"
90 /* The websockets packet header is variable length
91 * depending on the size of the payload... */
93 /* ...length when using 7-bit payload length */
94 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
95 /* ...length when using 16-bit payload length */
96 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
97 /* ...length when using 64-bit payload length */
98 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
100 /* Length of the optional data mask field in header */
101 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
103 /* Maximum length that can fit in 7-bit payload size */
104 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
105 /* Maximum length that can fit in 16-bit payload size */
106 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
108 /* Magic 7-bit length to indicate use of 16-bit payload length */
109 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
110 /* Magic 7-bit length to indicate use of 64-bit payload length */
111 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
113 /* Bitmasks for accessing header fields */
114 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
115 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
116 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
117 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
118 #define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8
120 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader
;
122 struct QEMU_PACKED QIOChannelWebsockHeader
{
128 QIOChannelWebsockMask m16
;
132 QIOChannelWebsockMask m64
;
134 QIOChannelWebsockMask m
;
138 typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader
;
140 struct QIOChannelWebsockHTTPHeader
{
146 QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION
= 0x0,
147 QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME
= 0x1,
148 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
= 0x2,
149 QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
= 0x8,
150 QIO_CHANNEL_WEBSOCK_OPCODE_PING
= 0x9,
151 QIO_CHANNEL_WEBSOCK_OPCODE_PONG
= 0xA
154 static void qio_channel_websock_handshake_send_res(QIOChannelWebsock
*ioc
,
162 va_start(vargs
, resmsg
);
163 response
= g_strdup_vprintf(resmsg
, vargs
);
164 responselen
= strlen(response
);
165 buffer_reserve(&ioc
->encoutput
, responselen
);
166 buffer_append(&ioc
->encoutput
, response
, responselen
);
170 static gchar
*qio_channel_websock_date_str(void)
173 time_t now
= time(NULL
);
178 strftime(datebuf
, sizeof(datebuf
), "%a, %d %b %Y %H:%M:%S GMT", &tm
);
180 return g_strdup(datebuf
);
183 static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock
*ioc
,
186 char *date
= qio_channel_websock_date_str();
187 qio_channel_websock_handshake_send_res(ioc
, resdata
, date
);
192 QIO_CHANNEL_WEBSOCK_STATUS_NORMAL
= 1000,
193 QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
= 1002,
194 QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA
= 1003,
195 QIO_CHANNEL_WEBSOCK_STATUS_POLICY
= 1008,
196 QIO_CHANNEL_WEBSOCK_STATUS_TOO_LARGE
= 1009,
197 QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR
= 1011,
201 qio_channel_websock_extract_headers(QIOChannelWebsock
*ioc
,
203 QIOChannelWebsockHTTPHeader
*hdrs
,
207 char *nl
, *sep
, *tmp
;
211 * First parse the HTTP protocol greeting of format:
213 * $METHOD $PATH $VERSION
220 nl
= strstr(buffer
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
222 error_setg(errp
, "Missing HTTP header delimiter");
227 tmp
= strchr(buffer
, ' ');
229 error_setg(errp
, "Missing HTTP path delimiter");
234 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_METHOD
)) {
235 error_setg(errp
, "Unsupported HTTP method %s", buffer
);
240 tmp
= strchr(buffer
, ' ');
242 error_setg(errp
, "Missing HTTP version delimiter");
247 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_PATH
)) {
248 qio_channel_websock_handshake_send_res_err(
249 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND
);
250 error_setg(errp
, "Unexpected HTTP path %s", buffer
);
256 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_VERSION
)) {
257 error_setg(errp
, "Unsupported HTTP version %s", buffer
);
261 buffer
= nl
+ strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
264 * Now parse all the header fields of format
270 * Cache-control: no-cache
273 QIOChannelWebsockHTTPHeader
*hdr
;
275 nl
= strstr(buffer
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
280 sep
= strchr(buffer
, ':');
282 error_setg(errp
, "Malformed HTTP header");
287 while (*sep
== ' ') {
291 if (nhdrs
>= nhdrsalloc
) {
292 error_setg(errp
, "Too many HTTP headers");
296 hdr
= &hdrs
[nhdrs
++];
300 /* Canonicalize header name for easier identification later */
301 for (tmp
= hdr
->name
; *tmp
; tmp
++) {
302 *tmp
= g_ascii_tolower(*tmp
);
306 buffer
= nl
+ strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
308 } while (nl
!= NULL
);
313 qio_channel_websock_handshake_send_res_err(
314 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST
);
319 qio_channel_websock_find_header(QIOChannelWebsockHTTPHeader
*hdrs
,
325 for (i
= 0; i
< nhdrs
; i
++) {
326 if (g_str_equal(hdrs
[i
].name
, name
)) {
327 return hdrs
[i
].value
;
335 static void qio_channel_websock_handshake_send_res_ok(QIOChannelWebsock
*ioc
,
339 char combined_key
[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
340 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1];
342 char *date
= qio_channel_websock_date_str();
344 g_strlcpy(combined_key
, key
, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+ 1);
345 g_strlcat(combined_key
, QIO_CHANNEL_WEBSOCK_GUID
,
346 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
347 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1);
349 /* hash and encode it */
350 if (qcrypto_hash_base64(QCRYPTO_HASH_ALG_SHA1
,
352 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
353 QIO_CHANNEL_WEBSOCK_GUID_LEN
,
356 qio_channel_websock_handshake_send_res_err(
357 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR
);
361 qio_channel_websock_handshake_send_res(
362 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK
, date
, accept
);
368 static void qio_channel_websock_handshake_process(QIOChannelWebsock
*ioc
,
372 QIOChannelWebsockHTTPHeader hdrs
[32];
373 size_t nhdrs
= G_N_ELEMENTS(hdrs
);
374 const char *protocols
= NULL
, *version
= NULL
, *key
= NULL
,
375 *host
= NULL
, *connection
= NULL
, *upgrade
= NULL
;
377 nhdrs
= qio_channel_websock_extract_headers(ioc
, buffer
, hdrs
, nhdrs
, errp
);
382 protocols
= qio_channel_websock_find_header(
383 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL
);
385 error_setg(errp
, "Missing websocket protocol header data");
389 version
= qio_channel_websock_find_header(
390 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_VERSION
);
392 error_setg(errp
, "Missing websocket version header data");
396 key
= qio_channel_websock_find_header(
397 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_KEY
);
399 error_setg(errp
, "Missing websocket key header data");
403 host
= qio_channel_websock_find_header(
404 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_HOST
);
406 error_setg(errp
, "Missing websocket host header data");
410 connection
= qio_channel_websock_find_header(
411 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION
);
413 error_setg(errp
, "Missing websocket connection header data");
417 upgrade
= qio_channel_websock_find_header(
418 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE
);
420 error_setg(errp
, "Missing websocket upgrade header data");
424 if (!g_strrstr(protocols
, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
)) {
425 error_setg(errp
, "No '%s' protocol is supported by client '%s'",
426 QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
, protocols
);
430 if (!g_str_equal(version
, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
)) {
431 error_setg(errp
, "Version '%s' is not supported by client '%s'",
432 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
, version
);
436 if (strlen(key
) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
) {
437 error_setg(errp
, "Key length '%zu' was not as expected '%d'",
438 strlen(key
), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
);
442 if (strcasecmp(connection
, QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE
) != 0) {
443 error_setg(errp
, "No connection upgrade requested '%s'", connection
);
447 if (strcasecmp(upgrade
, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET
) != 0) {
448 error_setg(errp
, "Incorrect upgrade method '%s'", upgrade
);
452 qio_channel_websock_handshake_send_res_ok(ioc
, key
, errp
);
456 qio_channel_websock_handshake_send_res_err(
457 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST
);
460 static int qio_channel_websock_handshake_read(QIOChannelWebsock
*ioc
,
465 /* Typical HTTP headers from novnc are 512 bytes, so limiting
466 * total header size to 4096 is easily enough. */
467 size_t want
= 4096 - ioc
->encinput
.offset
;
468 buffer_reserve(&ioc
->encinput
, want
);
469 ret
= qio_channel_read(ioc
->master
,
470 (char *)buffer_end(&ioc
->encinput
), want
, errp
);
474 ioc
->encinput
.offset
+= ret
;
476 handshake_end
= g_strstr_len((char *)ioc
->encinput
.buffer
,
477 ioc
->encinput
.offset
,
478 QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
);
479 if (!handshake_end
) {
480 if (ioc
->encinput
.offset
>= 4096) {
481 qio_channel_websock_handshake_send_res_err(
482 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE
);
484 "End of headers not found in first 4096 bytes");
490 *handshake_end
= '\0';
492 qio_channel_websock_handshake_process(ioc
,
493 (char *)ioc
->encinput
.buffer
,
496 buffer_advance(&ioc
->encinput
,
497 handshake_end
- (char *)ioc
->encinput
.buffer
+
498 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
));
502 static gboolean
qio_channel_websock_handshake_send(QIOChannel
*ioc
,
503 GIOCondition condition
,
506 QIOTask
*task
= user_data
;
507 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
508 qio_task_get_source(task
));
512 ret
= qio_channel_write(wioc
->master
,
513 (char *)wioc
->encoutput
.buffer
,
514 wioc
->encoutput
.offset
,
518 trace_qio_channel_websock_handshake_fail(ioc
, error_get_pretty(err
));
519 qio_task_set_error(task
, err
);
520 qio_task_complete(task
);
524 buffer_advance(&wioc
->encoutput
, ret
);
525 if (wioc
->encoutput
.offset
== 0) {
527 trace_qio_channel_websock_handshake_fail(
528 ioc
, error_get_pretty(wioc
->io_err
));
529 qio_task_set_error(task
, wioc
->io_err
);
531 qio_task_complete(task
);
533 trace_qio_channel_websock_handshake_complete(ioc
);
534 qio_task_complete(task
);
538 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_OUT
);
542 static gboolean
qio_channel_websock_handshake_io(QIOChannel
*ioc
,
543 GIOCondition condition
,
546 QIOTask
*task
= user_data
;
547 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
548 qio_task_get_source(task
));
552 ret
= qio_channel_websock_handshake_read(wioc
, &err
);
555 * We only take this path on a fatal I/O error reading from
556 * client connection, as most of the time we have an
557 * HTTP 4xx err response to send instead
559 trace_qio_channel_websock_handshake_fail(ioc
, error_get_pretty(err
));
560 qio_task_set_error(task
, err
);
561 qio_task_complete(task
);
565 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
566 /* need more data still */
571 error_propagate(&wioc
->io_err
, err
);
574 trace_qio_channel_websock_handshake_reply(ioc
);
575 qio_channel_add_watch(
578 qio_channel_websock_handshake_send
,
585 static void qio_channel_websock_encode_buffer(QIOChannelWebsock
*ioc
,
587 uint8_t opcode
, Buffer
*buffer
)
591 char buf
[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
];
592 QIOChannelWebsockHeader ws
;
595 header
.ws
.b0
= QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN
|
596 (opcode
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
);
597 if (buffer
->offset
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT
) {
598 header
.ws
.b1
= (uint8_t)buffer
->offset
;
599 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
600 } else if (buffer
->offset
<
601 QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT
) {
602 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
;
603 header
.ws
.u
.s16
.l16
= cpu_to_be16((uint16_t)buffer
->offset
);
604 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
606 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
;
607 header
.ws
.u
.s64
.l64
= cpu_to_be64(buffer
->offset
);
608 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
610 header_size
-= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK
;
612 trace_qio_channel_websock_encode(ioc
, opcode
, header_size
, buffer
->offset
);
613 buffer_reserve(output
, header_size
+ buffer
->offset
);
614 buffer_append(output
, header
.buf
, header_size
);
615 buffer_append(output
, buffer
->buffer
, buffer
->offset
);
619 static void qio_channel_websock_encode(QIOChannelWebsock
*ioc
)
621 if (!ioc
->rawoutput
.offset
) {
624 qio_channel_websock_encode_buffer(
625 ioc
, &ioc
->encoutput
, QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
,
627 buffer_reset(&ioc
->rawoutput
);
631 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*, Error
**);
634 static void qio_channel_websock_write_close(QIOChannelWebsock
*ioc
,
635 uint16_t code
, const char *reason
)
637 buffer_reserve(&ioc
->rawoutput
, 2 + (reason
? strlen(reason
) : 0));
638 *(uint16_t *)(ioc
->rawoutput
.buffer
+ ioc
->rawoutput
.offset
) =
640 ioc
->rawoutput
.offset
+= 2;
642 buffer_append(&ioc
->rawoutput
, reason
, strlen(reason
));
644 qio_channel_websock_encode_buffer(
645 ioc
, &ioc
->encoutput
, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
,
647 buffer_reset(&ioc
->rawoutput
);
648 qio_channel_websock_write_wire(ioc
, NULL
);
649 qio_channel_shutdown(ioc
->master
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
653 static int qio_channel_websock_decode_header(QIOChannelWebsock
*ioc
,
656 unsigned char opcode
, fin
, has_mask
;
659 QIOChannelWebsockHeader
*header
=
660 (QIOChannelWebsockHeader
*)ioc
->encinput
.buffer
;
662 if (ioc
->payload_remain
) {
664 "Decoding header but %zu bytes of payload remain",
665 ioc
->payload_remain
);
666 qio_channel_websock_write_close(
667 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR
,
668 "internal server error");
671 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
) {
672 /* header not complete */
673 return QIO_CHANNEL_ERR_BLOCK
;
676 fin
= header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN
;
677 opcode
= header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
;
678 has_mask
= header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK
;
679 payload_len
= header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN
;
681 /* Save or restore opcode. */
683 ioc
->opcode
= opcode
;
685 opcode
= ioc
->opcode
;
688 trace_qio_channel_websock_header_partial_decode(ioc
, payload_len
,
689 fin
, opcode
, (int)has_mask
);
691 if (opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
) {
696 /* Websocket frame sanity check:
697 * * Fragmentation is only supported for binary frames.
698 * * All frames sent by a client MUST be masked.
699 * * Only binary and ping/pong encoding is supported.
702 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
703 error_setg(errp
, "only binary websocket frames may be fragmented");
704 qio_channel_websock_write_close(
705 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_POLICY
,
706 "only binary frames may be fragmented");
710 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
&&
711 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
&&
712 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_PING
&&
713 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_PONG
) {
714 error_setg(errp
, "unsupported opcode: %#04x; only binary, close, "
715 "ping, and pong websocket frames are supported", opcode
);
716 qio_channel_websock_write_close(
717 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA
,
718 "only binary, close, ping, and pong frames are supported");
723 error_setg(errp
, "client websocket frames must be masked");
724 qio_channel_websock_write_close(
725 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
,
726 "client frames must be masked");
730 if (payload_len
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
) {
731 ioc
->payload_remain
= payload_len
;
732 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
733 ioc
->mask
= header
->u
.m
;
734 } else if (opcode
& QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK
) {
735 error_setg(errp
, "websocket control frame is too large");
736 qio_channel_websock_write_close(
737 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
,
738 "control frame is too large");
740 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
&&
741 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
) {
742 ioc
->payload_remain
= be16_to_cpu(header
->u
.s16
.l16
);
743 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
744 ioc
->mask
= header
->u
.s16
.m16
;
745 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
&&
746 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
) {
747 ioc
->payload_remain
= be64_to_cpu(header
->u
.s64
.l64
);
748 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
749 ioc
->mask
= header
->u
.s64
.m64
;
751 /* header not complete */
752 return QIO_CHANNEL_ERR_BLOCK
;
755 trace_qio_channel_websock_header_full_decode(
756 ioc
, header_size
, ioc
->payload_remain
, ioc
->mask
.u
);
757 buffer_advance(&ioc
->encinput
, header_size
);
762 static int qio_channel_websock_decode_payload(QIOChannelWebsock
*ioc
,
766 size_t payload_len
= 0;
769 if (ioc
->payload_remain
) {
770 /* If we aren't at the end of the payload, then drop
771 * off the last bytes, so we're always multiple of 4
772 * for purpose of unmasking, except at end of payload
774 if (ioc
->encinput
.offset
< ioc
->payload_remain
) {
775 /* Wait for the entire payload before processing control frames
776 * because the payload will most likely be echoed back. */
777 if (ioc
->opcode
& QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK
) {
778 return QIO_CHANNEL_ERR_BLOCK
;
780 payload_len
= ioc
->encinput
.offset
- (ioc
->encinput
.offset
% 4);
782 payload_len
= ioc
->payload_remain
;
784 if (payload_len
== 0) {
785 return QIO_CHANNEL_ERR_BLOCK
;
788 ioc
->payload_remain
-= payload_len
;
791 /* process 1 frame (32 bit op) */
792 payload32
= (uint32_t *)ioc
->encinput
.buffer
;
793 for (i
= 0; i
< payload_len
/ 4; i
++) {
794 payload32
[i
] ^= ioc
->mask
.u
;
796 /* process the remaining bytes (if any) */
797 for (i
*= 4; i
< payload_len
; i
++) {
798 ioc
->encinput
.buffer
[i
] ^= ioc
->mask
.c
[i
% 4];
802 trace_qio_channel_websock_payload_decode(
803 ioc
, ioc
->opcode
, ioc
->payload_remain
);
805 if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
807 /* binary frames are passed on */
808 buffer_reserve(&ioc
->rawinput
, payload_len
);
809 buffer_append(&ioc
->rawinput
, ioc
->encinput
.buffer
, payload_len
);
811 } else if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
) {
812 /* close frames are echoed back */
813 error_setg(errp
, "websocket closed by peer");
815 /* echo client status */
816 qio_channel_websock_encode_buffer(
817 ioc
, &ioc
->encoutput
, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
,
819 qio_channel_websock_write_wire(ioc
, NULL
);
820 qio_channel_shutdown(ioc
->master
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
822 /* send our own status */
823 qio_channel_websock_write_close(
824 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_NORMAL
, "peer requested close");
827 } else if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_PING
) {
828 /* ping frames produce an immediate reply */
829 buffer_reset(&ioc
->ping_reply
);
830 qio_channel_websock_encode_buffer(
831 ioc
, &ioc
->ping_reply
, QIO_CHANNEL_WEBSOCK_OPCODE_PONG
,
833 } /* pong frames are ignored */
836 buffer_advance(&ioc
->encinput
, payload_len
);
843 qio_channel_websock_new_server(QIOChannel
*master
)
845 QIOChannelWebsock
*wioc
;
848 wioc
= QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK
));
849 ioc
= QIO_CHANNEL(wioc
);
851 wioc
->master
= master
;
852 if (qio_channel_has_feature(master
, QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
853 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
855 object_ref(OBJECT(master
));
857 trace_qio_channel_websock_new_server(wioc
, master
);
861 void qio_channel_websock_handshake(QIOChannelWebsock
*ioc
,
864 GDestroyNotify destroy
)
868 task
= qio_task_new(OBJECT(ioc
),
873 trace_qio_channel_websock_handshake_start(ioc
);
874 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
875 qio_channel_add_watch(ioc
->master
,
877 qio_channel_websock_handshake_io
,
883 static void qio_channel_websock_finalize(Object
*obj
)
885 QIOChannelWebsock
*ioc
= QIO_CHANNEL_WEBSOCK(obj
);
887 buffer_free(&ioc
->encinput
);
888 buffer_free(&ioc
->encoutput
);
889 buffer_free(&ioc
->rawinput
);
890 buffer_free(&ioc
->rawoutput
);
891 buffer_free(&ioc
->ping_reply
);
892 object_unref(OBJECT(ioc
->master
));
894 g_source_remove(ioc
->io_tag
);
897 error_free(ioc
->io_err
);
902 static ssize_t
qio_channel_websock_read_wire(QIOChannelWebsock
*ioc
,
907 if (ioc
->encinput
.offset
< 4096) {
908 size_t want
= 4096 - ioc
->encinput
.offset
;
910 buffer_reserve(&ioc
->encinput
, want
);
911 ret
= qio_channel_read(ioc
->master
,
912 (char *)ioc
->encinput
.buffer
+
913 ioc
->encinput
.offset
,
919 if (ret
== 0 && ioc
->encinput
.offset
== 0) {
923 ioc
->encinput
.offset
+= ret
;
926 while (ioc
->encinput
.offset
!= 0) {
927 if (ioc
->payload_remain
== 0) {
928 ret
= qio_channel_websock_decode_header(ioc
, errp
);
934 ret
= qio_channel_websock_decode_payload(ioc
, errp
);
943 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*ioc
,
949 /* ping replies take priority over binary data */
950 if (!ioc
->ping_reply
.offset
) {
951 qio_channel_websock_encode(ioc
);
952 } else if (!ioc
->encoutput
.offset
) {
953 buffer_move_empty(&ioc
->encoutput
, &ioc
->ping_reply
);
956 while (ioc
->encoutput
.offset
> 0) {
957 ret
= qio_channel_write(ioc
->master
,
958 (char *)ioc
->encoutput
.buffer
,
959 ioc
->encoutput
.offset
,
962 if (ret
== QIO_CHANNEL_ERR_BLOCK
&&
969 buffer_advance(&ioc
->encoutput
, ret
);
976 static void qio_channel_websock_flush_free(gpointer user_data
)
978 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
979 object_unref(OBJECT(wioc
));
982 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
);
984 static gboolean
qio_channel_websock_flush(QIOChannel
*ioc
,
985 GIOCondition condition
,
988 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
991 if (condition
& G_IO_OUT
) {
992 ret
= qio_channel_websock_write_wire(wioc
, &wioc
->io_err
);
998 if (condition
& G_IO_IN
) {
999 ret
= qio_channel_websock_read_wire(wioc
, &wioc
->io_err
);
1006 qio_channel_websock_set_watch(wioc
);
1011 static void qio_channel_websock_unset_watch(QIOChannelWebsock
*ioc
)
1014 g_source_remove(ioc
->io_tag
);
1019 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
)
1021 GIOCondition cond
= 0;
1023 qio_channel_websock_unset_watch(ioc
);
1029 if (ioc
->encoutput
.offset
|| ioc
->ping_reply
.offset
) {
1032 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
&&
1038 object_ref(OBJECT(ioc
));
1040 qio_channel_add_watch(ioc
->master
,
1042 qio_channel_websock_flush
,
1044 qio_channel_websock_flush_free
);
1049 static ssize_t
qio_channel_websock_readv(QIOChannel
*ioc
,
1050 const struct iovec
*iov
,
1056 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1062 error_propagate(errp
, error_copy(wioc
->io_err
));
1066 if (!wioc
->rawinput
.offset
) {
1067 ret
= qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc
), errp
);
1073 for (i
= 0 ; i
< niov
; i
++) {
1074 size_t want
= iov
[i
].iov_len
;
1075 if (want
> (wioc
->rawinput
.offset
- got
)) {
1076 want
= (wioc
->rawinput
.offset
- got
);
1079 memcpy(iov
[i
].iov_base
,
1080 wioc
->rawinput
.buffer
+ got
,
1084 if (want
< iov
[i
].iov_len
) {
1089 buffer_advance(&wioc
->rawinput
, got
);
1090 qio_channel_websock_set_watch(wioc
);
1095 static ssize_t
qio_channel_websock_writev(QIOChannel
*ioc
,
1096 const struct iovec
*iov
,
1102 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1108 error_propagate(errp
, error_copy(wioc
->io_err
));
1113 error_setg(errp
, "%s", "Broken pipe");
1117 for (i
= 0; i
< niov
; i
++) {
1118 size_t want
= iov
[i
].iov_len
;
1119 if ((want
+ wioc
->rawoutput
.offset
) > QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
1120 want
= (QIO_CHANNEL_WEBSOCK_MAX_BUFFER
- wioc
->rawoutput
.offset
);
1126 buffer_reserve(&wioc
->rawoutput
, want
);
1127 buffer_append(&wioc
->rawoutput
, iov
[i
].iov_base
, want
);
1129 if (want
< iov
[i
].iov_len
) {
1135 ret
= qio_channel_websock_write_wire(wioc
, errp
);
1137 ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1138 qio_channel_websock_unset_watch(wioc
);
1142 qio_channel_websock_set_watch(wioc
);
1145 return QIO_CHANNEL_ERR_BLOCK
;
1151 static int qio_channel_websock_set_blocking(QIOChannel
*ioc
,
1155 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1157 qio_channel_set_blocking(wioc
->master
, enabled
, errp
);
1161 static void qio_channel_websock_set_delay(QIOChannel
*ioc
,
1164 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1166 qio_channel_set_delay(tioc
->master
, enabled
);
1169 static void qio_channel_websock_set_cork(QIOChannel
*ioc
,
1172 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1174 qio_channel_set_cork(tioc
->master
, enabled
);
1177 static int qio_channel_websock_shutdown(QIOChannel
*ioc
,
1178 QIOChannelShutdown how
,
1181 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1183 return qio_channel_shutdown(tioc
->master
, how
, errp
);
1186 static int qio_channel_websock_close(QIOChannel
*ioc
,
1189 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1191 trace_qio_channel_websock_close(ioc
);
1192 return qio_channel_close(wioc
->master
, errp
);
1195 typedef struct QIOChannelWebsockSource QIOChannelWebsockSource
;
1196 struct QIOChannelWebsockSource
{
1198 QIOChannelWebsock
*wioc
;
1199 GIOCondition condition
;
1203 qio_channel_websock_source_check(GSource
*source
)
1205 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1206 GIOCondition cond
= 0;
1208 if (wsource
->wioc
->rawinput
.offset
|| wsource
->wioc
->io_eof
) {
1211 if (wsource
->wioc
->rawoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
1215 return cond
& wsource
->condition
;
1219 qio_channel_websock_source_prepare(GSource
*source
,
1223 return qio_channel_websock_source_check(source
);
1227 qio_channel_websock_source_dispatch(GSource
*source
,
1228 GSourceFunc callback
,
1231 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
1232 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1234 return (*func
)(QIO_CHANNEL(wsource
->wioc
),
1235 qio_channel_websock_source_check(source
),
1240 qio_channel_websock_source_finalize(GSource
*source
)
1242 QIOChannelWebsockSource
*ssource
= (QIOChannelWebsockSource
*)source
;
1244 object_unref(OBJECT(ssource
->wioc
));
1247 GSourceFuncs qio_channel_websock_source_funcs
= {
1248 qio_channel_websock_source_prepare
,
1249 qio_channel_websock_source_check
,
1250 qio_channel_websock_source_dispatch
,
1251 qio_channel_websock_source_finalize
1254 static GSource
*qio_channel_websock_create_watch(QIOChannel
*ioc
,
1255 GIOCondition condition
)
1257 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1258 QIOChannelWebsockSource
*ssource
;
1261 source
= g_source_new(&qio_channel_websock_source_funcs
,
1262 sizeof(QIOChannelWebsockSource
));
1263 ssource
= (QIOChannelWebsockSource
*)source
;
1265 ssource
->wioc
= wioc
;
1266 object_ref(OBJECT(wioc
));
1268 ssource
->condition
= condition
;
1270 qio_channel_websock_set_watch(wioc
);
1274 static void qio_channel_websock_class_init(ObjectClass
*klass
,
1275 void *class_data G_GNUC_UNUSED
)
1277 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
1279 ioc_klass
->io_writev
= qio_channel_websock_writev
;
1280 ioc_klass
->io_readv
= qio_channel_websock_readv
;
1281 ioc_klass
->io_set_blocking
= qio_channel_websock_set_blocking
;
1282 ioc_klass
->io_set_cork
= qio_channel_websock_set_cork
;
1283 ioc_klass
->io_set_delay
= qio_channel_websock_set_delay
;
1284 ioc_klass
->io_close
= qio_channel_websock_close
;
1285 ioc_klass
->io_shutdown
= qio_channel_websock_shutdown
;
1286 ioc_klass
->io_create_watch
= qio_channel_websock_create_watch
;
1289 static const TypeInfo qio_channel_websock_info
= {
1290 .parent
= TYPE_QIO_CHANNEL
,
1291 .name
= TYPE_QIO_CHANNEL_WEBSOCK
,
1292 .instance_size
= sizeof(QIOChannelWebsock
),
1293 .instance_finalize
= qio_channel_websock_finalize
,
1294 .class_init
= qio_channel_websock_class_init
,
1297 static void qio_channel_websock_register_types(void)
1299 type_register_static(&qio_channel_websock_info
);
1302 type_init(qio_channel_websock_register_types
);