libqos: Track QTestState with QPCIBus
[qemu.git] / io / channel-websock.c
blob7fd6bb68ba3f4c05f5f90fb69a9b6bedb72e4eba
1 /*
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"
26 #include "trace.h"
27 #include "qemu/iov.h"
29 /* Max amount to allow in rawinput/encoutput buffers */
30 #define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
32 #define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
33 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
34 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
36 #define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
37 #define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
38 #define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
39 #define QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE "upgrade"
40 #define QIO_CHANNEL_WEBSOCK_HEADER_HOST "host"
41 #define QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION "connection"
43 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
44 #define QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE "Upgrade"
45 #define QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET "websocket"
47 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
48 "Server: QEMU VNC\r\n" \
49 "Date: %s\r\n"
51 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK \
52 "HTTP/1.1 101 Switching Protocols\r\n" \
53 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
54 "Upgrade: websocket\r\n" \
55 "Connection: Upgrade\r\n" \
56 "Sec-WebSocket-Accept: %s\r\n" \
57 "Sec-WebSocket-Protocol: binary\r\n" \
58 "\r\n"
59 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND \
60 "HTTP/1.1 404 Not Found\r\n" \
61 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
62 "Connection: close\r\n" \
63 "\r\n"
64 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST \
65 "HTTP/1.1 400 Bad Request\r\n" \
66 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
67 "Connection: close\r\n" \
68 "Sec-WebSocket-Version: " \
69 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION \
70 "\r\n"
71 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR \
72 "HTTP/1.1 500 Internal Server Error\r\n" \
73 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
74 "Connection: close\r\n" \
75 "\r\n"
76 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE \
77 "HTTP/1.1 403 Request Entity Too Large\r\n" \
78 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
79 "Connection: close\r\n" \
80 "\r\n"
81 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
82 #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
83 #define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
84 #define QIO_CHANNEL_WEBSOCK_HTTP_METHOD "GET"
85 #define QIO_CHANNEL_WEBSOCK_HTTP_PATH "/"
86 #define QIO_CHANNEL_WEBSOCK_HTTP_VERSION "HTTP/1.1"
88 /* The websockets packet header is variable length
89 * depending on the size of the payload... */
91 /* ...length when using 7-bit payload length */
92 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
93 /* ...length when using 16-bit payload length */
94 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
95 /* ...length when using 64-bit payload length */
96 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
98 /* Length of the optional data mask field in header */
99 #define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
101 /* Maximum length that can fit in 7-bit payload size */
102 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
103 /* Maximum length that can fit in 16-bit payload size */
104 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
106 /* Magic 7-bit length to indicate use of 16-bit payload length */
107 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
108 /* Magic 7-bit length to indicate use of 64-bit payload length */
109 #define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
111 /* Bitmasks for accessing header fields */
112 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
113 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
114 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
115 #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
116 #define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8
118 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader;
120 struct QEMU_PACKED QIOChannelWebsockHeader {
121 unsigned char b0;
122 unsigned char b1;
123 union {
124 struct QEMU_PACKED {
125 uint16_t l16;
126 QIOChannelWebsockMask m16;
127 } s16;
128 struct QEMU_PACKED {
129 uint64_t l64;
130 QIOChannelWebsockMask m64;
131 } s64;
132 QIOChannelWebsockMask m;
133 } u;
136 typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader;
138 struct QIOChannelWebsockHTTPHeader {
139 char *name;
140 char *value;
143 enum {
144 QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION = 0x0,
145 QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME = 0x1,
146 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME = 0x2,
147 QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE = 0x8,
148 QIO_CHANNEL_WEBSOCK_OPCODE_PING = 0x9,
149 QIO_CHANNEL_WEBSOCK_OPCODE_PONG = 0xA
152 static void GCC_FMT_ATTR(2, 3)
153 qio_channel_websock_handshake_send_res(QIOChannelWebsock *ioc,
154 const char *resmsg,
155 ...)
157 va_list vargs;
158 char *response;
159 size_t responselen;
161 va_start(vargs, resmsg);
162 response = g_strdup_vprintf(resmsg, vargs);
163 responselen = strlen(response);
164 buffer_reserve(&ioc->encoutput, responselen);
165 buffer_append(&ioc->encoutput, response, responselen);
166 va_end(vargs);
169 static gchar *qio_channel_websock_date_str(void)
171 struct tm tm;
172 time_t now = time(NULL);
173 char datebuf[128];
175 gmtime_r(&now, &tm);
177 strftime(datebuf, sizeof(datebuf), "%a, %d %b %Y %H:%M:%S GMT", &tm);
179 return g_strdup(datebuf);
182 static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock *ioc,
183 const char *resdata)
185 char *date = qio_channel_websock_date_str();
186 qio_channel_websock_handshake_send_res(ioc, resdata, date);
187 g_free(date);
190 enum {
191 QIO_CHANNEL_WEBSOCK_STATUS_NORMAL = 1000,
192 QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR = 1002,
193 QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA = 1003,
194 QIO_CHANNEL_WEBSOCK_STATUS_POLICY = 1008,
195 QIO_CHANNEL_WEBSOCK_STATUS_TOO_LARGE = 1009,
196 QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR = 1011,
199 static size_t
200 qio_channel_websock_extract_headers(QIOChannelWebsock *ioc,
201 char *buffer,
202 QIOChannelWebsockHTTPHeader *hdrs,
203 size_t nhdrsalloc,
204 Error **errp)
206 char *nl, *sep, *tmp;
207 size_t nhdrs = 0;
210 * First parse the HTTP protocol greeting of format:
212 * $METHOD $PATH $VERSION
214 * e.g.
216 * GET / HTTP/1.1
219 nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
220 if (!nl) {
221 error_setg(errp, "Missing HTTP header delimiter");
222 goto bad_request;
224 *nl = '\0';
225 trace_qio_channel_websock_http_greeting(ioc, buffer);
227 tmp = strchr(buffer, ' ');
228 if (!tmp) {
229 error_setg(errp, "Missing HTTP path delimiter");
230 return 0;
232 *tmp = '\0';
234 if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_METHOD)) {
235 error_setg(errp, "Unsupported HTTP method %s", buffer);
236 goto bad_request;
239 buffer = tmp + 1;
240 tmp = strchr(buffer, ' ');
241 if (!tmp) {
242 error_setg(errp, "Missing HTTP version delimiter");
243 goto bad_request;
245 *tmp = '\0';
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);
251 return 0;
254 buffer = tmp + 1;
256 if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_VERSION)) {
257 error_setg(errp, "Unsupported HTTP version %s", buffer);
258 goto bad_request;
261 buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
264 * Now parse all the header fields of format
266 * $NAME: $VALUE
268 * e.g.
270 * Cache-control: no-cache
272 do {
273 QIOChannelWebsockHTTPHeader *hdr;
275 nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
276 if (nl) {
277 *nl = '\0';
280 sep = strchr(buffer, ':');
281 if (!sep) {
282 error_setg(errp, "Malformed HTTP header");
283 goto bad_request;
285 *sep = '\0';
286 sep++;
287 while (*sep == ' ') {
288 sep++;
291 if (nhdrs >= nhdrsalloc) {
292 error_setg(errp, "Too many HTTP headers");
293 goto bad_request;
296 hdr = &hdrs[nhdrs++];
297 hdr->name = buffer;
298 hdr->value = sep;
300 /* Canonicalize header name for easier identification later */
301 for (tmp = hdr->name; *tmp; tmp++) {
302 *tmp = g_ascii_tolower(*tmp);
305 if (nl) {
306 buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
308 } while (nl != NULL);
310 return nhdrs;
312 bad_request:
313 qio_channel_websock_handshake_send_res_err(
314 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST);
315 return 0;
318 static const char *
319 qio_channel_websock_find_header(QIOChannelWebsockHTTPHeader *hdrs,
320 size_t nhdrs,
321 const char *name)
323 size_t i;
325 for (i = 0; i < nhdrs; i++) {
326 if (g_str_equal(hdrs[i].name, name)) {
327 return hdrs[i].value;
331 return NULL;
335 static void qio_channel_websock_handshake_send_res_ok(QIOChannelWebsock *ioc,
336 const char *key,
337 Error **errp)
339 char combined_key[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
340 QIO_CHANNEL_WEBSOCK_GUID_LEN + 1];
341 char *accept = NULL;
342 char *date = NULL;
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,
351 combined_key,
352 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
353 QIO_CHANNEL_WEBSOCK_GUID_LEN,
354 &accept,
355 errp) < 0) {
356 qio_channel_websock_handshake_send_res_err(
357 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR);
358 return;
361 date = qio_channel_websock_date_str();
362 qio_channel_websock_handshake_send_res(
363 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK, date, accept);
365 g_free(date);
366 g_free(accept);
369 static void qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
370 char *buffer,
371 Error **errp)
373 QIOChannelWebsockHTTPHeader hdrs[32];
374 size_t nhdrs = G_N_ELEMENTS(hdrs);
375 const char *protocols = NULL, *version = NULL, *key = NULL,
376 *host = NULL, *connection = NULL, *upgrade = NULL;
377 char **connectionv;
378 bool upgraded = false;
379 size_t i;
381 nhdrs = qio_channel_websock_extract_headers(ioc, buffer, hdrs, nhdrs, errp);
382 if (!nhdrs) {
383 return;
386 protocols = qio_channel_websock_find_header(
387 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL);
388 if (!protocols) {
389 error_setg(errp, "Missing websocket protocol header data");
390 goto bad_request;
393 version = qio_channel_websock_find_header(
394 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_VERSION);
395 if (!version) {
396 error_setg(errp, "Missing websocket version header data");
397 goto bad_request;
400 key = qio_channel_websock_find_header(
401 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_KEY);
402 if (!key) {
403 error_setg(errp, "Missing websocket key header data");
404 goto bad_request;
407 host = qio_channel_websock_find_header(
408 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_HOST);
409 if (!host) {
410 error_setg(errp, "Missing websocket host header data");
411 goto bad_request;
414 connection = qio_channel_websock_find_header(
415 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION);
416 if (!connection) {
417 error_setg(errp, "Missing websocket connection header data");
418 goto bad_request;
421 upgrade = qio_channel_websock_find_header(
422 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE);
423 if (!upgrade) {
424 error_setg(errp, "Missing websocket upgrade header data");
425 goto bad_request;
428 trace_qio_channel_websock_http_request(ioc, protocols, version,
429 host, connection, upgrade, key);
431 if (!g_strrstr(protocols, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY)) {
432 error_setg(errp, "No '%s' protocol is supported by client '%s'",
433 QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY, protocols);
434 goto bad_request;
437 if (!g_str_equal(version, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION)) {
438 error_setg(errp, "Version '%s' is not supported by client '%s'",
439 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION, version);
440 goto bad_request;
443 if (strlen(key) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN) {
444 error_setg(errp, "Key length '%zu' was not as expected '%d'",
445 strlen(key), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN);
446 goto bad_request;
449 connectionv = g_strsplit(connection, ",", 0);
450 for (i = 0; connectionv != NULL && connectionv[i] != NULL; i++) {
451 g_strstrip(connectionv[i]);
452 if (strcasecmp(connectionv[i],
453 QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE) == 0) {
454 upgraded = true;
457 g_strfreev(connectionv);
458 if (!upgraded) {
459 error_setg(errp, "No connection upgrade requested '%s'", connection);
460 goto bad_request;
463 if (strcasecmp(upgrade, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET) != 0) {
464 error_setg(errp, "Incorrect upgrade method '%s'", upgrade);
465 goto bad_request;
468 qio_channel_websock_handshake_send_res_ok(ioc, key, errp);
469 return;
471 bad_request:
472 qio_channel_websock_handshake_send_res_err(
473 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST);
476 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
477 Error **errp)
479 char *handshake_end;
480 ssize_t ret;
481 /* Typical HTTP headers from novnc are 512 bytes, so limiting
482 * total header size to 4096 is easily enough. */
483 size_t want = 4096 - ioc->encinput.offset;
484 buffer_reserve(&ioc->encinput, want);
485 ret = qio_channel_read(ioc->master,
486 (char *)buffer_end(&ioc->encinput), want, errp);
487 if (ret < 0) {
488 return -1;
490 ioc->encinput.offset += ret;
492 handshake_end = g_strstr_len((char *)ioc->encinput.buffer,
493 ioc->encinput.offset,
494 QIO_CHANNEL_WEBSOCK_HANDSHAKE_END);
495 if (!handshake_end) {
496 if (ioc->encinput.offset >= 4096) {
497 qio_channel_websock_handshake_send_res_err(
498 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE);
499 error_setg(errp,
500 "End of headers not found in first 4096 bytes");
501 return 1;
502 } else {
503 return 0;
506 *handshake_end = '\0';
508 qio_channel_websock_handshake_process(ioc,
509 (char *)ioc->encinput.buffer,
510 errp);
512 buffer_advance(&ioc->encinput,
513 handshake_end - (char *)ioc->encinput.buffer +
514 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END));
515 return 1;
518 static gboolean qio_channel_websock_handshake_send(QIOChannel *ioc,
519 GIOCondition condition,
520 gpointer user_data)
522 QIOTask *task = user_data;
523 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(
524 qio_task_get_source(task));
525 Error *err = NULL;
526 ssize_t ret;
528 ret = qio_channel_write(wioc->master,
529 (char *)wioc->encoutput.buffer,
530 wioc->encoutput.offset,
531 &err);
533 if (ret < 0) {
534 trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
535 qio_task_set_error(task, err);
536 qio_task_complete(task);
537 return FALSE;
540 buffer_advance(&wioc->encoutput, ret);
541 if (wioc->encoutput.offset == 0) {
542 if (wioc->io_err) {
543 trace_qio_channel_websock_handshake_fail(
544 ioc, error_get_pretty(wioc->io_err));
545 qio_task_set_error(task, wioc->io_err);
546 wioc->io_err = NULL;
547 qio_task_complete(task);
548 } else {
549 trace_qio_channel_websock_handshake_complete(ioc);
550 qio_task_complete(task);
552 return FALSE;
554 trace_qio_channel_websock_handshake_pending(ioc, G_IO_OUT);
555 return TRUE;
558 static gboolean qio_channel_websock_handshake_io(QIOChannel *ioc,
559 GIOCondition condition,
560 gpointer user_data)
562 QIOTask *task = user_data;
563 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(
564 qio_task_get_source(task));
565 Error *err = NULL;
566 int ret;
568 ret = qio_channel_websock_handshake_read(wioc, &err);
569 if (ret < 0) {
571 * We only take this path on a fatal I/O error reading from
572 * client connection, as most of the time we have an
573 * HTTP 4xx err response to send instead
575 trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
576 qio_task_set_error(task, err);
577 qio_task_complete(task);
578 return FALSE;
580 if (ret == 0) {
581 trace_qio_channel_websock_handshake_pending(ioc, G_IO_IN);
582 /* need more data still */
583 return TRUE;
586 if (err) {
587 error_propagate(&wioc->io_err, err);
590 trace_qio_channel_websock_handshake_reply(ioc);
591 qio_channel_add_watch(
592 wioc->master,
593 G_IO_OUT,
594 qio_channel_websock_handshake_send,
595 task,
596 NULL);
597 return FALSE;
601 static void qio_channel_websock_encode(QIOChannelWebsock *ioc,
602 uint8_t opcode,
603 const struct iovec *iov,
604 size_t niov,
605 size_t size)
607 size_t header_size;
608 size_t i;
609 union {
610 char buf[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT];
611 QIOChannelWebsockHeader ws;
612 } header;
614 assert(size <= iov_size(iov, niov));
616 header.ws.b0 = QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN |
617 (opcode & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE);
618 if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) {
619 header.ws.b1 = (uint8_t)size;
620 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;
621 } else if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) {
622 header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT;
623 header.ws.u.s16.l16 = cpu_to_be16((uint16_t)size);
624 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;
625 } else {
626 header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT;
627 header.ws.u.s64.l64 = cpu_to_be64(size);
628 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;
630 header_size -= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK;
632 trace_qio_channel_websock_encode(ioc, opcode, header_size, size);
633 buffer_reserve(&ioc->encoutput, header_size + size);
634 buffer_append(&ioc->encoutput, header.buf, header_size);
635 for (i = 0; i < niov && size != 0; i++) {
636 size_t want = iov[i].iov_len;
637 if (want > size) {
638 want = size;
640 buffer_append(&ioc->encoutput, iov[i].iov_base, want);
641 size -= want;
646 static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *, Error **);
649 static void qio_channel_websock_write_close(QIOChannelWebsock *ioc,
650 uint16_t code, const char *reason)
652 struct iovec iov[2] = {
653 { .iov_base = &code, .iov_len = sizeof(code) },
655 size_t niov = 1;
656 size_t size = iov[0].iov_len;
658 cpu_to_be16s(&code);
660 if (reason) {
661 iov[1].iov_base = (void *)reason;
662 iov[1].iov_len = strlen(reason);
663 size += iov[1].iov_len;
664 niov++;
666 qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE,
667 iov, niov, size);
668 qio_channel_websock_write_wire(ioc, NULL);
669 qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
673 static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc,
674 Error **errp)
676 unsigned char opcode, fin, has_mask;
677 size_t header_size;
678 size_t payload_len;
679 QIOChannelWebsockHeader *header =
680 (QIOChannelWebsockHeader *)ioc->encinput.buffer;
682 if (ioc->payload_remain) {
683 error_setg(errp,
684 "Decoding header but %zu bytes of payload remain",
685 ioc->payload_remain);
686 qio_channel_websock_write_close(
687 ioc, QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR,
688 "internal server error");
689 return -1;
691 if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT) {
692 /* header not complete */
693 return QIO_CHANNEL_ERR_BLOCK;
696 fin = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN;
697 opcode = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE;
698 has_mask = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK;
699 payload_len = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN;
701 /* Save or restore opcode. */
702 if (opcode) {
703 ioc->opcode = opcode;
704 } else {
705 opcode = ioc->opcode;
708 trace_qio_channel_websock_header_partial_decode(ioc, payload_len,
709 fin, opcode, (int)has_mask);
711 if (opcode == QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) {
712 /* disconnect */
713 return 0;
716 /* Websocket frame sanity check:
717 * * Fragmentation is only supported for binary frames.
718 * * All frames sent by a client MUST be masked.
719 * * Only binary and ping/pong encoding is supported.
721 if (!fin) {
722 if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) {
723 error_setg(errp, "only binary websocket frames may be fragmented");
724 qio_channel_websock_write_close(
725 ioc, QIO_CHANNEL_WEBSOCK_STATUS_POLICY ,
726 "only binary frames may be fragmented");
727 return -1;
729 } else {
730 if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME &&
731 opcode != QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE &&
732 opcode != QIO_CHANNEL_WEBSOCK_OPCODE_PING &&
733 opcode != QIO_CHANNEL_WEBSOCK_OPCODE_PONG) {
734 error_setg(errp, "unsupported opcode: %#04x; only binary, close, "
735 "ping, and pong websocket frames are supported", opcode);
736 qio_channel_websock_write_close(
737 ioc, QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA ,
738 "only binary, close, ping, and pong frames are supported");
739 return -1;
742 if (!has_mask) {
743 error_setg(errp, "client websocket frames must be masked");
744 qio_channel_websock_write_close(
745 ioc, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR,
746 "client frames must be masked");
747 return -1;
750 if (payload_len < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT) {
751 ioc->payload_remain = payload_len;
752 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;
753 ioc->mask = header->u.m;
754 } else if (opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) {
755 error_setg(errp, "websocket control frame is too large");
756 qio_channel_websock_write_close(
757 ioc, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR,
758 "control frame is too large");
759 return -1;
760 } else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT &&
761 ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT) {
762 ioc->payload_remain = be16_to_cpu(header->u.s16.l16);
763 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;
764 ioc->mask = header->u.s16.m16;
765 } else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT &&
766 ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT) {
767 ioc->payload_remain = be64_to_cpu(header->u.s64.l64);
768 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;
769 ioc->mask = header->u.s64.m64;
770 } else {
771 /* header not complete */
772 return QIO_CHANNEL_ERR_BLOCK;
775 trace_qio_channel_websock_header_full_decode(
776 ioc, header_size, ioc->payload_remain, ioc->mask.u);
777 buffer_advance(&ioc->encinput, header_size);
778 return 0;
782 static int qio_channel_websock_decode_payload(QIOChannelWebsock *ioc,
783 Error **errp)
785 size_t i;
786 size_t payload_len = 0;
787 uint32_t *payload32;
789 if (ioc->payload_remain) {
790 /* If we aren't at the end of the payload, then drop
791 * off the last bytes, so we're always multiple of 4
792 * for purpose of unmasking, except at end of payload
794 if (ioc->encinput.offset < ioc->payload_remain) {
795 /* Wait for the entire payload before processing control frames
796 * because the payload will most likely be echoed back. */
797 if (ioc->opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) {
798 return QIO_CHANNEL_ERR_BLOCK;
800 payload_len = ioc->encinput.offset - (ioc->encinput.offset % 4);
801 } else {
802 payload_len = ioc->payload_remain;
804 if (payload_len == 0) {
805 return QIO_CHANNEL_ERR_BLOCK;
808 ioc->payload_remain -= payload_len;
810 /* unmask frame */
811 /* process 1 frame (32 bit op) */
812 payload32 = (uint32_t *)ioc->encinput.buffer;
813 for (i = 0; i < payload_len / 4; i++) {
814 payload32[i] ^= ioc->mask.u;
816 /* process the remaining bytes (if any) */
817 for (i *= 4; i < payload_len; i++) {
818 ioc->encinput.buffer[i] ^= ioc->mask.c[i % 4];
822 trace_qio_channel_websock_payload_decode(
823 ioc, ioc->opcode, ioc->payload_remain);
825 if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) {
826 if (payload_len) {
827 /* binary frames are passed on */
828 buffer_reserve(&ioc->rawinput, payload_len);
829 buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len);
831 } else if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) {
832 /* close frames are echoed back */
833 error_setg(errp, "websocket closed by peer");
834 if (payload_len) {
835 /* echo client status */
836 struct iovec iov = { .iov_base = ioc->encinput.buffer,
837 .iov_len = ioc->encinput.offset };
838 qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE,
839 &iov, 1, iov.iov_len);
840 qio_channel_websock_write_wire(ioc, NULL);
841 qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
842 } else {
843 /* send our own status */
844 qio_channel_websock_write_close(
845 ioc, QIO_CHANNEL_WEBSOCK_STATUS_NORMAL, "peer requested close");
847 return -1;
848 } else if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_PING) {
849 /* ping frames produce an immediate reply, as long as we've not still
850 * got a previous pong queued, in which case we drop the new pong */
851 if (ioc->pong_remain == 0) {
852 struct iovec iov = { .iov_base = ioc->encinput.buffer,
853 .iov_len = ioc->encinput.offset };
854 qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_PONG,
855 &iov, 1, iov.iov_len);
856 ioc->pong_remain = ioc->encoutput.offset;
858 } /* pong frames are ignored */
860 if (payload_len) {
861 buffer_advance(&ioc->encinput, payload_len);
863 return 0;
867 QIOChannelWebsock *
868 qio_channel_websock_new_server(QIOChannel *master)
870 QIOChannelWebsock *wioc;
871 QIOChannel *ioc;
873 wioc = QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK));
874 ioc = QIO_CHANNEL(wioc);
876 wioc->master = master;
877 if (qio_channel_has_feature(master, QIO_CHANNEL_FEATURE_SHUTDOWN)) {
878 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
880 object_ref(OBJECT(master));
882 trace_qio_channel_websock_new_server(wioc, master);
883 return wioc;
886 void qio_channel_websock_handshake(QIOChannelWebsock *ioc,
887 QIOTaskFunc func,
888 gpointer opaque,
889 GDestroyNotify destroy)
891 QIOTask *task;
893 task = qio_task_new(OBJECT(ioc),
894 func,
895 opaque,
896 destroy);
898 trace_qio_channel_websock_handshake_start(ioc);
899 trace_qio_channel_websock_handshake_pending(ioc, G_IO_IN);
900 qio_channel_add_watch(ioc->master,
901 G_IO_IN,
902 qio_channel_websock_handshake_io,
903 task,
904 NULL);
908 static void qio_channel_websock_finalize(Object *obj)
910 QIOChannelWebsock *ioc = QIO_CHANNEL_WEBSOCK(obj);
912 buffer_free(&ioc->encinput);
913 buffer_free(&ioc->encoutput);
914 buffer_free(&ioc->rawinput);
915 object_unref(OBJECT(ioc->master));
916 if (ioc->io_tag) {
917 g_source_remove(ioc->io_tag);
919 if (ioc->io_err) {
920 error_free(ioc->io_err);
925 static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
926 Error **errp)
928 ssize_t ret;
930 if (ioc->encinput.offset < 4096) {
931 size_t want = 4096 - ioc->encinput.offset;
933 buffer_reserve(&ioc->encinput, want);
934 ret = qio_channel_read(ioc->master,
935 (char *)ioc->encinput.buffer +
936 ioc->encinput.offset,
937 want,
938 errp);
939 if (ret < 0) {
940 return ret;
942 if (ret == 0 && ioc->encinput.offset == 0) {
943 ioc->io_eof = TRUE;
944 return 0;
946 ioc->encinput.offset += ret;
949 while (ioc->encinput.offset != 0) {
950 if (ioc->payload_remain == 0) {
951 ret = qio_channel_websock_decode_header(ioc, errp);
952 if (ret < 0) {
953 return ret;
957 ret = qio_channel_websock_decode_payload(ioc, errp);
958 if (ret < 0) {
959 return ret;
962 return 1;
966 static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *ioc,
967 Error **errp)
969 ssize_t ret;
970 ssize_t done = 0;
972 while (ioc->encoutput.offset > 0) {
973 ret = qio_channel_write(ioc->master,
974 (char *)ioc->encoutput.buffer,
975 ioc->encoutput.offset,
976 errp);
977 if (ret < 0) {
978 if (ret == QIO_CHANNEL_ERR_BLOCK &&
979 done > 0) {
980 return done;
981 } else {
982 return ret;
985 buffer_advance(&ioc->encoutput, ret);
986 done += ret;
987 if (ioc->pong_remain < ret) {
988 ioc->pong_remain = 0;
989 } else {
990 ioc->pong_remain -= ret;
993 return done;
997 static void qio_channel_websock_flush_free(gpointer user_data)
999 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(user_data);
1000 object_unref(OBJECT(wioc));
1003 static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc);
1005 static gboolean qio_channel_websock_flush(QIOChannel *ioc,
1006 GIOCondition condition,
1007 gpointer user_data)
1009 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(user_data);
1010 ssize_t ret;
1012 if (condition & G_IO_OUT) {
1013 ret = qio_channel_websock_write_wire(wioc, &wioc->io_err);
1014 if (ret < 0) {
1015 goto cleanup;
1019 if (condition & G_IO_IN) {
1020 ret = qio_channel_websock_read_wire(wioc, &wioc->io_err);
1021 if (ret < 0) {
1022 goto cleanup;
1026 cleanup:
1027 qio_channel_websock_set_watch(wioc);
1028 return FALSE;
1032 static void qio_channel_websock_unset_watch(QIOChannelWebsock *ioc)
1034 if (ioc->io_tag) {
1035 g_source_remove(ioc->io_tag);
1036 ioc->io_tag = 0;
1040 static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc)
1042 GIOCondition cond = 0;
1044 qio_channel_websock_unset_watch(ioc);
1046 if (ioc->io_err) {
1047 return;
1050 if (ioc->encoutput.offset) {
1051 cond |= G_IO_OUT;
1053 if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER &&
1054 !ioc->io_eof) {
1055 cond |= G_IO_IN;
1058 if (cond) {
1059 object_ref(OBJECT(ioc));
1060 ioc->io_tag =
1061 qio_channel_add_watch(ioc->master,
1062 cond,
1063 qio_channel_websock_flush,
1064 ioc,
1065 qio_channel_websock_flush_free);
1070 static ssize_t qio_channel_websock_readv(QIOChannel *ioc,
1071 const struct iovec *iov,
1072 size_t niov,
1073 int **fds,
1074 size_t *nfds,
1075 Error **errp)
1077 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1078 size_t i;
1079 ssize_t got = 0;
1080 ssize_t ret;
1082 if (wioc->io_err) {
1083 error_propagate(errp, error_copy(wioc->io_err));
1084 return -1;
1087 if (!wioc->rawinput.offset) {
1088 ret = qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc), errp);
1089 if (ret < 0) {
1090 return ret;
1094 for (i = 0 ; i < niov ; i++) {
1095 size_t want = iov[i].iov_len;
1096 if (want > (wioc->rawinput.offset - got)) {
1097 want = (wioc->rawinput.offset - got);
1100 memcpy(iov[i].iov_base,
1101 wioc->rawinput.buffer + got,
1102 want);
1103 got += want;
1105 if (want < iov[i].iov_len) {
1106 break;
1110 buffer_advance(&wioc->rawinput, got);
1111 qio_channel_websock_set_watch(wioc);
1112 return got;
1116 static ssize_t qio_channel_websock_writev(QIOChannel *ioc,
1117 const struct iovec *iov,
1118 size_t niov,
1119 int *fds,
1120 size_t nfds,
1121 Error **errp)
1123 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1124 ssize_t want = iov_size(iov, niov);
1125 ssize_t avail;
1126 ssize_t ret;
1128 if (wioc->io_err) {
1129 error_propagate(errp, error_copy(wioc->io_err));
1130 return -1;
1133 if (wioc->io_eof) {
1134 error_setg(errp, "%s", "Broken pipe");
1135 return -1;
1138 avail = wioc->encoutput.offset >= QIO_CHANNEL_WEBSOCK_MAX_BUFFER ?
1139 0 : (QIO_CHANNEL_WEBSOCK_MAX_BUFFER - wioc->encoutput.offset);
1140 if (want > avail) {
1141 want = avail;
1144 if (want) {
1145 qio_channel_websock_encode(wioc,
1146 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME,
1147 iov, niov, want);
1150 /* Even if want == 0, we'll try write_wire in case there's
1151 * pending data we could usefully flush out
1153 ret = qio_channel_websock_write_wire(wioc, errp);
1154 if (ret < 0 &&
1155 ret != QIO_CHANNEL_ERR_BLOCK) {
1156 qio_channel_websock_unset_watch(wioc);
1157 return -1;
1160 qio_channel_websock_set_watch(wioc);
1162 if (want == 0) {
1163 return QIO_CHANNEL_ERR_BLOCK;
1166 return want;
1169 static int qio_channel_websock_set_blocking(QIOChannel *ioc,
1170 bool enabled,
1171 Error **errp)
1173 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1175 qio_channel_set_blocking(wioc->master, enabled, errp);
1176 return 0;
1179 static void qio_channel_websock_set_delay(QIOChannel *ioc,
1180 bool enabled)
1182 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
1184 qio_channel_set_delay(tioc->master, enabled);
1187 static void qio_channel_websock_set_cork(QIOChannel *ioc,
1188 bool enabled)
1190 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
1192 qio_channel_set_cork(tioc->master, enabled);
1195 static int qio_channel_websock_shutdown(QIOChannel *ioc,
1196 QIOChannelShutdown how,
1197 Error **errp)
1199 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
1201 return qio_channel_shutdown(tioc->master, how, errp);
1204 static int qio_channel_websock_close(QIOChannel *ioc,
1205 Error **errp)
1207 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1209 trace_qio_channel_websock_close(ioc);
1210 return qio_channel_close(wioc->master, errp);
1213 typedef struct QIOChannelWebsockSource QIOChannelWebsockSource;
1214 struct QIOChannelWebsockSource {
1215 GSource parent;
1216 QIOChannelWebsock *wioc;
1217 GIOCondition condition;
1220 static gboolean
1221 qio_channel_websock_source_check(GSource *source)
1223 QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
1224 GIOCondition cond = 0;
1226 if (wsource->wioc->rawinput.offset || wsource->wioc->io_eof) {
1227 cond |= G_IO_IN;
1229 if (wsource->wioc->encoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
1230 cond |= G_IO_OUT;
1233 return cond & wsource->condition;
1236 static gboolean
1237 qio_channel_websock_source_prepare(GSource *source,
1238 gint *timeout)
1240 *timeout = -1;
1241 return qio_channel_websock_source_check(source);
1244 static gboolean
1245 qio_channel_websock_source_dispatch(GSource *source,
1246 GSourceFunc callback,
1247 gpointer user_data)
1249 QIOChannelFunc func = (QIOChannelFunc)callback;
1250 QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
1252 return (*func)(QIO_CHANNEL(wsource->wioc),
1253 qio_channel_websock_source_check(source),
1254 user_data);
1257 static void
1258 qio_channel_websock_source_finalize(GSource *source)
1260 QIOChannelWebsockSource *ssource = (QIOChannelWebsockSource *)source;
1262 object_unref(OBJECT(ssource->wioc));
1265 GSourceFuncs qio_channel_websock_source_funcs = {
1266 qio_channel_websock_source_prepare,
1267 qio_channel_websock_source_check,
1268 qio_channel_websock_source_dispatch,
1269 qio_channel_websock_source_finalize
1272 static GSource *qio_channel_websock_create_watch(QIOChannel *ioc,
1273 GIOCondition condition)
1275 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1276 QIOChannelWebsockSource *ssource;
1277 GSource *source;
1279 source = g_source_new(&qio_channel_websock_source_funcs,
1280 sizeof(QIOChannelWebsockSource));
1281 ssource = (QIOChannelWebsockSource *)source;
1283 ssource->wioc = wioc;
1284 object_ref(OBJECT(wioc));
1286 ssource->condition = condition;
1288 qio_channel_websock_set_watch(wioc);
1289 return source;
1292 static void qio_channel_websock_class_init(ObjectClass *klass,
1293 void *class_data G_GNUC_UNUSED)
1295 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
1297 ioc_klass->io_writev = qio_channel_websock_writev;
1298 ioc_klass->io_readv = qio_channel_websock_readv;
1299 ioc_klass->io_set_blocking = qio_channel_websock_set_blocking;
1300 ioc_klass->io_set_cork = qio_channel_websock_set_cork;
1301 ioc_klass->io_set_delay = qio_channel_websock_set_delay;
1302 ioc_klass->io_close = qio_channel_websock_close;
1303 ioc_klass->io_shutdown = qio_channel_websock_shutdown;
1304 ioc_klass->io_create_watch = qio_channel_websock_create_watch;
1307 static const TypeInfo qio_channel_websock_info = {
1308 .parent = TYPE_QIO_CHANNEL,
1309 .name = TYPE_QIO_CHANNEL_WEBSOCK,
1310 .instance_size = sizeof(QIOChannelWebsock),
1311 .instance_finalize = qio_channel_websock_finalize,
1312 .class_init = qio_channel_websock_class_init,
1315 static void qio_channel_websock_register_types(void)
1317 type_register_static(&qio_channel_websock_info);
1320 type_init(qio_channel_websock_register_types);