blob: 47a0e941d9f6f9af92223b350e9b41a5a133befc [file] [log] [blame]
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001/*
2 * QEMU I/O channels driver websockets
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
Peter Maydellcae9fc52016-01-29 17:50:03 +000021#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010022#include "qapi/error.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010023#include "qemu/bswap.h"
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000024#include "io/channel-websock.h"
25#include "crypto/hash.h"
26#include "trace.h"
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +010027#include "qemu/iov.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020028#include "qemu/module.h"
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000029
Daniel P. Berrangea7b20a82017-10-09 14:43:42 +010030/* Max amount to allow in rawinput/encoutput buffers */
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000031#define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
32
33#define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
34#define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
35#define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
36
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +000037#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
38#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
39#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
40#define QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE "upgrade"
41#define QIO_CHANNEL_WEBSOCK_HEADER_HOST "host"
42#define QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION "connection"
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000043
44#define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +000045#define QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE "Upgrade"
46#define QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET "websocket"
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000047
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +010048#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
49 "Server: QEMU VNC\r\n" \
50 "Date: %s\r\n"
51
Yu-Chen Linc64e1e72019-11-23 11:43:06 +080052#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_WITH_PROTO_RES_OK \
53 "HTTP/1.1 101 Switching Protocols\r\n" \
54 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
55 "Upgrade: websocket\r\n" \
56 "Connection: Upgrade\r\n" \
57 "Sec-WebSocket-Accept: %s\r\n" \
58 "Sec-WebSocket-Protocol: binary\r\n" \
59 "\r\n"
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +010060#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK \
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000061 "HTTP/1.1 101 Switching Protocols\r\n" \
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +010062 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000063 "Upgrade: websocket\r\n" \
64 "Connection: Upgrade\r\n" \
65 "Sec-WebSocket-Accept: %s\r\n" \
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000066 "\r\n"
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +010067#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND \
68 "HTTP/1.1 404 Not Found\r\n" \
69 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
70 "Connection: close\r\n" \
71 "\r\n"
72#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST \
73 "HTTP/1.1 400 Bad Request\r\n" \
74 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
75 "Connection: close\r\n" \
76 "Sec-WebSocket-Version: " \
77 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION \
78 "\r\n"
79#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR \
80 "HTTP/1.1 500 Internal Server Error\r\n" \
81 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
82 "Connection: close\r\n" \
83 "\r\n"
84#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE \
85 "HTTP/1.1 403 Request Entity Too Large\r\n" \
86 QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
87 "Connection: close\r\n" \
88 "\r\n"
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000089#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
90#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
91#define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +000092#define QIO_CHANNEL_WEBSOCK_HTTP_METHOD "GET"
93#define QIO_CHANNEL_WEBSOCK_HTTP_PATH "/"
94#define QIO_CHANNEL_WEBSOCK_HTTP_VERSION "HTTP/1.1"
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +000095
96/* The websockets packet header is variable length
97 * depending on the size of the payload... */
98
99/* ...length when using 7-bit payload length */
100#define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
101/* ...length when using 16-bit payload length */
102#define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
103/* ...length when using 64-bit payload length */
104#define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
105
106/* Length of the optional data mask field in header */
107#define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
108
109/* Maximum length that can fit in 7-bit payload size */
110#define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
111/* Maximum length that can fit in 16-bit payload size */
112#define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
113
114/* Magic 7-bit length to indicate use of 16-bit payload length */
115#define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
116/* Magic 7-bit length to indicate use of 64-bit payload length */
117#define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
118
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700119/* Bitmasks for accessing header fields */
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000120#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
121#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
122#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
123#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
Brandon Carpenter01af17f2017-09-12 08:21:51 -0700124#define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000125
126typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader;
127
128struct QEMU_PACKED QIOChannelWebsockHeader {
129 unsigned char b0;
130 unsigned char b1;
131 union {
132 struct QEMU_PACKED {
133 uint16_t l16;
134 QIOChannelWebsockMask m16;
135 } s16;
136 struct QEMU_PACKED {
137 uint64_t l64;
138 QIOChannelWebsockMask m64;
139 } s64;
140 QIOChannelWebsockMask m;
141 } u;
142};
143
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000144typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader;
145
146struct QIOChannelWebsockHTTPHeader {
147 char *name;
148 char *value;
149};
150
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000151enum {
152 QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION = 0x0,
153 QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME = 0x1,
154 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME = 0x2,
155 QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE = 0x8,
156 QIO_CHANNEL_WEBSOCK_OPCODE_PING = 0x9,
157 QIO_CHANNEL_WEBSOCK_OPCODE_PONG = 0xA
158};
159
Stefan Weil52aa5642017-10-07 16:56:09 +0200160static void GCC_FMT_ATTR(2, 3)
161qio_channel_websock_handshake_send_res(QIOChannelWebsock *ioc,
162 const char *resmsg,
163 ...)
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100164{
165 va_list vargs;
166 char *response;
167 size_t responselen;
168
169 va_start(vargs, resmsg);
170 response = g_strdup_vprintf(resmsg, vargs);
171 responselen = strlen(response);
172 buffer_reserve(&ioc->encoutput, responselen);
173 buffer_append(&ioc->encoutput, response, responselen);
Marc-André Lureauad824bd2018-10-19 14:12:14 +0400174 g_free(response);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100175 va_end(vargs);
176}
177
178static gchar *qio_channel_websock_date_str(void)
179{
180 struct tm tm;
181 time_t now = time(NULL);
182 char datebuf[128];
183
184 gmtime_r(&now, &tm);
185
186 strftime(datebuf, sizeof(datebuf), "%a, %d %b %Y %H:%M:%S GMT", &tm);
187
188 return g_strdup(datebuf);
189}
190
191static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock *ioc,
192 const char *resdata)
193{
194 char *date = qio_channel_websock_date_str();
195 qio_channel_websock_handshake_send_res(ioc, resdata, date);
196 g_free(date);
197}
198
Brandon Carpenter530ca602017-09-12 08:21:53 -0700199enum {
200 QIO_CHANNEL_WEBSOCK_STATUS_NORMAL = 1000,
201 QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR = 1002,
202 QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA = 1003,
203 QIO_CHANNEL_WEBSOCK_STATUS_POLICY = 1008,
204 QIO_CHANNEL_WEBSOCK_STATUS_TOO_LARGE = 1009,
205 QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR = 1011,
206};
207
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000208static size_t
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100209qio_channel_websock_extract_headers(QIOChannelWebsock *ioc,
210 char *buffer,
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000211 QIOChannelWebsockHTTPHeader *hdrs,
212 size_t nhdrsalloc,
213 Error **errp)
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000214{
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000215 char *nl, *sep, *tmp;
216 size_t nhdrs = 0;
217
218 /*
219 * First parse the HTTP protocol greeting of format:
220 *
221 * $METHOD $PATH $VERSION
222 *
223 * e.g.
224 *
225 * GET / HTTP/1.1
226 */
227
228 nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
229 if (!nl) {
230 error_setg(errp, "Missing HTTP header delimiter");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100231 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000232 }
233 *nl = '\0';
Daniel P. Berrange0efd6c92017-10-10 13:28:03 +0100234 trace_qio_channel_websock_http_greeting(ioc, buffer);
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000235
236 tmp = strchr(buffer, ' ');
237 if (!tmp) {
238 error_setg(errp, "Missing HTTP path delimiter");
239 return 0;
240 }
241 *tmp = '\0';
242
243 if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_METHOD)) {
244 error_setg(errp, "Unsupported HTTP method %s", buffer);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100245 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000246 }
247
248 buffer = tmp + 1;
249 tmp = strchr(buffer, ' ');
250 if (!tmp) {
251 error_setg(errp, "Missing HTTP version delimiter");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100252 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000253 }
254 *tmp = '\0';
255
256 if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_PATH)) {
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100257 qio_channel_websock_handshake_send_res_err(
258 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND);
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000259 error_setg(errp, "Unexpected HTTP path %s", buffer);
260 return 0;
261 }
262
263 buffer = tmp + 1;
264
265 if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_VERSION)) {
266 error_setg(errp, "Unsupported HTTP version %s", buffer);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100267 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000268 }
269
270 buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
271
272 /*
273 * Now parse all the header fields of format
274 *
275 * $NAME: $VALUE
276 *
277 * e.g.
278 *
279 * Cache-control: no-cache
280 */
281 do {
282 QIOChannelWebsockHTTPHeader *hdr;
283
284 nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
285 if (nl) {
286 *nl = '\0';
287 }
288
289 sep = strchr(buffer, ':');
290 if (!sep) {
291 error_setg(errp, "Malformed HTTP header");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100292 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000293 }
294 *sep = '\0';
295 sep++;
296 while (*sep == ' ') {
297 sep++;
298 }
299
300 if (nhdrs >= nhdrsalloc) {
301 error_setg(errp, "Too many HTTP headers");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100302 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000303 }
304
305 hdr = &hdrs[nhdrs++];
306 hdr->name = buffer;
307 hdr->value = sep;
308
309 /* Canonicalize header name for easier identification later */
310 for (tmp = hdr->name; *tmp; tmp++) {
311 *tmp = g_ascii_tolower(*tmp);
312 }
313
314 if (nl) {
315 buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
316 }
317 } while (nl != NULL);
318
319 return nhdrs;
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100320
321 bad_request:
322 qio_channel_websock_handshake_send_res_err(
323 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST);
324 return 0;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000325}
326
327static const char *
328qio_channel_websock_find_header(QIOChannelWebsockHTTPHeader *hdrs,
329 size_t nhdrs,
330 const char *name)
331{
332 size_t i;
333
334 for (i = 0; i < nhdrs; i++) {
335 if (g_str_equal(hdrs[i].name, name)) {
336 return hdrs[i].value;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000337 }
338 }
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000339
340 return NULL;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000341}
342
343
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100344static void qio_channel_websock_handshake_send_res_ok(QIOChannelWebsock *ioc,
345 const char *key,
Yu-Chen Linc64e1e72019-11-23 11:43:06 +0800346 const bool use_protocols,
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100347 Error **errp)
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000348{
349 char combined_key[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
350 QIO_CHANNEL_WEBSOCK_GUID_LEN + 1];
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100351 char *accept = NULL;
Daniel P. Berrange7fc3fce2017-10-11 16:38:10 +0100352 char *date = NULL;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000353
354 g_strlcpy(combined_key, key, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN + 1);
355 g_strlcat(combined_key, QIO_CHANNEL_WEBSOCK_GUID,
356 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
357 QIO_CHANNEL_WEBSOCK_GUID_LEN + 1);
358
359 /* hash and encode it */
360 if (qcrypto_hash_base64(QCRYPTO_HASH_ALG_SHA1,
361 combined_key,
362 QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
363 QIO_CHANNEL_WEBSOCK_GUID_LEN,
364 &accept,
365 errp) < 0) {
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100366 qio_channel_websock_handshake_send_res_err(
367 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR);
368 return;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000369 }
370
Daniel P. Berrange7fc3fce2017-10-11 16:38:10 +0100371 date = qio_channel_websock_date_str();
Yu-Chen Linc64e1e72019-11-23 11:43:06 +0800372 if (use_protocols) {
373 qio_channel_websock_handshake_send_res(
374 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_WITH_PROTO_RES_OK,
375 date, accept);
376 } else {
377 qio_channel_websock_handshake_send_res(
378 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK, date, accept);
379 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000380
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100381 g_free(date);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000382 g_free(accept);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000383}
384
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100385static void qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
386 char *buffer,
387 Error **errp)
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000388{
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000389 QIOChannelWebsockHTTPHeader hdrs[32];
390 size_t nhdrs = G_N_ELEMENTS(hdrs);
391 const char *protocols = NULL, *version = NULL, *key = NULL,
392 *host = NULL, *connection = NULL, *upgrade = NULL;
Daniel P. Berrange6d5d23b2017-10-09 17:52:28 +0100393 char **connectionv;
394 bool upgraded = false;
395 size_t i;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000396
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100397 nhdrs = qio_channel_websock_extract_headers(ioc, buffer, hdrs, nhdrs, errp);
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000398 if (!nhdrs) {
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100399 return;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000400 }
401
402 protocols = qio_channel_websock_find_header(
403 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000404
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000405 version = qio_channel_websock_find_header(
406 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_VERSION);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000407 if (!version) {
408 error_setg(errp, "Missing websocket version header data");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100409 goto bad_request;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000410 }
411
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000412 key = qio_channel_websock_find_header(
413 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_KEY);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000414 if (!key) {
415 error_setg(errp, "Missing websocket key header data");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100416 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000417 }
418
419 host = qio_channel_websock_find_header(
420 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_HOST);
421 if (!host) {
422 error_setg(errp, "Missing websocket host header data");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100423 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000424 }
425
426 connection = qio_channel_websock_find_header(
427 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION);
428 if (!connection) {
429 error_setg(errp, "Missing websocket connection header data");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100430 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000431 }
432
433 upgrade = qio_channel_websock_find_header(
434 hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE);
435 if (!upgrade) {
436 error_setg(errp, "Missing websocket upgrade header data");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100437 goto bad_request;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000438 }
439
Daniel P. Berrange0efd6c92017-10-10 13:28:03 +0100440 trace_qio_channel_websock_http_request(ioc, protocols, version,
441 host, connection, upgrade, key);
442
Yu-Chen Linc64e1e72019-11-23 11:43:06 +0800443 if (protocols) {
444 if (!g_strrstr(protocols, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY)) {
445 error_setg(errp, "No '%s' protocol is supported by client '%s'",
446 QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY, protocols);
447 goto bad_request;
448 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000449 }
450
451 if (!g_str_equal(version, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION)) {
452 error_setg(errp, "Version '%s' is not supported by client '%s'",
453 QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION, version);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100454 goto bad_request;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000455 }
456
457 if (strlen(key) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN) {
458 error_setg(errp, "Key length '%zu' was not as expected '%d'",
459 strlen(key), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100460 goto bad_request;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000461 }
462
Daniel P. Berrange6d5d23b2017-10-09 17:52:28 +0100463 connectionv = g_strsplit(connection, ",", 0);
464 for (i = 0; connectionv != NULL && connectionv[i] != NULL; i++) {
465 g_strstrip(connectionv[i]);
466 if (strcasecmp(connectionv[i],
467 QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE) == 0) {
468 upgraded = true;
469 }
470 }
471 g_strfreev(connectionv);
472 if (!upgraded) {
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000473 error_setg(errp, "No connection upgrade requested '%s'", connection);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100474 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000475 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000476
Daniel P. Berrange33badfd2017-09-06 14:49:41 +0100477 if (strcasecmp(upgrade, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET) != 0) {
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000478 error_setg(errp, "Incorrect upgrade method '%s'", upgrade);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100479 goto bad_request;
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000480 }
481
Yu-Chen Linc64e1e72019-11-23 11:43:06 +0800482 qio_channel_websock_handshake_send_res_ok(ioc, key, !!protocols, errp);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100483 return;
484
485 bad_request:
486 qio_channel_websock_handshake_send_res_err(
487 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000488}
489
490static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
491 Error **errp)
492{
493 char *handshake_end;
494 ssize_t ret;
495 /* Typical HTTP headers from novnc are 512 bytes, so limiting
496 * total header size to 4096 is easily enough. */
497 size_t want = 4096 - ioc->encinput.offset;
498 buffer_reserve(&ioc->encinput, want);
499 ret = qio_channel_read(ioc->master,
500 (char *)buffer_end(&ioc->encinput), want, errp);
501 if (ret < 0) {
502 return -1;
503 }
504 ioc->encinput.offset += ret;
505
506 handshake_end = g_strstr_len((char *)ioc->encinput.buffer,
507 ioc->encinput.offset,
508 QIO_CHANNEL_WEBSOCK_HANDSHAKE_END);
509 if (!handshake_end) {
510 if (ioc->encinput.offset >= 4096) {
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100511 qio_channel_websock_handshake_send_res_err(
512 ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000513 error_setg(errp,
514 "End of headers not found in first 4096 bytes");
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100515 return 1;
Edgar Kaziakhmedova46ded12018-01-10 18:39:24 +0300516 } else if (ret == 0) {
517 error_setg(errp,
518 "End of headers not found before connection closed");
519 return -1;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000520 }
Edgar Kaziakhmedova46ded12018-01-10 18:39:24 +0300521 return 0;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000522 }
Daniel P. Berrange07e95cd2017-02-28 10:37:24 +0000523 *handshake_end = '\0';
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000524
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100525 qio_channel_websock_handshake_process(ioc,
526 (char *)ioc->encinput.buffer,
527 errp);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000528
529 buffer_advance(&ioc->encinput,
530 handshake_end - (char *)ioc->encinput.buffer +
531 strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END));
532 return 1;
533}
534
535static gboolean qio_channel_websock_handshake_send(QIOChannel *ioc,
536 GIOCondition condition,
537 gpointer user_data)
538{
539 QIOTask *task = user_data;
540 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(
541 qio_task_get_source(task));
542 Error *err = NULL;
543 ssize_t ret;
544
545 ret = qio_channel_write(wioc->master,
546 (char *)wioc->encoutput.buffer,
547 wioc->encoutput.offset,
548 &err);
549
550 if (ret < 0) {
Daniel P. Berrange3a3f8702017-09-06 11:38:36 +0100551 trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
Daniel P. Berrange60e705c2016-08-11 15:20:58 +0100552 qio_task_set_error(task, err);
553 qio_task_complete(task);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000554 return FALSE;
555 }
556
557 buffer_advance(&wioc->encoutput, ret);
558 if (wioc->encoutput.offset == 0) {
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100559 if (wioc->io_err) {
Daniel P. Berrange3a3f8702017-09-06 11:38:36 +0100560 trace_qio_channel_websock_handshake_fail(
561 ioc, error_get_pretty(wioc->io_err));
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100562 qio_task_set_error(task, wioc->io_err);
563 wioc->io_err = NULL;
564 qio_task_complete(task);
565 } else {
566 trace_qio_channel_websock_handshake_complete(ioc);
567 qio_task_complete(task);
568 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000569 return FALSE;
570 }
571 trace_qio_channel_websock_handshake_pending(ioc, G_IO_OUT);
572 return TRUE;
573}
574
575static gboolean qio_channel_websock_handshake_io(QIOChannel *ioc,
576 GIOCondition condition,
577 gpointer user_data)
578{
579 QIOTask *task = user_data;
580 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(
581 qio_task_get_source(task));
582 Error *err = NULL;
583 int ret;
584
585 ret = qio_channel_websock_handshake_read(wioc, &err);
586 if (ret < 0) {
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100587 /*
588 * We only take this path on a fatal I/O error reading from
589 * client connection, as most of the time we have an
590 * HTTP 4xx err response to send instead
591 */
Daniel P. Berrange3a3f8702017-09-06 11:38:36 +0100592 trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
Daniel P. Berrange60e705c2016-08-11 15:20:58 +0100593 qio_task_set_error(task, err);
594 qio_task_complete(task);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000595 return FALSE;
596 }
597 if (ret == 0) {
598 trace_qio_channel_websock_handshake_pending(ioc, G_IO_IN);
599 /* need more data still */
600 return TRUE;
601 }
602
Laurent Vivier710c2632018-03-23 15:32:00 +0100603 error_propagate(&wioc->io_err, err);
Daniel P. Berrangef69a8bd2017-09-06 11:33:17 +0100604
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000605 trace_qio_channel_websock_handshake_reply(ioc);
606 qio_channel_add_watch(
607 wioc->master,
608 G_IO_OUT,
609 qio_channel_websock_handshake_send,
610 task,
Daniel P. Berrangebc35d512016-06-07 12:27:51 +0100611 NULL);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000612 return FALSE;
613}
614
615
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100616static void qio_channel_websock_encode(QIOChannelWebsock *ioc,
617 uint8_t opcode,
618 const struct iovec *iov,
619 size_t niov,
620 size_t size)
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000621{
622 size_t header_size;
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100623 size_t i;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000624 union {
625 char buf[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT];
626 QIOChannelWebsockHeader ws;
627 } header;
628
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100629 assert(size <= iov_size(iov, niov));
630
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700631 header.ws.b0 = QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN |
Brandon Carpenter268a53f2017-09-12 08:21:52 -0700632 (opcode & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE);
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100633 if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) {
634 header.ws.b1 = (uint8_t)size;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000635 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100636 } else if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) {
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000637 header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT;
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100638 header.ws.u.s16.l16 = cpu_to_be16((uint16_t)size);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000639 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;
640 } else {
641 header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT;
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100642 header.ws.u.s64.l64 = cpu_to_be64(size);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000643 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;
644 }
645 header_size -= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK;
646
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100647 trace_qio_channel_websock_encode(ioc, opcode, header_size, size);
648 buffer_reserve(&ioc->encoutput, header_size + size);
649 buffer_append(&ioc->encoutput, header.buf, header_size);
650 for (i = 0; i < niov && size != 0; i++) {
651 size_t want = iov[i].iov_len;
652 if (want > size) {
653 want = size;
654 }
655 buffer_append(&ioc->encoutput, iov[i].iov_base, want);
656 size -= want;
657 }
Brandon Carpenter268a53f2017-09-12 08:21:52 -0700658}
659
660
Brandon Carpenter530ca602017-09-12 08:21:53 -0700661static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *, Error **);
662
663
664static void qio_channel_websock_write_close(QIOChannelWebsock *ioc,
665 uint16_t code, const char *reason)
666{
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +0100667 struct iovec iov[2] = {
668 { .iov_base = &code, .iov_len = sizeof(code) },
669 };
670 size_t niov = 1;
671 size_t size = iov[0].iov_len;
672
673 cpu_to_be16s(&code);
674
Brandon Carpenter530ca602017-09-12 08:21:53 -0700675 if (reason) {
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +0100676 iov[1].iov_base = (void *)reason;
677 iov[1].iov_len = strlen(reason);
678 size += iov[1].iov_len;
679 niov++;
Brandon Carpenter530ca602017-09-12 08:21:53 -0700680 }
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100681 qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE,
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +0100682 iov, niov, size);
Brandon Carpenter530ca602017-09-12 08:21:53 -0700683 qio_channel_websock_write_wire(ioc, NULL);
684 qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
685}
686
687
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700688static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc,
689 Error **errp)
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000690{
691 unsigned char opcode, fin, has_mask;
692 size_t header_size;
693 size_t payload_len;
694 QIOChannelWebsockHeader *header =
695 (QIOChannelWebsockHeader *)ioc->encinput.buffer;
696
697 if (ioc->payload_remain) {
698 error_setg(errp,
699 "Decoding header but %zu bytes of payload remain",
700 ioc->payload_remain);
Brandon Carpenter530ca602017-09-12 08:21:53 -0700701 qio_channel_websock_write_close(
702 ioc, QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR,
703 "internal server error");
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000704 return -1;
705 }
706 if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT) {
707 /* header not complete */
708 return QIO_CHANNEL_ERR_BLOCK;
709 }
710
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700711 fin = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000712 opcode = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE;
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700713 has_mask = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000714 payload_len = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN;
715
Brandon Carpenterff1300e2017-09-12 08:21:49 -0700716 /* Save or restore opcode. */
717 if (opcode) {
718 ioc->opcode = opcode;
719 } else {
720 opcode = ioc->opcode;
721 }
722
Daniel P. Berrange59f183b2017-09-21 11:00:47 +0100723 trace_qio_channel_websock_header_partial_decode(ioc, payload_len,
724 fin, opcode, (int)has_mask);
725
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000726 if (opcode == QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) {
727 /* disconnect */
728 return 0;
729 }
730
731 /* Websocket frame sanity check:
Brandon Carpenterff1300e2017-09-12 08:21:49 -0700732 * * Fragmentation is only supported for binary frames.
733 * * All frames sent by a client MUST be masked.
Brandon Carpenter268a53f2017-09-12 08:21:52 -0700734 * * Only binary and ping/pong encoding is supported.
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000735 */
736 if (!fin) {
Brandon Carpenterff1300e2017-09-12 08:21:49 -0700737 if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) {
738 error_setg(errp, "only binary websocket frames may be fragmented");
Brandon Carpenter530ca602017-09-12 08:21:53 -0700739 qio_channel_websock_write_close(
740 ioc, QIO_CHANNEL_WEBSOCK_STATUS_POLICY ,
741 "only binary frames may be fragmented");
Brandon Carpenterff1300e2017-09-12 08:21:49 -0700742 return -1;
743 }
744 } else {
Brandon Carpenter01af17f2017-09-12 08:21:51 -0700745 if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME &&
Brandon Carpenter530ca602017-09-12 08:21:53 -0700746 opcode != QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE &&
Brandon Carpenter01af17f2017-09-12 08:21:51 -0700747 opcode != QIO_CHANNEL_WEBSOCK_OPCODE_PING &&
748 opcode != QIO_CHANNEL_WEBSOCK_OPCODE_PONG) {
Brandon Carpenter530ca602017-09-12 08:21:53 -0700749 error_setg(errp, "unsupported opcode: %#04x; only binary, close, "
750 "ping, and pong websocket frames are supported", opcode);
751 qio_channel_websock_write_close(
752 ioc, QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA ,
753 "only binary, close, ping, and pong frames are supported");
Brandon Carpenterff1300e2017-09-12 08:21:49 -0700754 return -1;
755 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000756 }
757 if (!has_mask) {
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700758 error_setg(errp, "client websocket frames must be masked");
Brandon Carpenter530ca602017-09-12 08:21:53 -0700759 qio_channel_websock_write_close(
760 ioc, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR,
761 "client frames must be masked");
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000762 return -1;
763 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000764
765 if (payload_len < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT) {
766 ioc->payload_remain = payload_len;
767 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;
768 ioc->mask = header->u.m;
Brandon Carpenter01af17f2017-09-12 08:21:51 -0700769 } else if (opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) {
770 error_setg(errp, "websocket control frame is too large");
Brandon Carpenter530ca602017-09-12 08:21:53 -0700771 qio_channel_websock_write_close(
772 ioc, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR,
773 "control frame is too large");
Brandon Carpenter01af17f2017-09-12 08:21:51 -0700774 return -1;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000775 } else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT &&
776 ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT) {
777 ioc->payload_remain = be16_to_cpu(header->u.s16.l16);
778 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;
779 ioc->mask = header->u.s16.m16;
780 } else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT &&
781 ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT) {
782 ioc->payload_remain = be64_to_cpu(header->u.s64.l64);
783 header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;
784 ioc->mask = header->u.s64.m64;
785 } else {
786 /* header not complete */
787 return QIO_CHANNEL_ERR_BLOCK;
788 }
789
Daniel P. Berrange59f183b2017-09-21 11:00:47 +0100790 trace_qio_channel_websock_header_full_decode(
791 ioc, header_size, ioc->payload_remain, ioc->mask.u);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000792 buffer_advance(&ioc->encinput, header_size);
Brandon Carpenter530ca602017-09-12 08:21:53 -0700793 return 0;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000794}
795
796
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700797static int qio_channel_websock_decode_payload(QIOChannelWebsock *ioc,
798 Error **errp)
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000799{
800 size_t i;
Brandon Carpenter3a296402017-09-12 08:21:50 -0700801 size_t payload_len = 0;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000802 uint32_t *payload32;
803
Brandon Carpenter3a296402017-09-12 08:21:50 -0700804 if (ioc->payload_remain) {
805 /* If we aren't at the end of the payload, then drop
806 * off the last bytes, so we're always multiple of 4
807 * for purpose of unmasking, except at end of payload
808 */
809 if (ioc->encinput.offset < ioc->payload_remain) {
Brandon Carpenter268a53f2017-09-12 08:21:52 -0700810 /* Wait for the entire payload before processing control frames
811 * because the payload will most likely be echoed back. */
812 if (ioc->opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) {
813 return QIO_CHANNEL_ERR_BLOCK;
814 }
Brandon Carpenter3a296402017-09-12 08:21:50 -0700815 payload_len = ioc->encinput.offset - (ioc->encinput.offset % 4);
816 } else {
817 payload_len = ioc->payload_remain;
818 }
819 if (payload_len == 0) {
820 return QIO_CHANNEL_ERR_BLOCK;
821 }
822
823 ioc->payload_remain -= payload_len;
824
825 /* unmask frame */
826 /* process 1 frame (32 bit op) */
827 payload32 = (uint32_t *)ioc->encinput.buffer;
828 for (i = 0; i < payload_len / 4; i++) {
829 payload32[i] ^= ioc->mask.u;
830 }
831 /* process the remaining bytes (if any) */
832 for (i *= 4; i < payload_len; i++) {
833 ioc->encinput.buffer[i] ^= ioc->mask.c[i % 4];
834 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000835 }
836
Daniel P. Berrange59f183b2017-09-21 11:00:47 +0100837 trace_qio_channel_websock_payload_decode(
838 ioc, ioc->opcode, ioc->payload_remain);
839
Brandon Carpenter01af17f2017-09-12 08:21:51 -0700840 if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) {
841 if (payload_len) {
Brandon Carpenter268a53f2017-09-12 08:21:52 -0700842 /* binary frames are passed on */
Brandon Carpenter01af17f2017-09-12 08:21:51 -0700843 buffer_reserve(&ioc->rawinput, payload_len);
844 buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len);
845 }
Brandon Carpenter530ca602017-09-12 08:21:53 -0700846 } else if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) {
847 /* close frames are echoed back */
848 error_setg(errp, "websocket closed by peer");
849 if (payload_len) {
850 /* echo client status */
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100851 struct iovec iov = { .iov_base = ioc->encinput.buffer,
852 .iov_len = ioc->encinput.offset };
853 qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE,
854 &iov, 1, iov.iov_len);
Brandon Carpenter530ca602017-09-12 08:21:53 -0700855 qio_channel_websock_write_wire(ioc, NULL);
856 qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
857 } else {
858 /* send our own status */
859 qio_channel_websock_write_close(
860 ioc, QIO_CHANNEL_WEBSOCK_STATUS_NORMAL, "peer requested close");
861 }
862 return -1;
Brandon Carpenter268a53f2017-09-12 08:21:52 -0700863 } else if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_PING) {
Daniel P. Berrange57b0cdf2017-10-09 15:34:06 +0100864 /* ping frames produce an immediate reply, as long as we've not still
865 * got a previous pong queued, in which case we drop the new pong */
866 if (ioc->pong_remain == 0) {
Daniel P. Berrangefb74e592017-10-09 16:33:20 +0100867 struct iovec iov = { .iov_base = ioc->encinput.buffer,
868 .iov_len = ioc->encinput.offset };
869 qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_PONG,
870 &iov, 1, iov.iov_len);
Daniel P. Berrange57b0cdf2017-10-09 15:34:06 +0100871 ioc->pong_remain = ioc->encoutput.offset;
872 }
Brandon Carpenter268a53f2017-09-12 08:21:52 -0700873 } /* pong frames are ignored */
Brandon Carpenter01af17f2017-09-12 08:21:51 -0700874
Brandon Carpenter3a296402017-09-12 08:21:50 -0700875 if (payload_len) {
Brandon Carpenter3a296402017-09-12 08:21:50 -0700876 buffer_advance(&ioc->encinput, payload_len);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000877 }
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700878 return 0;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000879}
880
881
882QIOChannelWebsock *
883qio_channel_websock_new_server(QIOChannel *master)
884{
885 QIOChannelWebsock *wioc;
886 QIOChannel *ioc;
887
888 wioc = QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK));
889 ioc = QIO_CHANNEL(wioc);
890
891 wioc->master = master;
Felipe Franciosie413ae02016-09-29 08:52:36 -0700892 if (qio_channel_has_feature(master, QIO_CHANNEL_FEATURE_SHUTDOWN)) {
Felipe Franciosid8d3c7c2016-09-29 08:52:37 -0700893 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000894 }
895 object_ref(OBJECT(master));
896
897 trace_qio_channel_websock_new_server(wioc, master);
898 return wioc;
899}
900
901void qio_channel_websock_handshake(QIOChannelWebsock *ioc,
902 QIOTaskFunc func,
903 gpointer opaque,
904 GDestroyNotify destroy)
905{
906 QIOTask *task;
907
908 task = qio_task_new(OBJECT(ioc),
909 func,
910 opaque,
911 destroy);
912
913 trace_qio_channel_websock_handshake_start(ioc);
914 trace_qio_channel_websock_handshake_pending(ioc, G_IO_IN);
915 qio_channel_add_watch(ioc->master,
916 G_IO_IN,
917 qio_channel_websock_handshake_io,
918 task,
919 NULL);
920}
921
922
923static void qio_channel_websock_finalize(Object *obj)
924{
925 QIOChannelWebsock *ioc = QIO_CHANNEL_WEBSOCK(obj);
926
927 buffer_free(&ioc->encinput);
928 buffer_free(&ioc->encoutput);
929 buffer_free(&ioc->rawinput);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000930 object_unref(OBJECT(ioc->master));
931 if (ioc->io_tag) {
932 g_source_remove(ioc->io_tag);
933 }
934 if (ioc->io_err) {
935 error_free(ioc->io_err);
936 }
937}
938
939
940static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
941 Error **errp)
942{
943 ssize_t ret;
944
945 if (ioc->encinput.offset < 4096) {
946 size_t want = 4096 - ioc->encinput.offset;
947
948 buffer_reserve(&ioc->encinput, want);
949 ret = qio_channel_read(ioc->master,
950 (char *)ioc->encinput.buffer +
951 ioc->encinput.offset,
952 want,
953 errp);
954 if (ret < 0) {
955 return ret;
956 }
Brandon Carpentereefa3d82017-09-12 08:21:48 -0700957 if (ret == 0 && ioc->encinput.offset == 0) {
958 ioc->io_eof = TRUE;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000959 return 0;
960 }
961 ioc->encinput.offset += ret;
962 }
963
Daniel P. Berrangecd892a22017-01-27 18:11:32 +0000964 while (ioc->encinput.offset != 0) {
965 if (ioc->payload_remain == 0) {
966 ret = qio_channel_websock_decode_header(ioc, errp);
967 if (ret < 0) {
968 return ret;
969 }
Daniel P. Berrangecd892a22017-01-27 18:11:32 +0000970 }
971
972 ret = qio_channel_websock_decode_payload(ioc, errp);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000973 if (ret < 0) {
974 return ret;
975 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000976 }
Daniel P. Berrangecd892a22017-01-27 18:11:32 +0000977 return 1;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000978}
979
980
981static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *ioc,
982 Error **errp)
983{
984 ssize_t ret;
985 ssize_t done = 0;
Brandon Carpenter268a53f2017-09-12 08:21:52 -0700986
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +0000987 while (ioc->encoutput.offset > 0) {
988 ret = qio_channel_write(ioc->master,
989 (char *)ioc->encoutput.buffer,
990 ioc->encoutput.offset,
991 errp);
992 if (ret < 0) {
993 if (ret == QIO_CHANNEL_ERR_BLOCK &&
994 done > 0) {
995 return done;
996 } else {
997 return ret;
998 }
999 }
1000 buffer_advance(&ioc->encoutput, ret);
1001 done += ret;
Daniel P. Berrange57b0cdf2017-10-09 15:34:06 +01001002 if (ioc->pong_remain < ret) {
1003 ioc->pong_remain = 0;
1004 } else {
1005 ioc->pong_remain -= ret;
1006 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001007 }
1008 return done;
1009}
1010
1011
1012static void qio_channel_websock_flush_free(gpointer user_data)
1013{
1014 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(user_data);
1015 object_unref(OBJECT(wioc));
1016}
1017
1018static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc);
1019
1020static gboolean qio_channel_websock_flush(QIOChannel *ioc,
1021 GIOCondition condition,
1022 gpointer user_data)
1023{
1024 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(user_data);
1025 ssize_t ret;
1026
1027 if (condition & G_IO_OUT) {
1028 ret = qio_channel_websock_write_wire(wioc, &wioc->io_err);
1029 if (ret < 0) {
1030 goto cleanup;
1031 }
1032 }
1033
1034 if (condition & G_IO_IN) {
1035 ret = qio_channel_websock_read_wire(wioc, &wioc->io_err);
1036 if (ret < 0) {
1037 goto cleanup;
1038 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001039 }
1040
1041 cleanup:
1042 qio_channel_websock_set_watch(wioc);
1043 return FALSE;
1044}
1045
1046
1047static void qio_channel_websock_unset_watch(QIOChannelWebsock *ioc)
1048{
1049 if (ioc->io_tag) {
1050 g_source_remove(ioc->io_tag);
1051 ioc->io_tag = 0;
1052 }
1053}
1054
1055static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc)
1056{
1057 GIOCondition cond = 0;
1058
1059 qio_channel_websock_unset_watch(ioc);
1060
1061 if (ioc->io_err) {
1062 return;
1063 }
1064
Daniel P. Berrange57b0cdf2017-10-09 15:34:06 +01001065 if (ioc->encoutput.offset) {
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001066 cond |= G_IO_OUT;
1067 }
1068 if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER &&
1069 !ioc->io_eof) {
1070 cond |= G_IO_IN;
1071 }
1072
1073 if (cond) {
1074 object_ref(OBJECT(ioc));
1075 ioc->io_tag =
1076 qio_channel_add_watch(ioc->master,
1077 cond,
1078 qio_channel_websock_flush,
1079 ioc,
1080 qio_channel_websock_flush_free);
1081 }
1082}
1083
1084
1085static ssize_t qio_channel_websock_readv(QIOChannel *ioc,
1086 const struct iovec *iov,
1087 size_t niov,
1088 int **fds,
1089 size_t *nfds,
1090 Error **errp)
1091{
1092 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1093 size_t i;
1094 ssize_t got = 0;
1095 ssize_t ret;
1096
1097 if (wioc->io_err) {
Eduardo Habkoste79ea672017-06-08 10:39:04 -03001098 error_propagate(errp, error_copy(wioc->io_err));
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001099 return -1;
1100 }
1101
1102 if (!wioc->rawinput.offset) {
1103 ret = qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc), errp);
1104 if (ret < 0) {
1105 return ret;
1106 }
1107 }
1108
1109 for (i = 0 ; i < niov ; i++) {
1110 size_t want = iov[i].iov_len;
1111 if (want > (wioc->rawinput.offset - got)) {
1112 want = (wioc->rawinput.offset - got);
1113 }
1114
1115 memcpy(iov[i].iov_base,
1116 wioc->rawinput.buffer + got,
1117 want);
1118 got += want;
1119
1120 if (want < iov[i].iov_len) {
1121 break;
1122 }
1123 }
1124
1125 buffer_advance(&wioc->rawinput, got);
1126 qio_channel_websock_set_watch(wioc);
1127 return got;
1128}
1129
1130
1131static ssize_t qio_channel_websock_writev(QIOChannel *ioc,
1132 const struct iovec *iov,
1133 size_t niov,
1134 int *fds,
1135 size_t nfds,
1136 Error **errp)
1137{
1138 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +01001139 ssize_t want = iov_size(iov, niov);
1140 ssize_t avail;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001141 ssize_t ret;
1142
1143 if (wioc->io_err) {
Eduardo Habkoste79ea672017-06-08 10:39:04 -03001144 error_propagate(errp, error_copy(wioc->io_err));
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001145 return -1;
1146 }
1147
1148 if (wioc->io_eof) {
1149 error_setg(errp, "%s", "Broken pipe");
1150 return -1;
1151 }
1152
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +01001153 avail = wioc->encoutput.offset >= QIO_CHANNEL_WEBSOCK_MAX_BUFFER ?
1154 0 : (QIO_CHANNEL_WEBSOCK_MAX_BUFFER - wioc->encoutput.offset);
1155 if (want > avail) {
1156 want = avail;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001157 }
1158
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +01001159 if (want) {
Daniel P. Berrangefb74e592017-10-09 16:33:20 +01001160 qio_channel_websock_encode(wioc,
1161 QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME,
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +01001162 iov, niov, want);
Daniel P. Berrangebac6c952017-10-09 14:39:17 +01001163 }
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +01001164
1165 /* Even if want == 0, we'll try write_wire in case there's
1166 * pending data we could usefully flush out
1167 */
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001168 ret = qio_channel_websock_write_wire(wioc, errp);
1169 if (ret < 0 &&
1170 ret != QIO_CHANNEL_ERR_BLOCK) {
1171 qio_channel_websock_unset_watch(wioc);
1172 return -1;
1173 }
1174
1175 qio_channel_websock_set_watch(wioc);
1176
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +01001177 if (want == 0) {
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001178 return QIO_CHANNEL_ERR_BLOCK;
1179 }
1180
Daniel P. Berrange8dfd5f92017-10-09 16:54:07 +01001181 return want;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001182}
1183
1184static int qio_channel_websock_set_blocking(QIOChannel *ioc,
1185 bool enabled,
1186 Error **errp)
1187{
1188 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1189
1190 qio_channel_set_blocking(wioc->master, enabled, errp);
1191 return 0;
1192}
1193
1194static void qio_channel_websock_set_delay(QIOChannel *ioc,
1195 bool enabled)
1196{
1197 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
1198
1199 qio_channel_set_delay(tioc->master, enabled);
1200}
1201
1202static void qio_channel_websock_set_cork(QIOChannel *ioc,
1203 bool enabled)
1204{
1205 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
1206
1207 qio_channel_set_cork(tioc->master, enabled);
1208}
1209
1210static int qio_channel_websock_shutdown(QIOChannel *ioc,
1211 QIOChannelShutdown how,
1212 Error **errp)
1213{
1214 QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
1215
1216 return qio_channel_shutdown(tioc->master, how, errp);
1217}
1218
1219static int qio_channel_websock_close(QIOChannel *ioc,
1220 Error **errp)
1221{
1222 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1223
Daniel P. Berrange59f183b2017-09-21 11:00:47 +01001224 trace_qio_channel_websock_close(ioc);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001225 return qio_channel_close(wioc->master, errp);
1226}
1227
1228typedef struct QIOChannelWebsockSource QIOChannelWebsockSource;
1229struct QIOChannelWebsockSource {
1230 GSource parent;
1231 QIOChannelWebsock *wioc;
1232 GIOCondition condition;
1233};
1234
1235static gboolean
Brandon Carpentereefa3d82017-09-12 08:21:48 -07001236qio_channel_websock_source_check(GSource *source)
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001237{
1238 QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
1239 GIOCondition cond = 0;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001240
Daniel P. Berrangédd154c42019-02-20 18:22:39 +00001241 if (wsource->wioc->rawinput.offset) {
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001242 cond |= G_IO_IN;
1243 }
Daniel P. Berrangea7b20a82017-10-09 14:43:42 +01001244 if (wsource->wioc->encoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001245 cond |= G_IO_OUT;
1246 }
Daniel P. Berrangédd154c42019-02-20 18:22:39 +00001247 if (wsource->wioc->io_eof) {
1248 cond |= G_IO_HUP;
1249 }
1250 if (wsource->wioc->io_err) {
1251 cond |= G_IO_ERR;
1252 }
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001253
1254 return cond & wsource->condition;
1255}
1256
1257static gboolean
Brandon Carpentereefa3d82017-09-12 08:21:48 -07001258qio_channel_websock_source_prepare(GSource *source,
1259 gint *timeout)
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001260{
Brandon Carpentereefa3d82017-09-12 08:21:48 -07001261 *timeout = -1;
1262 return qio_channel_websock_source_check(source);
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001263}
1264
1265static gboolean
1266qio_channel_websock_source_dispatch(GSource *source,
1267 GSourceFunc callback,
1268 gpointer user_data)
1269{
1270 QIOChannelFunc func = (QIOChannelFunc)callback;
1271 QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001272
1273 return (*func)(QIO_CHANNEL(wsource->wioc),
Brandon Carpentereefa3d82017-09-12 08:21:48 -07001274 qio_channel_websock_source_check(source),
Daniel P. Berrange2d1d0e72015-03-04 15:57:41 +00001275 user_data);
1276}
1277
1278static void
1279qio_channel_websock_source_finalize(GSource *source)
1280{
1281 QIOChannelWebsockSource *ssource = (QIOChannelWebsockSource *)source;
1282
1283 object_unref(OBJECT(ssource->wioc));
1284}
1285
1286GSourceFuncs qio_channel_websock_source_funcs = {
1287 qio_channel_websock_source_prepare,
1288 qio_channel_websock_source_check,
1289 qio_channel_websock_source_dispatch,
1290 qio_channel_websock_source_finalize
1291};
1292
1293static GSource *qio_channel_websock_create_watch(QIOChannel *ioc,
1294 GIOCondition condition)
1295{
1296 QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
1297 QIOChannelWebsockSource *ssource;
1298 GSource *source;
1299
1300 source = g_source_new(&qio_channel_websock_source_funcs,
1301 sizeof(QIOChannelWebsockSource));
1302 ssource = (QIOChannelWebsockSource *)source;
1303
1304 ssource->wioc = wioc;
1305 object_ref(OBJECT(wioc));
1306
1307 ssource->condition = condition;
1308
1309 qio_channel_websock_set_watch(wioc);
1310 return source;
1311}
1312
1313static void qio_channel_websock_class_init(ObjectClass *klass,
1314 void *class_data G_GNUC_UNUSED)
1315{
1316 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
1317
1318 ioc_klass->io_writev = qio_channel_websock_writev;
1319 ioc_klass->io_readv = qio_channel_websock_readv;
1320 ioc_klass->io_set_blocking = qio_channel_websock_set_blocking;
1321 ioc_klass->io_set_cork = qio_channel_websock_set_cork;
1322 ioc_klass->io_set_delay = qio_channel_websock_set_delay;
1323 ioc_klass->io_close = qio_channel_websock_close;
1324 ioc_klass->io_shutdown = qio_channel_websock_shutdown;
1325 ioc_klass->io_create_watch = qio_channel_websock_create_watch;
1326}
1327
1328static const TypeInfo qio_channel_websock_info = {
1329 .parent = TYPE_QIO_CHANNEL,
1330 .name = TYPE_QIO_CHANNEL_WEBSOCK,
1331 .instance_size = sizeof(QIOChannelWebsock),
1332 .instance_finalize = qio_channel_websock_finalize,
1333 .class_init = qio_channel_websock_class_init,
1334};
1335
1336static void qio_channel_websock_register_types(void)
1337{
1338 type_register_static(&qio_channel_websock_info);
1339}
1340
1341type_init(qio_channel_websock_register_types);