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"
32 /* Max amount to allow in rawinput/encoutput buffers */
33 #define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
35 #define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
36 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
37 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
39 #define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
40 #define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
41 #define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
42 #define QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE "upgrade"
43 #define QIO_CHANNEL_WEBSOCK_HEADER_HOST "host"
44 #define QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION "connection"
46 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
47 #define QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE "Upgrade"
48 #define QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET "websocket"
50 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
51 "Server: QEMU VNC\r\n" \
54 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK \
55 "HTTP/1.1 101 Switching Protocols\r\n" \
56 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
57 "Upgrade: websocket\r\n" \
58 "Connection: Upgrade\r\n" \
59 "Sec-WebSocket-Accept: %s\r\n" \
60 "Sec-WebSocket-Protocol: binary\r\n" \
62 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND \
63 "HTTP/1.1 404 Not Found\r\n" \
64 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
65 "Connection: close\r\n" \
67 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST \
68 "HTTP/1.1 400 Bad Request\r\n" \
69 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
70 "Connection: close\r\n" \
71 "Sec-WebSocket-Version: " \
72 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION \
74 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR \
75 "HTTP/1.1 500 Internal Server Error\r\n" \
76 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
77 "Connection: close\r\n" \
79 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE \
80 "HTTP/1.1 403 Request Entity Too Large\r\n" \
81 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
82 "Connection: close\r\n" \
84 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
85 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
86 #define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
87 #define QIO_CHANNEL_WEBSOCK_HTTP_METHOD "GET"
88 #define QIO_CHANNEL_WEBSOCK_HTTP_PATH "/"
89 #define QIO_CHANNEL_WEBSOCK_HTTP_VERSION "HTTP/1.1"
91 /* The websockets packet header is variable length
92 * depending on the size of the payload... */
94 /* ...length when using 7-bit payload length */
95 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
96 /* ...length when using 16-bit payload length */
97 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
98 /* ...length when using 64-bit payload length */
99 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
101 /* Length of the optional data mask field in header */
102 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
104 /* Maximum length that can fit in 7-bit payload size */
105 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
106 /* Maximum length that can fit in 16-bit payload size */
107 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
109 /* Magic 7-bit length to indicate use of 16-bit payload length */
110 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
111 /* Magic 7-bit length to indicate use of 64-bit payload length */
112 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
114 /* Bitmasks for accessing header fields */
115 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
116 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
117 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
118 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
119 #define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8
121 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader
;
123 struct QEMU_PACKED QIOChannelWebsockHeader
{
129 QIOChannelWebsockMask m16
;
133 QIOChannelWebsockMask m64
;
135 QIOChannelWebsockMask m
;
139 typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader
;
141 struct QIOChannelWebsockHTTPHeader
{
147 QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION
= 0x0,
148 QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME
= 0x1,
149 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
= 0x2,
150 QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
= 0x8,
151 QIO_CHANNEL_WEBSOCK_OPCODE_PING
= 0x9,
152 QIO_CHANNEL_WEBSOCK_OPCODE_PONG
= 0xA
155 static void GCC_FMT_ATTR(2, 3)
156 qio_channel_websock_handshake_send_res(QIOChannelWebsock
*ioc
,
164 va_start(vargs
, resmsg
);
165 response
= g_strdup_vprintf(resmsg
, vargs
);
166 responselen
= strlen(response
);
167 buffer_reserve(&ioc
->encoutput
, responselen
);
168 buffer_append(&ioc
->encoutput
, response
, responselen
);
172 static gchar
*qio_channel_websock_date_str(void)
175 time_t now
= time(NULL
);
180 strftime(datebuf
, sizeof(datebuf
), "%a, %d %b %Y %H:%M:%S GMT", &tm
);
182 return g_strdup(datebuf
);
185 static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock
*ioc
,
188 char *date
= qio_channel_websock_date_str();
189 qio_channel_websock_handshake_send_res(ioc
, resdata
, date
);
194 QIO_CHANNEL_WEBSOCK_STATUS_NORMAL
= 1000,
195 QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
= 1002,
196 QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA
= 1003,
197 QIO_CHANNEL_WEBSOCK_STATUS_POLICY
= 1008,
198 QIO_CHANNEL_WEBSOCK_STATUS_TOO_LARGE
= 1009,
199 QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR
= 1011,
203 qio_channel_websock_extract_headers(QIOChannelWebsock
*ioc
,
205 QIOChannelWebsockHTTPHeader
*hdrs
,
209 char *nl
, *sep
, *tmp
;
213 * First parse the HTTP protocol greeting of format:
215 * $METHOD $PATH $VERSION
222 nl
= strstr(buffer
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
224 error_setg(errp
, "Missing HTTP header delimiter");
228 trace_qio_channel_websock_http_greeting(ioc
, buffer
);
230 tmp
= strchr(buffer
, ' ');
232 error_setg(errp
, "Missing HTTP path delimiter");
237 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_METHOD
)) {
238 error_setg(errp
, "Unsupported HTTP method %s", buffer
);
243 tmp
= strchr(buffer
, ' ');
245 error_setg(errp
, "Missing HTTP version delimiter");
250 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_PATH
)) {
251 qio_channel_websock_handshake_send_res_err(
252 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND
);
253 error_setg(errp
, "Unexpected HTTP path %s", buffer
);
259 if (!g_str_equal(buffer
, QIO_CHANNEL_WEBSOCK_HTTP_VERSION
)) {
260 error_setg(errp
, "Unsupported HTTP version %s", buffer
);
264 buffer
= nl
+ strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
267 * Now parse all the header fields of format
273 * Cache-control: no-cache
276 QIOChannelWebsockHTTPHeader
*hdr
;
278 nl
= strstr(buffer
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
283 sep
= strchr(buffer
, ':');
285 error_setg(errp
, "Malformed HTTP header");
290 while (*sep
== ' ') {
294 if (nhdrs
>= nhdrsalloc
) {
295 error_setg(errp
, "Too many HTTP headers");
299 hdr
= &hdrs
[nhdrs
++];
303 /* Canonicalize header name for easier identification later */
304 for (tmp
= hdr
->name
; *tmp
; tmp
++) {
305 *tmp
= g_ascii_tolower(*tmp
);
309 buffer
= nl
+ strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM
);
311 } while (nl
!= NULL
);
316 qio_channel_websock_handshake_send_res_err(
317 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST
);
322 qio_channel_websock_find_header(QIOChannelWebsockHTTPHeader
*hdrs
,
328 for (i
= 0; i
< nhdrs
; i
++) {
329 if (g_str_equal(hdrs
[i
].name
, name
)) {
330 return hdrs
[i
].value
;
338 static void qio_channel_websock_handshake_send_res_ok(QIOChannelWebsock
*ioc
,
342 char combined_key
[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
343 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1];
347 g_strlcpy(combined_key
, key
, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+ 1);
348 g_strlcat(combined_key
, QIO_CHANNEL_WEBSOCK_GUID
,
349 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
350 QIO_CHANNEL_WEBSOCK_GUID_LEN
+ 1);
352 /* hash and encode it */
353 if (qcrypto_hash_base64(QCRYPTO_HASH_ALG_SHA1
,
355 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
+
356 QIO_CHANNEL_WEBSOCK_GUID_LEN
,
359 qio_channel_websock_handshake_send_res_err(
360 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR
);
364 date
= qio_channel_websock_date_str();
365 qio_channel_websock_handshake_send_res(
366 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK
, date
, accept
);
372 static void qio_channel_websock_handshake_process(QIOChannelWebsock
*ioc
,
376 QIOChannelWebsockHTTPHeader hdrs
[32];
377 size_t nhdrs
= G_N_ELEMENTS(hdrs
);
378 const char *protocols
= NULL
, *version
= NULL
, *key
= NULL
,
379 *host
= NULL
, *connection
= NULL
, *upgrade
= NULL
;
381 bool upgraded
= false;
384 nhdrs
= qio_channel_websock_extract_headers(ioc
, buffer
, hdrs
, nhdrs
, errp
);
389 protocols
= qio_channel_websock_find_header(
390 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL
);
392 error_setg(errp
, "Missing websocket protocol header data");
396 version
= qio_channel_websock_find_header(
397 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_VERSION
);
399 error_setg(errp
, "Missing websocket version header data");
403 key
= qio_channel_websock_find_header(
404 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_KEY
);
406 error_setg(errp
, "Missing websocket key header data");
410 host
= qio_channel_websock_find_header(
411 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_HOST
);
413 error_setg(errp
, "Missing websocket host header data");
417 connection
= qio_channel_websock_find_header(
418 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION
);
420 error_setg(errp
, "Missing websocket connection header data");
424 upgrade
= qio_channel_websock_find_header(
425 hdrs
, nhdrs
, QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE
);
427 error_setg(errp
, "Missing websocket upgrade header data");
431 trace_qio_channel_websock_http_request(ioc
, protocols
, version
,
432 host
, connection
, upgrade
, key
);
434 if (!g_strrstr(protocols
, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
)) {
435 error_setg(errp
, "No '%s' protocol is supported by client '%s'",
436 QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY
, protocols
);
440 if (!g_str_equal(version
, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
)) {
441 error_setg(errp
, "Version '%s' is not supported by client '%s'",
442 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION
, version
);
446 if (strlen(key
) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
) {
447 error_setg(errp
, "Key length '%zu' was not as expected '%d'",
448 strlen(key
), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN
);
452 connectionv
= g_strsplit(connection
, ",", 0);
453 for (i
= 0; connectionv
!= NULL
&& connectionv
[i
] != NULL
; i
++) {
454 g_strstrip(connectionv
[i
]);
455 if (strcasecmp(connectionv
[i
],
456 QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE
) == 0) {
460 g_strfreev(connectionv
);
462 error_setg(errp
, "No connection upgrade requested '%s'", connection
);
466 if (strcasecmp(upgrade
, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET
) != 0) {
467 error_setg(errp
, "Incorrect upgrade method '%s'", upgrade
);
471 qio_channel_websock_handshake_send_res_ok(ioc
, key
, errp
);
475 qio_channel_websock_handshake_send_res_err(
476 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST
);
479 static int qio_channel_websock_handshake_read(QIOChannelWebsock
*ioc
,
484 /* Typical HTTP headers from novnc are 512 bytes, so limiting
485 * total header size to 4096 is easily enough. */
486 size_t want
= 4096 - ioc
->encinput
.offset
;
487 buffer_reserve(&ioc
->encinput
, want
);
488 ret
= qio_channel_read(ioc
->master
,
489 (char *)buffer_end(&ioc
->encinput
), want
, errp
);
493 ioc
->encinput
.offset
+= ret
;
495 handshake_end
= g_strstr_len((char *)ioc
->encinput
.buffer
,
496 ioc
->encinput
.offset
,
497 QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
);
498 if (!handshake_end
) {
499 if (ioc
->encinput
.offset
>= 4096) {
500 qio_channel_websock_handshake_send_res_err(
501 ioc
, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE
);
503 "End of headers not found in first 4096 bytes");
509 *handshake_end
= '\0';
511 qio_channel_websock_handshake_process(ioc
,
512 (char *)ioc
->encinput
.buffer
,
515 buffer_advance(&ioc
->encinput
,
516 handshake_end
- (char *)ioc
->encinput
.buffer
+
517 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END
));
521 static gboolean
qio_channel_websock_handshake_send(QIOChannel
*ioc
,
522 GIOCondition condition
,
525 QIOTask
*task
= user_data
;
526 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
527 qio_task_get_source(task
));
531 ret
= qio_channel_write(wioc
->master
,
532 (char *)wioc
->encoutput
.buffer
,
533 wioc
->encoutput
.offset
,
537 trace_qio_channel_websock_handshake_fail(ioc
, error_get_pretty(err
));
538 qio_task_set_error(task
, err
);
539 qio_task_complete(task
);
543 buffer_advance(&wioc
->encoutput
, ret
);
544 if (wioc
->encoutput
.offset
== 0) {
546 trace_qio_channel_websock_handshake_fail(
547 ioc
, error_get_pretty(wioc
->io_err
));
548 qio_task_set_error(task
, wioc
->io_err
);
550 qio_task_complete(task
);
552 trace_qio_channel_websock_handshake_complete(ioc
);
553 qio_task_complete(task
);
557 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_OUT
);
561 static gboolean
qio_channel_websock_handshake_io(QIOChannel
*ioc
,
562 GIOCondition condition
,
565 QIOTask
*task
= user_data
;
566 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(
567 qio_task_get_source(task
));
571 ret
= qio_channel_websock_handshake_read(wioc
, &err
);
574 * We only take this path on a fatal I/O error reading from
575 * client connection, as most of the time we have an
576 * HTTP 4xx err response to send instead
578 trace_qio_channel_websock_handshake_fail(ioc
, error_get_pretty(err
));
579 qio_task_set_error(task
, err
);
580 qio_task_complete(task
);
584 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
585 /* need more data still */
590 error_propagate(&wioc
->io_err
, err
);
593 trace_qio_channel_websock_handshake_reply(ioc
);
594 qio_channel_add_watch(
597 qio_channel_websock_handshake_send
,
604 static void qio_channel_websock_encode(QIOChannelWebsock
*ioc
,
606 const struct iovec
*iov
,
613 char buf
[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
];
614 QIOChannelWebsockHeader ws
;
617 assert(size
<= iov_size(iov
, niov
));
619 header
.ws
.b0
= QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN
|
620 (opcode
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
);
621 if (size
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT
) {
622 header
.ws
.b1
= (uint8_t)size
;
623 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
624 } else if (size
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT
) {
625 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
;
626 header
.ws
.u
.s16
.l16
= cpu_to_be16((uint16_t)size
);
627 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
629 header
.ws
.b1
= QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
;
630 header
.ws
.u
.s64
.l64
= cpu_to_be64(size
);
631 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
633 header_size
-= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK
;
635 trace_qio_channel_websock_encode(ioc
, opcode
, header_size
, size
);
636 buffer_reserve(&ioc
->encoutput
, header_size
+ size
);
637 buffer_append(&ioc
->encoutput
, header
.buf
, header_size
);
638 for (i
= 0; i
< niov
&& size
!= 0; i
++) {
639 size_t want
= iov
[i
].iov_len
;
643 buffer_append(&ioc
->encoutput
, iov
[i
].iov_base
, want
);
649 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*, Error
**);
652 static void qio_channel_websock_write_close(QIOChannelWebsock
*ioc
,
653 uint16_t code
, const char *reason
)
655 struct iovec iov
[2] = {
656 { .iov_base
= &code
, .iov_len
= sizeof(code
) },
659 size_t size
= iov
[0].iov_len
;
664 iov
[1].iov_base
= (void *)reason
;
665 iov
[1].iov_len
= strlen(reason
);
666 size
+= iov
[1].iov_len
;
669 qio_channel_websock_encode(ioc
, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
,
671 qio_channel_websock_write_wire(ioc
, NULL
);
672 qio_channel_shutdown(ioc
->master
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
676 static int qio_channel_websock_decode_header(QIOChannelWebsock
*ioc
,
679 unsigned char opcode
, fin
, has_mask
;
682 QIOChannelWebsockHeader
*header
=
683 (QIOChannelWebsockHeader
*)ioc
->encinput
.buffer
;
685 if (ioc
->payload_remain
) {
687 "Decoding header but %zu bytes of payload remain",
688 ioc
->payload_remain
);
689 qio_channel_websock_write_close(
690 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR
,
691 "internal server error");
694 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
) {
695 /* header not complete */
696 return QIO_CHANNEL_ERR_BLOCK
;
699 fin
= header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN
;
700 opcode
= header
->b0
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE
;
701 has_mask
= header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK
;
702 payload_len
= header
->b1
& QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN
;
704 /* Save or restore opcode. */
706 ioc
->opcode
= opcode
;
708 opcode
= ioc
->opcode
;
711 trace_qio_channel_websock_header_partial_decode(ioc
, payload_len
,
712 fin
, opcode
, (int)has_mask
);
714 if (opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
) {
719 /* Websocket frame sanity check:
720 * * Fragmentation is only supported for binary frames.
721 * * All frames sent by a client MUST be masked.
722 * * Only binary and ping/pong encoding is supported.
725 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
726 error_setg(errp
, "only binary websocket frames may be fragmented");
727 qio_channel_websock_write_close(
728 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_POLICY
,
729 "only binary frames may be fragmented");
733 if (opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
&&
734 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
&&
735 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_PING
&&
736 opcode
!= QIO_CHANNEL_WEBSOCK_OPCODE_PONG
) {
737 error_setg(errp
, "unsupported opcode: %#04x; only binary, close, "
738 "ping, and pong websocket frames are supported", opcode
);
739 qio_channel_websock_write_close(
740 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA
,
741 "only binary, close, ping, and pong frames are supported");
746 error_setg(errp
, "client websocket frames must be masked");
747 qio_channel_websock_write_close(
748 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
,
749 "client frames must be masked");
753 if (payload_len
< QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
) {
754 ioc
->payload_remain
= payload_len
;
755 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT
;
756 ioc
->mask
= header
->u
.m
;
757 } else if (opcode
& QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK
) {
758 error_setg(errp
, "websocket control frame is too large");
759 qio_channel_websock_write_close(
760 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR
,
761 "control frame is too large");
763 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT
&&
764 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
) {
765 ioc
->payload_remain
= be16_to_cpu(header
->u
.s16
.l16
);
766 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT
;
767 ioc
->mask
= header
->u
.s16
.m16
;
768 } else if (payload_len
== QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT
&&
769 ioc
->encinput
.offset
>= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
) {
770 ioc
->payload_remain
= be64_to_cpu(header
->u
.s64
.l64
);
771 header_size
= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT
;
772 ioc
->mask
= header
->u
.s64
.m64
;
774 /* header not complete */
775 return QIO_CHANNEL_ERR_BLOCK
;
778 trace_qio_channel_websock_header_full_decode(
779 ioc
, header_size
, ioc
->payload_remain
, ioc
->mask
.u
);
780 buffer_advance(&ioc
->encinput
, header_size
);
785 static int qio_channel_websock_decode_payload(QIOChannelWebsock
*ioc
,
789 size_t payload_len
= 0;
792 if (ioc
->payload_remain
) {
793 /* If we aren't at the end of the payload, then drop
794 * off the last bytes, so we're always multiple of 4
795 * for purpose of unmasking, except at end of payload
797 if (ioc
->encinput
.offset
< ioc
->payload_remain
) {
798 /* Wait for the entire payload before processing control frames
799 * because the payload will most likely be echoed back. */
800 if (ioc
->opcode
& QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK
) {
801 return QIO_CHANNEL_ERR_BLOCK
;
803 payload_len
= ioc
->encinput
.offset
- (ioc
->encinput
.offset
% 4);
805 payload_len
= ioc
->payload_remain
;
807 if (payload_len
== 0) {
808 return QIO_CHANNEL_ERR_BLOCK
;
811 ioc
->payload_remain
-= payload_len
;
814 /* process 1 frame (32 bit op) */
815 payload32
= (uint32_t *)ioc
->encinput
.buffer
;
816 for (i
= 0; i
< payload_len
/ 4; i
++) {
817 payload32
[i
] ^= ioc
->mask
.u
;
819 /* process the remaining bytes (if any) */
820 for (i
*= 4; i
< payload_len
; i
++) {
821 ioc
->encinput
.buffer
[i
] ^= ioc
->mask
.c
[i
% 4];
825 trace_qio_channel_websock_payload_decode(
826 ioc
, ioc
->opcode
, ioc
->payload_remain
);
828 if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
) {
830 /* binary frames are passed on */
831 buffer_reserve(&ioc
->rawinput
, payload_len
);
832 buffer_append(&ioc
->rawinput
, ioc
->encinput
.buffer
, payload_len
);
834 } else if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
) {
835 /* close frames are echoed back */
836 error_setg(errp
, "websocket closed by peer");
838 /* echo client status */
839 struct iovec iov
= { .iov_base
= ioc
->encinput
.buffer
,
840 .iov_len
= ioc
->encinput
.offset
};
841 qio_channel_websock_encode(ioc
, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE
,
842 &iov
, 1, iov
.iov_len
);
843 qio_channel_websock_write_wire(ioc
, NULL
);
844 qio_channel_shutdown(ioc
->master
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
846 /* send our own status */
847 qio_channel_websock_write_close(
848 ioc
, QIO_CHANNEL_WEBSOCK_STATUS_NORMAL
, "peer requested close");
851 } else if (ioc
->opcode
== QIO_CHANNEL_WEBSOCK_OPCODE_PING
) {
852 /* ping frames produce an immediate reply, as long as we've not still
853 * got a previous pong queued, in which case we drop the new pong */
854 if (ioc
->pong_remain
== 0) {
855 struct iovec iov
= { .iov_base
= ioc
->encinput
.buffer
,
856 .iov_len
= ioc
->encinput
.offset
};
857 qio_channel_websock_encode(ioc
, QIO_CHANNEL_WEBSOCK_OPCODE_PONG
,
858 &iov
, 1, iov
.iov_len
);
859 ioc
->pong_remain
= ioc
->encoutput
.offset
;
861 } /* pong frames are ignored */
864 buffer_advance(&ioc
->encinput
, payload_len
);
871 qio_channel_websock_new_server(QIOChannel
*master
)
873 QIOChannelWebsock
*wioc
;
876 wioc
= QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK
));
877 ioc
= QIO_CHANNEL(wioc
);
879 wioc
->master
= master
;
880 if (qio_channel_has_feature(master
, QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
881 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
883 object_ref(OBJECT(master
));
885 trace_qio_channel_websock_new_server(wioc
, master
);
889 void qio_channel_websock_handshake(QIOChannelWebsock
*ioc
,
892 GDestroyNotify destroy
)
896 task
= qio_task_new(OBJECT(ioc
),
901 trace_qio_channel_websock_handshake_start(ioc
);
902 trace_qio_channel_websock_handshake_pending(ioc
, G_IO_IN
);
903 qio_channel_add_watch(ioc
->master
,
905 qio_channel_websock_handshake_io
,
911 static void qio_channel_websock_finalize(Object
*obj
)
913 QIOChannelWebsock
*ioc
= QIO_CHANNEL_WEBSOCK(obj
);
915 buffer_free(&ioc
->encinput
);
916 buffer_free(&ioc
->encoutput
);
917 buffer_free(&ioc
->rawinput
);
918 object_unref(OBJECT(ioc
->master
));
920 g_source_remove(ioc
->io_tag
);
923 error_free(ioc
->io_err
);
928 static ssize_t
qio_channel_websock_read_wire(QIOChannelWebsock
*ioc
,
933 if (ioc
->encinput
.offset
< 4096) {
934 size_t want
= 4096 - ioc
->encinput
.offset
;
936 buffer_reserve(&ioc
->encinput
, want
);
937 ret
= qio_channel_read(ioc
->master
,
938 (char *)ioc
->encinput
.buffer
+
939 ioc
->encinput
.offset
,
945 if (ret
== 0 && ioc
->encinput
.offset
== 0) {
949 ioc
->encinput
.offset
+= ret
;
952 while (ioc
->encinput
.offset
!= 0) {
953 if (ioc
->payload_remain
== 0) {
954 ret
= qio_channel_websock_decode_header(ioc
, errp
);
960 ret
= qio_channel_websock_decode_payload(ioc
, errp
);
969 static ssize_t
qio_channel_websock_write_wire(QIOChannelWebsock
*ioc
,
975 while (ioc
->encoutput
.offset
> 0) {
976 ret
= qio_channel_write(ioc
->master
,
977 (char *)ioc
->encoutput
.buffer
,
978 ioc
->encoutput
.offset
,
981 if (ret
== QIO_CHANNEL_ERR_BLOCK
&&
988 buffer_advance(&ioc
->encoutput
, ret
);
990 if (ioc
->pong_remain
< ret
) {
991 ioc
->pong_remain
= 0;
993 ioc
->pong_remain
-= ret
;
1000 static void qio_channel_websock_flush_free(gpointer user_data
)
1002 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
1003 object_unref(OBJECT(wioc
));
1006 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
);
1008 static gboolean
qio_channel_websock_flush(QIOChannel
*ioc
,
1009 GIOCondition condition
,
1012 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(user_data
);
1015 if (condition
& G_IO_OUT
) {
1016 ret
= qio_channel_websock_write_wire(wioc
, &wioc
->io_err
);
1022 if (condition
& G_IO_IN
) {
1023 ret
= qio_channel_websock_read_wire(wioc
, &wioc
->io_err
);
1030 qio_channel_websock_set_watch(wioc
);
1035 static void qio_channel_websock_unset_watch(QIOChannelWebsock
*ioc
)
1038 g_source_remove(ioc
->io_tag
);
1043 static void qio_channel_websock_set_watch(QIOChannelWebsock
*ioc
)
1045 GIOCondition cond
= 0;
1047 qio_channel_websock_unset_watch(ioc
);
1053 if (ioc
->encoutput
.offset
) {
1056 if (ioc
->encinput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
&&
1062 object_ref(OBJECT(ioc
));
1064 qio_channel_add_watch(ioc
->master
,
1066 qio_channel_websock_flush
,
1068 qio_channel_websock_flush_free
);
1073 static ssize_t
qio_channel_websock_readv(QIOChannel
*ioc
,
1074 const struct iovec
*iov
,
1080 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1086 error_propagate(errp
, error_copy(wioc
->io_err
));
1090 if (!wioc
->rawinput
.offset
) {
1091 ret
= qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc
), errp
);
1097 for (i
= 0 ; i
< niov
; i
++) {
1098 size_t want
= iov
[i
].iov_len
;
1099 if (want
> (wioc
->rawinput
.offset
- got
)) {
1100 want
= (wioc
->rawinput
.offset
- got
);
1103 memcpy(iov
[i
].iov_base
,
1104 wioc
->rawinput
.buffer
+ got
,
1108 if (want
< iov
[i
].iov_len
) {
1113 buffer_advance(&wioc
->rawinput
, got
);
1114 qio_channel_websock_set_watch(wioc
);
1119 static ssize_t
qio_channel_websock_writev(QIOChannel
*ioc
,
1120 const struct iovec
*iov
,
1126 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1127 ssize_t want
= iov_size(iov
, niov
);
1132 error_propagate(errp
, error_copy(wioc
->io_err
));
1137 error_setg(errp
, "%s", "Broken pipe");
1141 avail
= wioc
->encoutput
.offset
>= QIO_CHANNEL_WEBSOCK_MAX_BUFFER
?
1142 0 : (QIO_CHANNEL_WEBSOCK_MAX_BUFFER
- wioc
->encoutput
.offset
);
1148 qio_channel_websock_encode(wioc
,
1149 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME
,
1153 /* Even if want == 0, we'll try write_wire in case there's
1154 * pending data we could usefully flush out
1156 ret
= qio_channel_websock_write_wire(wioc
, errp
);
1158 ret
!= QIO_CHANNEL_ERR_BLOCK
) {
1159 qio_channel_websock_unset_watch(wioc
);
1163 qio_channel_websock_set_watch(wioc
);
1166 return QIO_CHANNEL_ERR_BLOCK
;
1172 static int qio_channel_websock_set_blocking(QIOChannel
*ioc
,
1176 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1178 qio_channel_set_blocking(wioc
->master
, enabled
, errp
);
1182 static void qio_channel_websock_set_delay(QIOChannel
*ioc
,
1185 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1187 qio_channel_set_delay(tioc
->master
, enabled
);
1190 static void qio_channel_websock_set_cork(QIOChannel
*ioc
,
1193 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1195 qio_channel_set_cork(tioc
->master
, enabled
);
1198 static int qio_channel_websock_shutdown(QIOChannel
*ioc
,
1199 QIOChannelShutdown how
,
1202 QIOChannelWebsock
*tioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1204 return qio_channel_shutdown(tioc
->master
, how
, errp
);
1207 static int qio_channel_websock_close(QIOChannel
*ioc
,
1210 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1212 trace_qio_channel_websock_close(ioc
);
1213 return qio_channel_close(wioc
->master
, errp
);
1216 typedef struct QIOChannelWebsockSource QIOChannelWebsockSource
;
1217 struct QIOChannelWebsockSource
{
1219 QIOChannelWebsock
*wioc
;
1220 GIOCondition condition
;
1224 qio_channel_websock_source_check(GSource
*source
)
1226 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1227 GIOCondition cond
= 0;
1229 if (wsource
->wioc
->rawinput
.offset
|| wsource
->wioc
->io_eof
) {
1232 if (wsource
->wioc
->encoutput
.offset
< QIO_CHANNEL_WEBSOCK_MAX_BUFFER
) {
1236 return cond
& wsource
->condition
;
1240 qio_channel_websock_source_prepare(GSource
*source
,
1244 return qio_channel_websock_source_check(source
);
1248 qio_channel_websock_source_dispatch(GSource
*source
,
1249 GSourceFunc callback
,
1252 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
1253 QIOChannelWebsockSource
*wsource
= (QIOChannelWebsockSource
*)source
;
1255 return (*func
)(QIO_CHANNEL(wsource
->wioc
),
1256 qio_channel_websock_source_check(source
),
1261 qio_channel_websock_source_finalize(GSource
*source
)
1263 QIOChannelWebsockSource
*ssource
= (QIOChannelWebsockSource
*)source
;
1265 object_unref(OBJECT(ssource
->wioc
));
1268 GSourceFuncs qio_channel_websock_source_funcs
= {
1269 qio_channel_websock_source_prepare
,
1270 qio_channel_websock_source_check
,
1271 qio_channel_websock_source_dispatch
,
1272 qio_channel_websock_source_finalize
1275 static GSource
*qio_channel_websock_create_watch(QIOChannel
*ioc
,
1276 GIOCondition condition
)
1278 QIOChannelWebsock
*wioc
= QIO_CHANNEL_WEBSOCK(ioc
);
1279 QIOChannelWebsockSource
*ssource
;
1282 source
= g_source_new(&qio_channel_websock_source_funcs
,
1283 sizeof(QIOChannelWebsockSource
));
1284 ssource
= (QIOChannelWebsockSource
*)source
;
1286 ssource
->wioc
= wioc
;
1287 object_ref(OBJECT(wioc
));
1289 ssource
->condition
= condition
;
1291 qio_channel_websock_set_watch(wioc
);
1295 static void qio_channel_websock_class_init(ObjectClass
*klass
,
1296 void *class_data G_GNUC_UNUSED
)
1298 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
1300 ioc_klass
->io_writev
= qio_channel_websock_writev
;
1301 ioc_klass
->io_readv
= qio_channel_websock_readv
;
1302 ioc_klass
->io_set_blocking
= qio_channel_websock_set_blocking
;
1303 ioc_klass
->io_set_cork
= qio_channel_websock_set_cork
;
1304 ioc_klass
->io_set_delay
= qio_channel_websock_set_delay
;
1305 ioc_klass
->io_close
= qio_channel_websock_close
;
1306 ioc_klass
->io_shutdown
= qio_channel_websock_shutdown
;
1307 ioc_klass
->io_create_watch
= qio_channel_websock_create_watch
;
1310 static const TypeInfo qio_channel_websock_info
= {
1311 .parent
= TYPE_QIO_CHANNEL
,
1312 .name
= TYPE_QIO_CHANNEL_WEBSOCK
,
1313 .instance_size
= sizeof(QIOChannelWebsock
),
1314 .instance_finalize
= qio_channel_websock_finalize
,
1315 .class_init
= qio_channel_websock_class_init
,
1318 static void qio_channel_websock_register_types(void)
1320 type_register_static(&qio_channel_websock_info
);
1323 type_init(qio_channel_websock_register_types
);