Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1 | /* |
| 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 Maydell | cae9fc5 | 2016-01-29 17:50:03 +0000 | [diff] [blame] | 21 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 22 | #include "qapi/error.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 23 | #include "qemu/bswap.h" |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 24 | #include "io/channel-websock.h" |
| 25 | #include "crypto/hash.h" |
| 26 | #include "trace.h" |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 27 | #include "qemu/iov.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 28 | #include "qemu/module.h" |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 29 | |
Daniel P. Berrange | a7b20a8 | 2017-10-09 14:43:42 +0100 | [diff] [blame] | 30 | /* Max amount to allow in rawinput/encoutput buffers */ |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 31 | #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. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 37 | #define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol" |
| 38 | #define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version" |
| 39 | #define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key" |
| 40 | #define QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE "upgrade" |
| 41 | #define QIO_CHANNEL_WEBSOCK_HEADER_HOST "host" |
| 42 | #define QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION "connection" |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 43 | |
| 44 | #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary" |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 45 | #define QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE "Upgrade" |
| 46 | #define QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET "websocket" |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 47 | |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 48 | #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \ |
| 49 | "Server: QEMU VNC\r\n" \ |
| 50 | "Date: %s\r\n" |
| 51 | |
Yu-Chen Lin | c64e1e7 | 2019-11-23 11:43:06 +0800 | [diff] [blame] | 52 | #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_WITH_PROTO_RES_OK \ |
| 53 | "HTTP/1.1 101 Switching Protocols\r\n" \ |
| 54 | QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \ |
| 55 | "Upgrade: websocket\r\n" \ |
| 56 | "Connection: Upgrade\r\n" \ |
| 57 | "Sec-WebSocket-Accept: %s\r\n" \ |
| 58 | "Sec-WebSocket-Protocol: binary\r\n" \ |
| 59 | "\r\n" |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 60 | #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK \ |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 61 | "HTTP/1.1 101 Switching Protocols\r\n" \ |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 62 | QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \ |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 63 | "Upgrade: websocket\r\n" \ |
| 64 | "Connection: Upgrade\r\n" \ |
| 65 | "Sec-WebSocket-Accept: %s\r\n" \ |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 66 | "\r\n" |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 67 | #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND \ |
| 68 | "HTTP/1.1 404 Not Found\r\n" \ |
| 69 | QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \ |
| 70 | "Connection: close\r\n" \ |
| 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 89 | #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n" |
| 90 | #define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n" |
| 91 | #define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13" |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 92 | #define QIO_CHANNEL_WEBSOCK_HTTP_METHOD "GET" |
| 93 | #define QIO_CHANNEL_WEBSOCK_HTTP_PATH "/" |
| 94 | #define QIO_CHANNEL_WEBSOCK_HTTP_VERSION "HTTP/1.1" |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 95 | |
| 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 Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 119 | /* Bitmasks for accessing header fields */ |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 120 | #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80 |
| 121 | #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f |
| 122 | #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80 |
| 123 | #define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f |
Brandon Carpenter | 01af17f | 2017-09-12 08:21:51 -0700 | [diff] [blame] | 124 | #define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8 |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 125 | |
| 126 | typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader; |
| 127 | |
| 128 | struct 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. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 144 | typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader; |
| 145 | |
| 146 | struct QIOChannelWebsockHTTPHeader { |
| 147 | char *name; |
| 148 | char *value; |
| 149 | }; |
| 150 | |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 151 | enum { |
| 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 Weil | 52aa564 | 2017-10-07 16:56:09 +0200 | [diff] [blame] | 160 | static void GCC_FMT_ATTR(2, 3) |
| 161 | qio_channel_websock_handshake_send_res(QIOChannelWebsock *ioc, |
| 162 | const char *resmsg, |
| 163 | ...) |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 164 | { |
| 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é Lureau | ad824bd | 2018-10-19 14:12:14 +0400 | [diff] [blame] | 174 | g_free(response); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 175 | va_end(vargs); |
| 176 | } |
| 177 | |
| 178 | static 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 | |
| 191 | static 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 Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 199 | enum { |
| 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. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 208 | static size_t |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 209 | qio_channel_websock_extract_headers(QIOChannelWebsock *ioc, |
| 210 | char *buffer, |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 211 | QIOChannelWebsockHTTPHeader *hdrs, |
| 212 | size_t nhdrsalloc, |
| 213 | Error **errp) |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 214 | { |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 215 | 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 231 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 232 | } |
| 233 | *nl = '\0'; |
Daniel P. Berrange | 0efd6c9 | 2017-10-10 13:28:03 +0100 | [diff] [blame] | 234 | trace_qio_channel_websock_http_greeting(ioc, buffer); |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 235 | |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 245 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | buffer = tmp + 1; |
| 249 | tmp = strchr(buffer, ' '); |
| 250 | if (!tmp) { |
| 251 | error_setg(errp, "Missing HTTP version delimiter"); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 252 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 253 | } |
| 254 | *tmp = '\0'; |
| 255 | |
| 256 | if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_PATH)) { |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 257 | qio_channel_websock_handshake_send_res_err( |
| 258 | ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND); |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 259 | 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 267 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 268 | } |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 292 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 293 | } |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 302 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 303 | } |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 320 | |
| 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. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static const char * |
| 328 | qio_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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 339 | |
| 340 | return NULL; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 344 | static void qio_channel_websock_handshake_send_res_ok(QIOChannelWebsock *ioc, |
| 345 | const char *key, |
Yu-Chen Lin | c64e1e7 | 2019-11-23 11:43:06 +0800 | [diff] [blame] | 346 | const bool use_protocols, |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 347 | Error **errp) |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 348 | { |
| 349 | char combined_key[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN + |
| 350 | QIO_CHANNEL_WEBSOCK_GUID_LEN + 1]; |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 351 | char *accept = NULL; |
Daniel P. Berrange | 7fc3fce | 2017-10-11 16:38:10 +0100 | [diff] [blame] | 352 | char *date = NULL; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 353 | |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 366 | qio_channel_websock_handshake_send_res_err( |
| 367 | ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR); |
| 368 | return; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Daniel P. Berrange | 7fc3fce | 2017-10-11 16:38:10 +0100 | [diff] [blame] | 371 | date = qio_channel_websock_date_str(); |
Yu-Chen Lin | c64e1e7 | 2019-11-23 11:43:06 +0800 | [diff] [blame] | 372 | 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 380 | |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 381 | g_free(date); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 382 | g_free(accept); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 385 | static void qio_channel_websock_handshake_process(QIOChannelWebsock *ioc, |
| 386 | char *buffer, |
| 387 | Error **errp) |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 388 | { |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 389 | QIOChannelWebsockHTTPHeader hdrs[32]; |
| 390 | size_t nhdrs = G_N_ELEMENTS(hdrs); |
| 391 | const char *protocols = NULL, *version = NULL, *key = NULL, |
| 392 | *host = NULL, *connection = NULL, *upgrade = NULL; |
Daniel P. Berrange | 6d5d23b | 2017-10-09 17:52:28 +0100 | [diff] [blame] | 393 | char **connectionv; |
| 394 | bool upgraded = false; |
| 395 | size_t i; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 396 | |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 397 | nhdrs = qio_channel_websock_extract_headers(ioc, buffer, hdrs, nhdrs, errp); |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 398 | if (!nhdrs) { |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 399 | return; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | protocols = qio_channel_websock_find_header( |
| 403 | hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 404 | |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 405 | version = qio_channel_websock_find_header( |
| 406 | hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_VERSION); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 407 | if (!version) { |
| 408 | error_setg(errp, "Missing websocket version header data"); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 409 | goto bad_request; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 412 | key = qio_channel_websock_find_header( |
| 413 | hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_KEY); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 414 | if (!key) { |
| 415 | error_setg(errp, "Missing websocket key header data"); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 416 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 417 | } |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 423 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 424 | } |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 430 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 431 | } |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 437 | goto bad_request; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Daniel P. Berrange | 0efd6c9 | 2017-10-10 13:28:03 +0100 | [diff] [blame] | 440 | trace_qio_channel_websock_http_request(ioc, protocols, version, |
| 441 | host, connection, upgrade, key); |
| 442 | |
Yu-Chen Lin | c64e1e7 | 2019-11-23 11:43:06 +0800 | [diff] [blame] | 443 | 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 449 | } |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 454 | goto bad_request; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 455 | } |
| 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 460 | goto bad_request; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Daniel P. Berrange | 6d5d23b | 2017-10-09 17:52:28 +0100 | [diff] [blame] | 463 | connectionv = g_strsplit(connection, ",", 0); |
| 464 | for (i = 0; connectionv != NULL && connectionv[i] != NULL; i++) { |
| 465 | g_strstrip(connectionv[i]); |
| 466 | if (strcasecmp(connectionv[i], |
| 467 | QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE) == 0) { |
| 468 | upgraded = true; |
| 469 | } |
| 470 | } |
| 471 | g_strfreev(connectionv); |
| 472 | if (!upgraded) { |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 473 | error_setg(errp, "No connection upgrade requested '%s'", connection); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 474 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 475 | } |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 476 | |
Daniel P. Berrange | 33badfd | 2017-09-06 14:49:41 +0100 | [diff] [blame] | 477 | if (strcasecmp(upgrade, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET) != 0) { |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 478 | error_setg(errp, "Incorrect upgrade method '%s'", upgrade); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 479 | goto bad_request; |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Yu-Chen Lin | c64e1e7 | 2019-11-23 11:43:06 +0800 | [diff] [blame] | 482 | qio_channel_websock_handshake_send_res_ok(ioc, key, !!protocols, errp); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 483 | return; |
| 484 | |
| 485 | bad_request: |
| 486 | qio_channel_websock_handshake_send_res_err( |
| 487 | ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 511 | qio_channel_websock_handshake_send_res_err( |
| 512 | ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 513 | error_setg(errp, |
| 514 | "End of headers not found in first 4096 bytes"); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 515 | return 1; |
Edgar Kaziakhmedov | a46ded1 | 2018-01-10 18:39:24 +0300 | [diff] [blame] | 516 | } else if (ret == 0) { |
| 517 | error_setg(errp, |
| 518 | "End of headers not found before connection closed"); |
| 519 | return -1; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 520 | } |
Edgar Kaziakhmedov | a46ded1 | 2018-01-10 18:39:24 +0300 | [diff] [blame] | 521 | return 0; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 522 | } |
Daniel P. Berrange | 07e95cd | 2017-02-28 10:37:24 +0000 | [diff] [blame] | 523 | *handshake_end = '\0'; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 524 | |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 525 | qio_channel_websock_handshake_process(ioc, |
| 526 | (char *)ioc->encinput.buffer, |
| 527 | errp); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 528 | |
| 529 | buffer_advance(&ioc->encinput, |
| 530 | handshake_end - (char *)ioc->encinput.buffer + |
| 531 | strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END)); |
| 532 | return 1; |
| 533 | } |
| 534 | |
| 535 | static 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. Berrange | 3a3f870 | 2017-09-06 11:38:36 +0100 | [diff] [blame] | 551 | trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err)); |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 552 | qio_task_set_error(task, err); |
| 553 | qio_task_complete(task); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 554 | return FALSE; |
| 555 | } |
| 556 | |
| 557 | buffer_advance(&wioc->encoutput, ret); |
| 558 | if (wioc->encoutput.offset == 0) { |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 559 | if (wioc->io_err) { |
Daniel P. Berrange | 3a3f870 | 2017-09-06 11:38:36 +0100 | [diff] [blame] | 560 | trace_qio_channel_websock_handshake_fail( |
| 561 | ioc, error_get_pretty(wioc->io_err)); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 562 | 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 569 | return FALSE; |
| 570 | } |
| 571 | trace_qio_channel_websock_handshake_pending(ioc, G_IO_OUT); |
| 572 | return TRUE; |
| 573 | } |
| 574 | |
| 575 | static 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. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 587 | /* |
| 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. Berrange | 3a3f870 | 2017-09-06 11:38:36 +0100 | [diff] [blame] | 592 | trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err)); |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 593 | qio_task_set_error(task, err); |
| 594 | qio_task_complete(task); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 595 | 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 Vivier | 710c263 | 2018-03-23 15:32:00 +0100 | [diff] [blame] | 603 | error_propagate(&wioc->io_err, err); |
Daniel P. Berrange | f69a8bd | 2017-09-06 11:33:17 +0100 | [diff] [blame] | 604 | |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 605 | 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. Berrange | bc35d51 | 2016-06-07 12:27:51 +0100 | [diff] [blame] | 611 | NULL); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 612 | return FALSE; |
| 613 | } |
| 614 | |
| 615 | |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 616 | static 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 621 | { |
| 622 | size_t header_size; |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 623 | size_t i; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 624 | union { |
| 625 | char buf[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT]; |
| 626 | QIOChannelWebsockHeader ws; |
| 627 | } header; |
| 628 | |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 629 | assert(size <= iov_size(iov, niov)); |
| 630 | |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 631 | header.ws.b0 = QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN | |
Brandon Carpenter | 268a53f | 2017-09-12 08:21:52 -0700 | [diff] [blame] | 632 | (opcode & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE); |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 633 | if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) { |
| 634 | header.ws.b1 = (uint8_t)size; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 635 | header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT; |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 636 | } else if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) { |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 637 | header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT; |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 638 | header.ws.u.s16.l16 = cpu_to_be16((uint16_t)size); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 639 | 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. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 642 | header.ws.u.s64.l64 = cpu_to_be64(size); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 643 | header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT; |
| 644 | } |
| 645 | header_size -= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK; |
| 646 | |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 647 | trace_qio_channel_websock_encode(ioc, opcode, header_size, size); |
| 648 | buffer_reserve(&ioc->encoutput, header_size + size); |
| 649 | buffer_append(&ioc->encoutput, header.buf, header_size); |
| 650 | for (i = 0; i < niov && size != 0; i++) { |
| 651 | size_t want = iov[i].iov_len; |
| 652 | if (want > size) { |
| 653 | want = size; |
| 654 | } |
| 655 | buffer_append(&ioc->encoutput, iov[i].iov_base, want); |
| 656 | size -= want; |
| 657 | } |
Brandon Carpenter | 268a53f | 2017-09-12 08:21:52 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 661 | static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *, Error **); |
| 662 | |
| 663 | |
| 664 | static void qio_channel_websock_write_close(QIOChannelWebsock *ioc, |
| 665 | uint16_t code, const char *reason) |
| 666 | { |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 667 | 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 Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 675 | if (reason) { |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 676 | iov[1].iov_base = (void *)reason; |
| 677 | iov[1].iov_len = strlen(reason); |
| 678 | size += iov[1].iov_len; |
| 679 | niov++; |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 680 | } |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 681 | qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 682 | iov, niov, size); |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 683 | qio_channel_websock_write_wire(ioc, NULL); |
| 684 | qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); |
| 685 | } |
| 686 | |
| 687 | |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 688 | static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc, |
| 689 | Error **errp) |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 690 | { |
| 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 Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 701 | qio_channel_websock_write_close( |
| 702 | ioc, QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR, |
| 703 | "internal server error"); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 704 | 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 Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 711 | fin = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 712 | opcode = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE; |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 713 | has_mask = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 714 | payload_len = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN; |
| 715 | |
Brandon Carpenter | ff1300e | 2017-09-12 08:21:49 -0700 | [diff] [blame] | 716 | /* Save or restore opcode. */ |
| 717 | if (opcode) { |
| 718 | ioc->opcode = opcode; |
| 719 | } else { |
| 720 | opcode = ioc->opcode; |
| 721 | } |
| 722 | |
Daniel P. Berrange | 59f183b | 2017-09-21 11:00:47 +0100 | [diff] [blame] | 723 | trace_qio_channel_websock_header_partial_decode(ioc, payload_len, |
| 724 | fin, opcode, (int)has_mask); |
| 725 | |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 726 | if (opcode == QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) { |
| 727 | /* disconnect */ |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | /* Websocket frame sanity check: |
Brandon Carpenter | ff1300e | 2017-09-12 08:21:49 -0700 | [diff] [blame] | 732 | * * Fragmentation is only supported for binary frames. |
| 733 | * * All frames sent by a client MUST be masked. |
Brandon Carpenter | 268a53f | 2017-09-12 08:21:52 -0700 | [diff] [blame] | 734 | * * Only binary and ping/pong encoding is supported. |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 735 | */ |
| 736 | if (!fin) { |
Brandon Carpenter | ff1300e | 2017-09-12 08:21:49 -0700 | [diff] [blame] | 737 | if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { |
| 738 | error_setg(errp, "only binary websocket frames may be fragmented"); |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 739 | qio_channel_websock_write_close( |
| 740 | ioc, QIO_CHANNEL_WEBSOCK_STATUS_POLICY , |
| 741 | "only binary frames may be fragmented"); |
Brandon Carpenter | ff1300e | 2017-09-12 08:21:49 -0700 | [diff] [blame] | 742 | return -1; |
| 743 | } |
| 744 | } else { |
Brandon Carpenter | 01af17f | 2017-09-12 08:21:51 -0700 | [diff] [blame] | 745 | if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME && |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 746 | opcode != QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE && |
Brandon Carpenter | 01af17f | 2017-09-12 08:21:51 -0700 | [diff] [blame] | 747 | opcode != QIO_CHANNEL_WEBSOCK_OPCODE_PING && |
| 748 | opcode != QIO_CHANNEL_WEBSOCK_OPCODE_PONG) { |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 749 | error_setg(errp, "unsupported opcode: %#04x; only binary, close, " |
| 750 | "ping, and pong websocket frames are supported", opcode); |
| 751 | qio_channel_websock_write_close( |
| 752 | ioc, QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA , |
| 753 | "only binary, close, ping, and pong frames are supported"); |
Brandon Carpenter | ff1300e | 2017-09-12 08:21:49 -0700 | [diff] [blame] | 754 | return -1; |
| 755 | } |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 756 | } |
| 757 | if (!has_mask) { |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 758 | error_setg(errp, "client websocket frames must be masked"); |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 759 | qio_channel_websock_write_close( |
| 760 | ioc, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR, |
| 761 | "client frames must be masked"); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 762 | return -1; |
| 763 | } |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 764 | |
| 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 Carpenter | 01af17f | 2017-09-12 08:21:51 -0700 | [diff] [blame] | 769 | } else if (opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) { |
| 770 | error_setg(errp, "websocket control frame is too large"); |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 771 | qio_channel_websock_write_close( |
| 772 | ioc, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR, |
| 773 | "control frame is too large"); |
Brandon Carpenter | 01af17f | 2017-09-12 08:21:51 -0700 | [diff] [blame] | 774 | return -1; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 775 | } else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT && |
| 776 | ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT) { |
| 777 | ioc->payload_remain = be16_to_cpu(header->u.s16.l16); |
| 778 | header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT; |
| 779 | ioc->mask = header->u.s16.m16; |
| 780 | } else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT && |
| 781 | ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT) { |
| 782 | ioc->payload_remain = be64_to_cpu(header->u.s64.l64); |
| 783 | header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT; |
| 784 | ioc->mask = header->u.s64.m64; |
| 785 | } else { |
| 786 | /* header not complete */ |
| 787 | return QIO_CHANNEL_ERR_BLOCK; |
| 788 | } |
| 789 | |
Daniel P. Berrange | 59f183b | 2017-09-21 11:00:47 +0100 | [diff] [blame] | 790 | trace_qio_channel_websock_header_full_decode( |
| 791 | ioc, header_size, ioc->payload_remain, ioc->mask.u); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 792 | buffer_advance(&ioc->encinput, header_size); |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 793 | return 0; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 797 | static int qio_channel_websock_decode_payload(QIOChannelWebsock *ioc, |
| 798 | Error **errp) |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 799 | { |
| 800 | size_t i; |
Brandon Carpenter | 3a29640 | 2017-09-12 08:21:50 -0700 | [diff] [blame] | 801 | size_t payload_len = 0; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 802 | uint32_t *payload32; |
| 803 | |
Brandon Carpenter | 3a29640 | 2017-09-12 08:21:50 -0700 | [diff] [blame] | 804 | if (ioc->payload_remain) { |
| 805 | /* If we aren't at the end of the payload, then drop |
| 806 | * off the last bytes, so we're always multiple of 4 |
| 807 | * for purpose of unmasking, except at end of payload |
| 808 | */ |
| 809 | if (ioc->encinput.offset < ioc->payload_remain) { |
Brandon Carpenter | 268a53f | 2017-09-12 08:21:52 -0700 | [diff] [blame] | 810 | /* Wait for the entire payload before processing control frames |
| 811 | * because the payload will most likely be echoed back. */ |
| 812 | if (ioc->opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) { |
| 813 | return QIO_CHANNEL_ERR_BLOCK; |
| 814 | } |
Brandon Carpenter | 3a29640 | 2017-09-12 08:21:50 -0700 | [diff] [blame] | 815 | 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Daniel P. Berrange | 59f183b | 2017-09-21 11:00:47 +0100 | [diff] [blame] | 837 | trace_qio_channel_websock_payload_decode( |
| 838 | ioc, ioc->opcode, ioc->payload_remain); |
| 839 | |
Brandon Carpenter | 01af17f | 2017-09-12 08:21:51 -0700 | [diff] [blame] | 840 | if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { |
| 841 | if (payload_len) { |
Brandon Carpenter | 268a53f | 2017-09-12 08:21:52 -0700 | [diff] [blame] | 842 | /* binary frames are passed on */ |
Brandon Carpenter | 01af17f | 2017-09-12 08:21:51 -0700 | [diff] [blame] | 843 | buffer_reserve(&ioc->rawinput, payload_len); |
| 844 | buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len); |
| 845 | } |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 846 | } 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. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 851 | struct iovec iov = { .iov_base = ioc->encinput.buffer, |
| 852 | .iov_len = ioc->encinput.offset }; |
| 853 | qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, |
| 854 | &iov, 1, iov.iov_len); |
Brandon Carpenter | 530ca60 | 2017-09-12 08:21:53 -0700 | [diff] [blame] | 855 | 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 Carpenter | 268a53f | 2017-09-12 08:21:52 -0700 | [diff] [blame] | 863 | } else if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_PING) { |
Daniel P. Berrange | 57b0cdf | 2017-10-09 15:34:06 +0100 | [diff] [blame] | 864 | /* ping frames produce an immediate reply, as long as we've not still |
| 865 | * got a previous pong queued, in which case we drop the new pong */ |
| 866 | if (ioc->pong_remain == 0) { |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 867 | struct iovec iov = { .iov_base = ioc->encinput.buffer, |
| 868 | .iov_len = ioc->encinput.offset }; |
| 869 | qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_PONG, |
| 870 | &iov, 1, iov.iov_len); |
Daniel P. Berrange | 57b0cdf | 2017-10-09 15:34:06 +0100 | [diff] [blame] | 871 | ioc->pong_remain = ioc->encoutput.offset; |
| 872 | } |
Brandon Carpenter | 268a53f | 2017-09-12 08:21:52 -0700 | [diff] [blame] | 873 | } /* pong frames are ignored */ |
Brandon Carpenter | 01af17f | 2017-09-12 08:21:51 -0700 | [diff] [blame] | 874 | |
Brandon Carpenter | 3a29640 | 2017-09-12 08:21:50 -0700 | [diff] [blame] | 875 | if (payload_len) { |
Brandon Carpenter | 3a29640 | 2017-09-12 08:21:50 -0700 | [diff] [blame] | 876 | buffer_advance(&ioc->encinput, payload_len); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 877 | } |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 878 | return 0; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | |
| 882 | QIOChannelWebsock * |
| 883 | qio_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 Franciosi | e413ae0 | 2016-09-29 08:52:36 -0700 | [diff] [blame] | 892 | if (qio_channel_has_feature(master, QIO_CHANNEL_FEATURE_SHUTDOWN)) { |
Felipe Franciosi | d8d3c7c | 2016-09-29 08:52:37 -0700 | [diff] [blame] | 893 | qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 894 | } |
| 895 | object_ref(OBJECT(master)); |
| 896 | |
| 897 | trace_qio_channel_websock_new_server(wioc, master); |
| 898 | return wioc; |
| 899 | } |
| 900 | |
| 901 | void 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 | |
| 923 | static 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 930 | 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 | |
| 940 | static 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 Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 957 | if (ret == 0 && ioc->encinput.offset == 0) { |
| 958 | ioc->io_eof = TRUE; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 959 | return 0; |
| 960 | } |
| 961 | ioc->encinput.offset += ret; |
| 962 | } |
| 963 | |
Daniel P. Berrange | cd892a2 | 2017-01-27 18:11:32 +0000 | [diff] [blame] | 964 | 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. Berrange | cd892a2 | 2017-01-27 18:11:32 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | ret = qio_channel_websock_decode_payload(ioc, errp); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 973 | if (ret < 0) { |
| 974 | return ret; |
| 975 | } |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 976 | } |
Daniel P. Berrange | cd892a2 | 2017-01-27 18:11:32 +0000 | [diff] [blame] | 977 | return 1; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | |
| 981 | static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *ioc, |
| 982 | Error **errp) |
| 983 | { |
| 984 | ssize_t ret; |
| 985 | ssize_t done = 0; |
Brandon Carpenter | 268a53f | 2017-09-12 08:21:52 -0700 | [diff] [blame] | 986 | |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 987 | 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. Berrange | 57b0cdf | 2017-10-09 15:34:06 +0100 | [diff] [blame] | 1002 | if (ioc->pong_remain < ret) { |
| 1003 | ioc->pong_remain = 0; |
| 1004 | } else { |
| 1005 | ioc->pong_remain -= ret; |
| 1006 | } |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1007 | } |
| 1008 | return done; |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | static 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 | |
| 1018 | static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc); |
| 1019 | |
| 1020 | static 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | cleanup: |
| 1042 | qio_channel_websock_set_watch(wioc); |
| 1043 | return FALSE; |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | static 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 | |
| 1055 | static 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. Berrange | 57b0cdf | 2017-10-09 15:34:06 +0100 | [diff] [blame] | 1065 | if (ioc->encoutput.offset) { |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1066 | 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 | |
| 1085 | static 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 Habkost | e79ea67 | 2017-06-08 10:39:04 -0300 | [diff] [blame] | 1098 | error_propagate(errp, error_copy(wioc->io_err)); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1099 | 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 | |
| 1131 | static 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. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 1139 | ssize_t want = iov_size(iov, niov); |
| 1140 | ssize_t avail; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1141 | ssize_t ret; |
| 1142 | |
| 1143 | if (wioc->io_err) { |
Eduardo Habkost | e79ea67 | 2017-06-08 10:39:04 -0300 | [diff] [blame] | 1144 | error_propagate(errp, error_copy(wioc->io_err)); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1145 | return -1; |
| 1146 | } |
| 1147 | |
| 1148 | if (wioc->io_eof) { |
| 1149 | error_setg(errp, "%s", "Broken pipe"); |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 1153 | 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 1159 | if (want) { |
Daniel P. Berrange | fb74e59 | 2017-10-09 16:33:20 +0100 | [diff] [blame] | 1160 | qio_channel_websock_encode(wioc, |
| 1161 | QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME, |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 1162 | iov, niov, want); |
Daniel P. Berrange | bac6c95 | 2017-10-09 14:39:17 +0100 | [diff] [blame] | 1163 | } |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 1164 | |
| 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1168 | 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. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 1177 | if (want == 0) { |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1178 | return QIO_CHANNEL_ERR_BLOCK; |
| 1179 | } |
| 1180 | |
Daniel P. Berrange | 8dfd5f9 | 2017-10-09 16:54:07 +0100 | [diff] [blame] | 1181 | return want; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | static 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 | |
| 1194 | static 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 | |
| 1202 | static 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 | |
| 1210 | static 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 | |
| 1219 | static int qio_channel_websock_close(QIOChannel *ioc, |
| 1220 | Error **errp) |
| 1221 | { |
| 1222 | QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc); |
| 1223 | |
Daniel P. Berrange | 59f183b | 2017-09-21 11:00:47 +0100 | [diff] [blame] | 1224 | trace_qio_channel_websock_close(ioc); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1225 | return qio_channel_close(wioc->master, errp); |
| 1226 | } |
| 1227 | |
| 1228 | typedef struct QIOChannelWebsockSource QIOChannelWebsockSource; |
| 1229 | struct QIOChannelWebsockSource { |
| 1230 | GSource parent; |
| 1231 | QIOChannelWebsock *wioc; |
| 1232 | GIOCondition condition; |
| 1233 | }; |
| 1234 | |
| 1235 | static gboolean |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 1236 | qio_channel_websock_source_check(GSource *source) |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1237 | { |
| 1238 | QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source; |
| 1239 | GIOCondition cond = 0; |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1240 | |
Daniel P. Berrangé | dd154c4 | 2019-02-20 18:22:39 +0000 | [diff] [blame] | 1241 | if (wsource->wioc->rawinput.offset) { |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1242 | cond |= G_IO_IN; |
| 1243 | } |
Daniel P. Berrange | a7b20a8 | 2017-10-09 14:43:42 +0100 | [diff] [blame] | 1244 | if (wsource->wioc->encoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) { |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1245 | cond |= G_IO_OUT; |
| 1246 | } |
Daniel P. Berrangé | dd154c4 | 2019-02-20 18:22:39 +0000 | [diff] [blame] | 1247 | 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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1253 | |
| 1254 | return cond & wsource->condition; |
| 1255 | } |
| 1256 | |
| 1257 | static gboolean |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 1258 | qio_channel_websock_source_prepare(GSource *source, |
| 1259 | gint *timeout) |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1260 | { |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 1261 | *timeout = -1; |
| 1262 | return qio_channel_websock_source_check(source); |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | static gboolean |
| 1266 | qio_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. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1272 | |
| 1273 | return (*func)(QIO_CHANNEL(wsource->wioc), |
Brandon Carpenter | eefa3d8 | 2017-09-12 08:21:48 -0700 | [diff] [blame] | 1274 | qio_channel_websock_source_check(source), |
Daniel P. Berrange | 2d1d0e7 | 2015-03-04 15:57:41 +0000 | [diff] [blame] | 1275 | user_data); |
| 1276 | } |
| 1277 | |
| 1278 | static void |
| 1279 | qio_channel_websock_source_finalize(GSource *source) |
| 1280 | { |
| 1281 | QIOChannelWebsockSource *ssource = (QIOChannelWebsockSource *)source; |
| 1282 | |
| 1283 | object_unref(OBJECT(ssource->wioc)); |
| 1284 | } |
| 1285 | |
| 1286 | GSourceFuncs qio_channel_websock_source_funcs = { |
| 1287 | qio_channel_websock_source_prepare, |
| 1288 | qio_channel_websock_source_check, |
| 1289 | qio_channel_websock_source_dispatch, |
| 1290 | qio_channel_websock_source_finalize |
| 1291 | }; |
| 1292 | |
| 1293 | static 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 | |
| 1313 | static 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 | |
| 1328 | static const TypeInfo qio_channel_websock_info = { |
| 1329 | .parent = TYPE_QIO_CHANNEL, |
| 1330 | .name = TYPE_QIO_CHANNEL_WEBSOCK, |
| 1331 | .instance_size = sizeof(QIOChannelWebsock), |
| 1332 | .instance_finalize = qio_channel_websock_finalize, |
| 1333 | .class_init = qio_channel_websock_class_init, |
| 1334 | }; |
| 1335 | |
| 1336 | static void qio_channel_websock_register_types(void) |
| 1337 | { |
| 1338 | type_register_static(&qio_channel_websock_info); |
| 1339 | } |
| 1340 | |
| 1341 | type_init(qio_channel_websock_register_types); |