2015-03-04 23:57:41 +08:00
|
|
|
/*
|
|
|
|
* QEMU I/O channels driver websockets
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-30 01:50:03 +08:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2016-03-16 00:22:36 +08:00
|
|
|
#include "qemu/bswap.h"
|
2015-03-04 23:57:41 +08:00
|
|
|
#include "io/channel-websock.h"
|
|
|
|
#include "crypto/hash.h"
|
|
|
|
#include "trace.h"
|
2017-10-09 23:54:07 +08:00
|
|
|
#include "qemu/iov.h"
|
2015-03-04 23:57:41 +08:00
|
|
|
|
2017-10-09 21:43:42 +08:00
|
|
|
/* Max amount to allow in rawinput/encoutput buffers */
|
2015-03-04 23:57:41 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_MAX_BUFFER 8192
|
|
|
|
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN 24
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
|
|
|
|
|
2017-02-28 18:37:24 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE "upgrade"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_HOST "host"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION "connection"
|
2015-03-04 23:57:41 +08:00
|
|
|
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
|
2017-02-28 18:37:24 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE "Upgrade"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET "websocket"
|
2015-03-04 23:57:41 +08:00
|
|
|
|
2017-09-06 18:33:17 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
|
|
|
|
"Server: QEMU VNC\r\n" \
|
|
|
|
"Date: %s\r\n"
|
|
|
|
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK \
|
2015-03-04 23:57:41 +08:00
|
|
|
"HTTP/1.1 101 Switching Protocols\r\n" \
|
2017-09-06 18:33:17 +08:00
|
|
|
QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
|
2015-03-04 23:57:41 +08:00
|
|
|
"Upgrade: websocket\r\n" \
|
|
|
|
"Connection: Upgrade\r\n" \
|
|
|
|
"Sec-WebSocket-Accept: %s\r\n" \
|
|
|
|
"Sec-WebSocket-Protocol: binary\r\n" \
|
|
|
|
"\r\n"
|
2017-09-06 18:33:17 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND \
|
|
|
|
"HTTP/1.1 404 Not Found\r\n" \
|
|
|
|
QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
|
|
|
|
"Connection: close\r\n" \
|
|
|
|
"\r\n"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST \
|
|
|
|
"HTTP/1.1 400 Bad Request\r\n" \
|
|
|
|
QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
|
|
|
|
"Connection: close\r\n" \
|
|
|
|
"Sec-WebSocket-Version: " \
|
|
|
|
QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION \
|
|
|
|
"\r\n"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR \
|
|
|
|
"HTTP/1.1 500 Internal Server Error\r\n" \
|
|
|
|
QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
|
|
|
|
"Connection: close\r\n" \
|
|
|
|
"\r\n"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE \
|
|
|
|
"HTTP/1.1 403 Request Entity Too Large\r\n" \
|
|
|
|
QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_COMMON \
|
|
|
|
"Connection: close\r\n" \
|
|
|
|
"\r\n"
|
2015-03-04 23:57:41 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM "\r\n"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HANDSHAKE_END "\r\n\r\n"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION "13"
|
2017-02-28 18:37:24 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_HTTP_METHOD "GET"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HTTP_PATH "/"
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HTTP_VERSION "HTTP/1.1"
|
2015-03-04 23:57:41 +08:00
|
|
|
|
|
|
|
/* The websockets packet header is variable length
|
|
|
|
* depending on the size of the payload... */
|
|
|
|
|
|
|
|
/* ...length when using 7-bit payload length */
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT 6
|
|
|
|
/* ...length when using 16-bit payload length */
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT 8
|
|
|
|
/* ...length when using 64-bit payload length */
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT 14
|
|
|
|
|
|
|
|
/* Length of the optional data mask field in header */
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK 4
|
|
|
|
|
|
|
|
/* Maximum length that can fit in 7-bit payload size */
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT 126
|
|
|
|
/* Maximum length that can fit in 16-bit payload size */
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT 65536
|
|
|
|
|
|
|
|
/* Magic 7-bit length to indicate use of 16-bit payload length */
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT 126
|
|
|
|
/* Magic 7-bit length to indicate use of 64-bit payload length */
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT 127
|
|
|
|
|
2017-09-12 23:21:48 +08:00
|
|
|
/* Bitmasks for accessing header fields */
|
2015-03-04 23:57:41 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN 0x80
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80
|
|
|
|
#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f
|
2017-09-12 23:21:51 +08:00
|
|
|
#define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8
|
2015-03-04 23:57:41 +08:00
|
|
|
|
|
|
|
typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader;
|
|
|
|
|
|
|
|
struct QEMU_PACKED QIOChannelWebsockHeader {
|
|
|
|
unsigned char b0;
|
|
|
|
unsigned char b1;
|
|
|
|
union {
|
|
|
|
struct QEMU_PACKED {
|
|
|
|
uint16_t l16;
|
|
|
|
QIOChannelWebsockMask m16;
|
|
|
|
} s16;
|
|
|
|
struct QEMU_PACKED {
|
|
|
|
uint64_t l64;
|
|
|
|
QIOChannelWebsockMask m64;
|
|
|
|
} s64;
|
|
|
|
QIOChannelWebsockMask m;
|
|
|
|
} u;
|
|
|
|
};
|
|
|
|
|
2017-02-28 18:37:24 +08:00
|
|
|
typedef struct QIOChannelWebsockHTTPHeader QIOChannelWebsockHTTPHeader;
|
|
|
|
|
|
|
|
struct QIOChannelWebsockHTTPHeader {
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
};
|
|
|
|
|
2015-03-04 23:57:41 +08:00
|
|
|
enum {
|
|
|
|
QIO_CHANNEL_WEBSOCK_OPCODE_CONTINUATION = 0x0,
|
|
|
|
QIO_CHANNEL_WEBSOCK_OPCODE_TEXT_FRAME = 0x1,
|
|
|
|
QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME = 0x2,
|
|
|
|
QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE = 0x8,
|
|
|
|
QIO_CHANNEL_WEBSOCK_OPCODE_PING = 0x9,
|
|
|
|
QIO_CHANNEL_WEBSOCK_OPCODE_PONG = 0xA
|
|
|
|
};
|
|
|
|
|
2017-10-07 22:56:09 +08:00
|
|
|
static void GCC_FMT_ATTR(2, 3)
|
|
|
|
qio_channel_websock_handshake_send_res(QIOChannelWebsock *ioc,
|
|
|
|
const char *resmsg,
|
|
|
|
...)
|
2017-09-06 18:33:17 +08:00
|
|
|
{
|
|
|
|
va_list vargs;
|
|
|
|
char *response;
|
|
|
|
size_t responselen;
|
|
|
|
|
|
|
|
va_start(vargs, resmsg);
|
|
|
|
response = g_strdup_vprintf(resmsg, vargs);
|
|
|
|
responselen = strlen(response);
|
|
|
|
buffer_reserve(&ioc->encoutput, responselen);
|
|
|
|
buffer_append(&ioc->encoutput, response, responselen);
|
|
|
|
va_end(vargs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *qio_channel_websock_date_str(void)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
char datebuf[128];
|
|
|
|
|
|
|
|
gmtime_r(&now, &tm);
|
|
|
|
|
|
|
|
strftime(datebuf, sizeof(datebuf), "%a, %d %b %Y %H:%M:%S GMT", &tm);
|
|
|
|
|
|
|
|
return g_strdup(datebuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qio_channel_websock_handshake_send_res_err(QIOChannelWebsock *ioc,
|
|
|
|
const char *resdata)
|
|
|
|
{
|
|
|
|
char *date = qio_channel_websock_date_str();
|
|
|
|
qio_channel_websock_handshake_send_res(ioc, resdata, date);
|
|
|
|
g_free(date);
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:21:53 +08:00
|
|
|
enum {
|
|
|
|
QIO_CHANNEL_WEBSOCK_STATUS_NORMAL = 1000,
|
|
|
|
QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR = 1002,
|
|
|
|
QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA = 1003,
|
|
|
|
QIO_CHANNEL_WEBSOCK_STATUS_POLICY = 1008,
|
|
|
|
QIO_CHANNEL_WEBSOCK_STATUS_TOO_LARGE = 1009,
|
|
|
|
QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR = 1011,
|
|
|
|
};
|
|
|
|
|
2017-02-28 18:37:24 +08:00
|
|
|
static size_t
|
2017-09-06 18:33:17 +08:00
|
|
|
qio_channel_websock_extract_headers(QIOChannelWebsock *ioc,
|
|
|
|
char *buffer,
|
2017-02-28 18:37:24 +08:00
|
|
|
QIOChannelWebsockHTTPHeader *hdrs,
|
|
|
|
size_t nhdrsalloc,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
char *nl, *sep, *tmp;
|
|
|
|
size_t nhdrs = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First parse the HTTP protocol greeting of format:
|
|
|
|
*
|
|
|
|
* $METHOD $PATH $VERSION
|
|
|
|
*
|
|
|
|
* e.g.
|
|
|
|
*
|
|
|
|
* GET / HTTP/1.1
|
|
|
|
*/
|
|
|
|
|
|
|
|
nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
|
|
|
|
if (!nl) {
|
|
|
|
error_setg(errp, "Missing HTTP header delimiter");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
*nl = '\0';
|
2017-10-10 20:28:03 +08:00
|
|
|
trace_qio_channel_websock_http_greeting(ioc, buffer);
|
2017-02-28 18:37:24 +08:00
|
|
|
|
|
|
|
tmp = strchr(buffer, ' ');
|
|
|
|
if (!tmp) {
|
|
|
|
error_setg(errp, "Missing HTTP path delimiter");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*tmp = '\0';
|
|
|
|
|
|
|
|
if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_METHOD)) {
|
|
|
|
error_setg(errp, "Unsupported HTTP method %s", buffer);
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = tmp + 1;
|
|
|
|
tmp = strchr(buffer, ' ');
|
|
|
|
if (!tmp) {
|
|
|
|
error_setg(errp, "Missing HTTP version delimiter");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
*tmp = '\0';
|
|
|
|
|
|
|
|
if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_PATH)) {
|
2017-09-06 18:33:17 +08:00
|
|
|
qio_channel_websock_handshake_send_res_err(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_NOT_FOUND);
|
2017-02-28 18:37:24 +08:00
|
|
|
error_setg(errp, "Unexpected HTTP path %s", buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = tmp + 1;
|
|
|
|
|
|
|
|
if (!g_str_equal(buffer, QIO_CHANNEL_WEBSOCK_HTTP_VERSION)) {
|
|
|
|
error_setg(errp, "Unsupported HTTP version %s", buffer);
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now parse all the header fields of format
|
|
|
|
*
|
|
|
|
* $NAME: $VALUE
|
|
|
|
*
|
|
|
|
* e.g.
|
|
|
|
*
|
|
|
|
* Cache-control: no-cache
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
QIOChannelWebsockHTTPHeader *hdr;
|
|
|
|
|
|
|
|
nl = strstr(buffer, QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
|
|
|
|
if (nl) {
|
|
|
|
*nl = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
sep = strchr(buffer, ':');
|
|
|
|
if (!sep) {
|
|
|
|
error_setg(errp, "Malformed HTTP header");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
*sep = '\0';
|
|
|
|
sep++;
|
|
|
|
while (*sep == ' ') {
|
|
|
|
sep++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nhdrs >= nhdrsalloc) {
|
|
|
|
error_setg(errp, "Too many HTTP headers");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr = &hdrs[nhdrs++];
|
|
|
|
hdr->name = buffer;
|
|
|
|
hdr->value = sep;
|
|
|
|
|
|
|
|
/* Canonicalize header name for easier identification later */
|
|
|
|
for (tmp = hdr->name; *tmp; tmp++) {
|
|
|
|
*tmp = g_ascii_tolower(*tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nl) {
|
|
|
|
buffer = nl + strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_DELIM);
|
|
|
|
}
|
|
|
|
} while (nl != NULL);
|
|
|
|
|
|
|
|
return nhdrs;
|
2017-09-06 18:33:17 +08:00
|
|
|
|
|
|
|
bad_request:
|
|
|
|
qio_channel_websock_handshake_send_res_err(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST);
|
|
|
|
return 0;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
qio_channel_websock_find_header(QIOChannelWebsockHTTPHeader *hdrs,
|
|
|
|
size_t nhdrs,
|
|
|
|
const char *name)
|
2015-03-04 23:57:41 +08:00
|
|
|
{
|
2017-02-28 18:37:24 +08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < nhdrs; i++) {
|
|
|
|
if (g_str_equal(hdrs[i].name, name)) {
|
|
|
|
return hdrs[i].value;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-28 18:37:24 +08:00
|
|
|
|
|
|
|
return NULL;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 18:33:17 +08:00
|
|
|
static void qio_channel_websock_handshake_send_res_ok(QIOChannelWebsock *ioc,
|
|
|
|
const char *key,
|
|
|
|
Error **errp)
|
2015-03-04 23:57:41 +08:00
|
|
|
{
|
|
|
|
char combined_key[QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
|
|
|
|
QIO_CHANNEL_WEBSOCK_GUID_LEN + 1];
|
2017-09-06 18:33:17 +08:00
|
|
|
char *accept = NULL;
|
2017-10-11 23:38:10 +08:00
|
|
|
char *date = NULL;
|
2015-03-04 23:57:41 +08:00
|
|
|
|
|
|
|
g_strlcpy(combined_key, key, QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN + 1);
|
|
|
|
g_strlcat(combined_key, QIO_CHANNEL_WEBSOCK_GUID,
|
|
|
|
QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
|
|
|
|
QIO_CHANNEL_WEBSOCK_GUID_LEN + 1);
|
|
|
|
|
|
|
|
/* hash and encode it */
|
|
|
|
if (qcrypto_hash_base64(QCRYPTO_HASH_ALG_SHA1,
|
|
|
|
combined_key,
|
|
|
|
QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN +
|
|
|
|
QIO_CHANNEL_WEBSOCK_GUID_LEN,
|
|
|
|
&accept,
|
|
|
|
errp) < 0) {
|
2017-09-06 18:33:17 +08:00
|
|
|
qio_channel_websock_handshake_send_res_err(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_SERVER_ERR);
|
|
|
|
return;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 23:38:10 +08:00
|
|
|
date = qio_channel_websock_date_str();
|
2017-09-06 18:33:17 +08:00
|
|
|
qio_channel_websock_handshake_send_res(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_OK, date, accept);
|
2015-03-04 23:57:41 +08:00
|
|
|
|
2017-09-06 18:33:17 +08:00
|
|
|
g_free(date);
|
2015-03-04 23:57:41 +08:00
|
|
|
g_free(accept);
|
|
|
|
}
|
|
|
|
|
2017-09-06 18:33:17 +08:00
|
|
|
static void qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
|
|
|
|
char *buffer,
|
|
|
|
Error **errp)
|
2015-03-04 23:57:41 +08:00
|
|
|
{
|
2017-02-28 18:37:24 +08:00
|
|
|
QIOChannelWebsockHTTPHeader hdrs[32];
|
|
|
|
size_t nhdrs = G_N_ELEMENTS(hdrs);
|
|
|
|
const char *protocols = NULL, *version = NULL, *key = NULL,
|
|
|
|
*host = NULL, *connection = NULL, *upgrade = NULL;
|
2017-10-10 00:52:28 +08:00
|
|
|
char **connectionv;
|
|
|
|
bool upgraded = false;
|
|
|
|
size_t i;
|
2015-03-04 23:57:41 +08:00
|
|
|
|
2017-09-06 18:33:17 +08:00
|
|
|
nhdrs = qio_channel_websock_extract_headers(ioc, buffer, hdrs, nhdrs, errp);
|
2017-02-28 18:37:24 +08:00
|
|
|
if (!nhdrs) {
|
2017-09-06 18:33:17 +08:00
|
|
|
return;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protocols = qio_channel_websock_find_header(
|
|
|
|
hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL);
|
2015-03-04 23:57:41 +08:00
|
|
|
if (!protocols) {
|
|
|
|
error_setg(errp, "Missing websocket protocol header data");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-28 18:37:24 +08:00
|
|
|
version = qio_channel_websock_find_header(
|
|
|
|
hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_VERSION);
|
2015-03-04 23:57:41 +08:00
|
|
|
if (!version) {
|
|
|
|
error_setg(errp, "Missing websocket version header data");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-28 18:37:24 +08:00
|
|
|
key = qio_channel_websock_find_header(
|
|
|
|
hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_KEY);
|
2015-03-04 23:57:41 +08:00
|
|
|
if (!key) {
|
|
|
|
error_setg(errp, "Missing websocket key header data");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
host = qio_channel_websock_find_header(
|
|
|
|
hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_HOST);
|
|
|
|
if (!host) {
|
|
|
|
error_setg(errp, "Missing websocket host header data");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
connection = qio_channel_websock_find_header(
|
|
|
|
hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_CONNECTION);
|
|
|
|
if (!connection) {
|
|
|
|
error_setg(errp, "Missing websocket connection header data");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
upgrade = qio_channel_websock_find_header(
|
|
|
|
hdrs, nhdrs, QIO_CHANNEL_WEBSOCK_HEADER_UPGRADE);
|
|
|
|
if (!upgrade) {
|
|
|
|
error_setg(errp, "Missing websocket upgrade header data");
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
2017-10-10 20:28:03 +08:00
|
|
|
trace_qio_channel_websock_http_request(ioc, protocols, version,
|
|
|
|
host, connection, upgrade, key);
|
|
|
|
|
2015-03-04 23:57:41 +08:00
|
|
|
if (!g_strrstr(protocols, QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY)) {
|
|
|
|
error_setg(errp, "No '%s' protocol is supported by client '%s'",
|
|
|
|
QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY, protocols);
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_str_equal(version, QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION)) {
|
|
|
|
error_setg(errp, "Version '%s' is not supported by client '%s'",
|
|
|
|
QIO_CHANNEL_WEBSOCK_SUPPORTED_VERSION, version);
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(key) != QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN) {
|
|
|
|
error_setg(errp, "Key length '%zu' was not as expected '%d'",
|
|
|
|
strlen(key), QIO_CHANNEL_WEBSOCK_CLIENT_KEY_LEN);
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
2017-10-10 00:52:28 +08:00
|
|
|
connectionv = g_strsplit(connection, ",", 0);
|
|
|
|
for (i = 0; connectionv != NULL && connectionv[i] != NULL; i++) {
|
|
|
|
g_strstrip(connectionv[i]);
|
|
|
|
if (strcasecmp(connectionv[i],
|
|
|
|
QIO_CHANNEL_WEBSOCK_CONNECTION_UPGRADE) == 0) {
|
|
|
|
upgraded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev(connectionv);
|
|
|
|
if (!upgraded) {
|
2017-02-28 18:37:24 +08:00
|
|
|
error_setg(errp, "No connection upgrade requested '%s'", connection);
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 21:49:41 +08:00
|
|
|
if (strcasecmp(upgrade, QIO_CHANNEL_WEBSOCK_UPGRADE_WEBSOCKET) != 0) {
|
2017-02-28 18:37:24 +08:00
|
|
|
error_setg(errp, "Incorrect upgrade method '%s'", upgrade);
|
2017-09-06 18:33:17 +08:00
|
|
|
goto bad_request;
|
2017-02-28 18:37:24 +08:00
|
|
|
}
|
2015-03-04 23:57:41 +08:00
|
|
|
|
2017-09-06 18:33:17 +08:00
|
|
|
qio_channel_websock_handshake_send_res_ok(ioc, key, errp);
|
|
|
|
return;
|
|
|
|
|
|
|
|
bad_request:
|
|
|
|
qio_channel_websock_handshake_send_res_err(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_BAD_REQUEST);
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
char *handshake_end;
|
|
|
|
ssize_t ret;
|
|
|
|
/* Typical HTTP headers from novnc are 512 bytes, so limiting
|
|
|
|
* total header size to 4096 is easily enough. */
|
|
|
|
size_t want = 4096 - ioc->encinput.offset;
|
|
|
|
buffer_reserve(&ioc->encinput, want);
|
|
|
|
ret = qio_channel_read(ioc->master,
|
|
|
|
(char *)buffer_end(&ioc->encinput), want, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ioc->encinput.offset += ret;
|
|
|
|
|
|
|
|
handshake_end = g_strstr_len((char *)ioc->encinput.buffer,
|
|
|
|
ioc->encinput.offset,
|
|
|
|
QIO_CHANNEL_WEBSOCK_HANDSHAKE_END);
|
|
|
|
if (!handshake_end) {
|
|
|
|
if (ioc->encinput.offset >= 4096) {
|
2017-09-06 18:33:17 +08:00
|
|
|
qio_channel_websock_handshake_send_res_err(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_HANDSHAKE_RES_TOO_LARGE);
|
2015-03-04 23:57:41 +08:00
|
|
|
error_setg(errp,
|
|
|
|
"End of headers not found in first 4096 bytes");
|
2017-09-06 18:33:17 +08:00
|
|
|
return 1;
|
2015-03-04 23:57:41 +08:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 18:37:24 +08:00
|
|
|
*handshake_end = '\0';
|
2015-03-04 23:57:41 +08:00
|
|
|
|
2017-09-06 18:33:17 +08:00
|
|
|
qio_channel_websock_handshake_process(ioc,
|
|
|
|
(char *)ioc->encinput.buffer,
|
|
|
|
errp);
|
2015-03-04 23:57:41 +08:00
|
|
|
|
|
|
|
buffer_advance(&ioc->encinput,
|
|
|
|
handshake_end - (char *)ioc->encinput.buffer +
|
|
|
|
strlen(QIO_CHANNEL_WEBSOCK_HANDSHAKE_END));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean qio_channel_websock_handshake_send(QIOChannel *ioc,
|
|
|
|
GIOCondition condition,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
QIOTask *task = user_data;
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(
|
|
|
|
qio_task_get_source(task));
|
|
|
|
Error *err = NULL;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = qio_channel_write(wioc->master,
|
|
|
|
(char *)wioc->encoutput.buffer,
|
|
|
|
wioc->encoutput.offset,
|
|
|
|
&err);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2017-09-06 18:38:36 +08:00
|
|
|
trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
|
2016-08-11 22:20:58 +08:00
|
|
|
qio_task_set_error(task, err);
|
|
|
|
qio_task_complete(task);
|
2015-03-04 23:57:41 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_advance(&wioc->encoutput, ret);
|
|
|
|
if (wioc->encoutput.offset == 0) {
|
2017-09-06 18:33:17 +08:00
|
|
|
if (wioc->io_err) {
|
2017-09-06 18:38:36 +08:00
|
|
|
trace_qio_channel_websock_handshake_fail(
|
|
|
|
ioc, error_get_pretty(wioc->io_err));
|
2017-09-06 18:33:17 +08:00
|
|
|
qio_task_set_error(task, wioc->io_err);
|
|
|
|
wioc->io_err = NULL;
|
|
|
|
qio_task_complete(task);
|
|
|
|
} else {
|
|
|
|
trace_qio_channel_websock_handshake_complete(ioc);
|
|
|
|
qio_task_complete(task);
|
|
|
|
}
|
2015-03-04 23:57:41 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
trace_qio_channel_websock_handshake_pending(ioc, G_IO_OUT);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean qio_channel_websock_handshake_io(QIOChannel *ioc,
|
|
|
|
GIOCondition condition,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
QIOTask *task = user_data;
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(
|
|
|
|
qio_task_get_source(task));
|
|
|
|
Error *err = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = qio_channel_websock_handshake_read(wioc, &err);
|
|
|
|
if (ret < 0) {
|
2017-09-06 18:33:17 +08:00
|
|
|
/*
|
|
|
|
* We only take this path on a fatal I/O error reading from
|
|
|
|
* client connection, as most of the time we have an
|
|
|
|
* HTTP 4xx err response to send instead
|
|
|
|
*/
|
2017-09-06 18:38:36 +08:00
|
|
|
trace_qio_channel_websock_handshake_fail(ioc, error_get_pretty(err));
|
2016-08-11 22:20:58 +08:00
|
|
|
qio_task_set_error(task, err);
|
|
|
|
qio_task_complete(task);
|
2015-03-04 23:57:41 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
trace_qio_channel_websock_handshake_pending(ioc, G_IO_IN);
|
|
|
|
/* need more data still */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-09-06 18:33:17 +08:00
|
|
|
if (err) {
|
|
|
|
error_propagate(&wioc->io_err, err);
|
|
|
|
}
|
|
|
|
|
2015-03-04 23:57:41 +08:00
|
|
|
trace_qio_channel_websock_handshake_reply(ioc);
|
|
|
|
qio_channel_add_watch(
|
|
|
|
wioc->master,
|
|
|
|
G_IO_OUT,
|
|
|
|
qio_channel_websock_handshake_send,
|
|
|
|
task,
|
2016-06-07 19:27:51 +08:00
|
|
|
NULL);
|
2015-03-04 23:57:41 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-09 23:33:20 +08:00
|
|
|
static void qio_channel_websock_encode(QIOChannelWebsock *ioc,
|
|
|
|
uint8_t opcode,
|
|
|
|
const struct iovec *iov,
|
|
|
|
size_t niov,
|
|
|
|
size_t size)
|
2015-03-04 23:57:41 +08:00
|
|
|
{
|
|
|
|
size_t header_size;
|
2017-10-09 23:33:20 +08:00
|
|
|
size_t i;
|
2015-03-04 23:57:41 +08:00
|
|
|
union {
|
|
|
|
char buf[QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT];
|
|
|
|
QIOChannelWebsockHeader ws;
|
|
|
|
} header;
|
|
|
|
|
2017-10-09 23:33:20 +08:00
|
|
|
assert(size <= iov_size(iov, niov));
|
|
|
|
|
2017-09-12 23:21:48 +08:00
|
|
|
header.ws.b0 = QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN |
|
2017-09-12 23:21:52 +08:00
|
|
|
(opcode & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE);
|
2017-10-09 23:33:20 +08:00
|
|
|
if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) {
|
|
|
|
header.ws.b1 = (uint8_t)size;
|
2015-03-04 23:57:41 +08:00
|
|
|
header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;
|
2017-10-09 23:33:20 +08:00
|
|
|
} else if (size < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) {
|
2015-03-04 23:57:41 +08:00
|
|
|
header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT;
|
2017-10-09 23:33:20 +08:00
|
|
|
header.ws.u.s16.l16 = cpu_to_be16((uint16_t)size);
|
2015-03-04 23:57:41 +08:00
|
|
|
header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;
|
|
|
|
} else {
|
|
|
|
header.ws.b1 = QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT;
|
2017-10-09 23:33:20 +08:00
|
|
|
header.ws.u.s64.l64 = cpu_to_be64(size);
|
2015-03-04 23:57:41 +08:00
|
|
|
header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;
|
|
|
|
}
|
|
|
|
header_size -= QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK;
|
|
|
|
|
2017-10-09 23:33:20 +08:00
|
|
|
trace_qio_channel_websock_encode(ioc, opcode, header_size, size);
|
|
|
|
buffer_reserve(&ioc->encoutput, header_size + size);
|
|
|
|
buffer_append(&ioc->encoutput, header.buf, header_size);
|
|
|
|
for (i = 0; i < niov && size != 0; i++) {
|
|
|
|
size_t want = iov[i].iov_len;
|
|
|
|
if (want > size) {
|
|
|
|
want = size;
|
|
|
|
}
|
|
|
|
buffer_append(&ioc->encoutput, iov[i].iov_base, want);
|
|
|
|
size -= want;
|
|
|
|
}
|
2017-09-12 23:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-12 23:21:53 +08:00
|
|
|
static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *, Error **);
|
|
|
|
|
|
|
|
|
|
|
|
static void qio_channel_websock_write_close(QIOChannelWebsock *ioc,
|
|
|
|
uint16_t code, const char *reason)
|
|
|
|
{
|
2017-10-09 23:54:07 +08:00
|
|
|
struct iovec iov[2] = {
|
|
|
|
{ .iov_base = &code, .iov_len = sizeof(code) },
|
|
|
|
};
|
|
|
|
size_t niov = 1;
|
|
|
|
size_t size = iov[0].iov_len;
|
|
|
|
|
|
|
|
cpu_to_be16s(&code);
|
|
|
|
|
2017-09-12 23:21:53 +08:00
|
|
|
if (reason) {
|
2017-10-09 23:54:07 +08:00
|
|
|
iov[1].iov_base = (void *)reason;
|
|
|
|
iov[1].iov_len = strlen(reason);
|
|
|
|
size += iov[1].iov_len;
|
|
|
|
niov++;
|
2017-09-12 23:21:53 +08:00
|
|
|
}
|
2017-10-09 23:33:20 +08:00
|
|
|
qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE,
|
2017-10-09 23:54:07 +08:00
|
|
|
iov, niov, size);
|
2017-09-12 23:21:53 +08:00
|
|
|
qio_channel_websock_write_wire(ioc, NULL);
|
|
|
|
qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-12 23:21:48 +08:00
|
|
|
static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc,
|
|
|
|
Error **errp)
|
2015-03-04 23:57:41 +08:00
|
|
|
{
|
|
|
|
unsigned char opcode, fin, has_mask;
|
|
|
|
size_t header_size;
|
|
|
|
size_t payload_len;
|
|
|
|
QIOChannelWebsockHeader *header =
|
|
|
|
(QIOChannelWebsockHeader *)ioc->encinput.buffer;
|
|
|
|
|
|
|
|
if (ioc->payload_remain) {
|
|
|
|
error_setg(errp,
|
|
|
|
"Decoding header but %zu bytes of payload remain",
|
|
|
|
ioc->payload_remain);
|
2017-09-12 23:21:53 +08:00
|
|
|
qio_channel_websock_write_close(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR,
|
|
|
|
"internal server error");
|
2015-03-04 23:57:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT) {
|
|
|
|
/* header not complete */
|
|
|
|
return QIO_CHANNEL_ERR_BLOCK;
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:21:48 +08:00
|
|
|
fin = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN;
|
2015-03-04 23:57:41 +08:00
|
|
|
opcode = header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE;
|
2017-09-12 23:21:48 +08:00
|
|
|
has_mask = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK;
|
2015-03-04 23:57:41 +08:00
|
|
|
payload_len = header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN;
|
|
|
|
|
2017-09-12 23:21:49 +08:00
|
|
|
/* Save or restore opcode. */
|
|
|
|
if (opcode) {
|
|
|
|
ioc->opcode = opcode;
|
|
|
|
} else {
|
|
|
|
opcode = ioc->opcode;
|
|
|
|
}
|
|
|
|
|
2017-09-21 18:00:47 +08:00
|
|
|
trace_qio_channel_websock_header_partial_decode(ioc, payload_len,
|
|
|
|
fin, opcode, (int)has_mask);
|
|
|
|
|
2015-03-04 23:57:41 +08:00
|
|
|
if (opcode == QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) {
|
|
|
|
/* disconnect */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Websocket frame sanity check:
|
2017-09-12 23:21:49 +08:00
|
|
|
* * Fragmentation is only supported for binary frames.
|
|
|
|
* * All frames sent by a client MUST be masked.
|
2017-09-12 23:21:52 +08:00
|
|
|
* * Only binary and ping/pong encoding is supported.
|
2015-03-04 23:57:41 +08:00
|
|
|
*/
|
|
|
|
if (!fin) {
|
2017-09-12 23:21:49 +08:00
|
|
|
if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) {
|
|
|
|
error_setg(errp, "only binary websocket frames may be fragmented");
|
2017-09-12 23:21:53 +08:00
|
|
|
qio_channel_websock_write_close(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_STATUS_POLICY ,
|
|
|
|
"only binary frames may be fragmented");
|
2017-09-12 23:21:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-12 23:21:51 +08:00
|
|
|
if (opcode != QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME &&
|
2017-09-12 23:21:53 +08:00
|
|
|
opcode != QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE &&
|
2017-09-12 23:21:51 +08:00
|
|
|
opcode != QIO_CHANNEL_WEBSOCK_OPCODE_PING &&
|
|
|
|
opcode != QIO_CHANNEL_WEBSOCK_OPCODE_PONG) {
|
2017-09-12 23:21:53 +08:00
|
|
|
error_setg(errp, "unsupported opcode: %#04x; only binary, close, "
|
|
|
|
"ping, and pong websocket frames are supported", opcode);
|
|
|
|
qio_channel_websock_write_close(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA ,
|
|
|
|
"only binary, close, ping, and pong frames are supported");
|
2017-09-12 23:21:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
if (!has_mask) {
|
2017-09-12 23:21:48 +08:00
|
|
|
error_setg(errp, "client websocket frames must be masked");
|
2017-09-12 23:21:53 +08:00
|
|
|
qio_channel_websock_write_close(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR,
|
|
|
|
"client frames must be masked");
|
2015-03-04 23:57:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload_len < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT) {
|
|
|
|
ioc->payload_remain = payload_len;
|
|
|
|
header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT;
|
|
|
|
ioc->mask = header->u.m;
|
2017-09-12 23:21:51 +08:00
|
|
|
} else if (opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) {
|
|
|
|
error_setg(errp, "websocket control frame is too large");
|
2017-09-12 23:21:53 +08:00
|
|
|
qio_channel_websock_write_close(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR,
|
|
|
|
"control frame is too large");
|
2017-09-12 23:21:51 +08:00
|
|
|
return -1;
|
2015-03-04 23:57:41 +08:00
|
|
|
} else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT &&
|
|
|
|
ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT) {
|
|
|
|
ioc->payload_remain = be16_to_cpu(header->u.s16.l16);
|
|
|
|
header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT;
|
|
|
|
ioc->mask = header->u.s16.m16;
|
|
|
|
} else if (payload_len == QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT &&
|
|
|
|
ioc->encinput.offset >= QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT) {
|
|
|
|
ioc->payload_remain = be64_to_cpu(header->u.s64.l64);
|
|
|
|
header_size = QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT;
|
|
|
|
ioc->mask = header->u.s64.m64;
|
|
|
|
} else {
|
|
|
|
/* header not complete */
|
|
|
|
return QIO_CHANNEL_ERR_BLOCK;
|
|
|
|
}
|
|
|
|
|
2017-09-21 18:00:47 +08:00
|
|
|
trace_qio_channel_websock_header_full_decode(
|
|
|
|
ioc, header_size, ioc->payload_remain, ioc->mask.u);
|
2015-03-04 23:57:41 +08:00
|
|
|
buffer_advance(&ioc->encinput, header_size);
|
2017-09-12 23:21:53 +08:00
|
|
|
return 0;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-12 23:21:48 +08:00
|
|
|
static int qio_channel_websock_decode_payload(QIOChannelWebsock *ioc,
|
|
|
|
Error **errp)
|
2015-03-04 23:57:41 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
2017-09-12 23:21:50 +08:00
|
|
|
size_t payload_len = 0;
|
2015-03-04 23:57:41 +08:00
|
|
|
uint32_t *payload32;
|
|
|
|
|
2017-09-12 23:21:50 +08:00
|
|
|
if (ioc->payload_remain) {
|
|
|
|
/* If we aren't at the end of the payload, then drop
|
|
|
|
* off the last bytes, so we're always multiple of 4
|
|
|
|
* for purpose of unmasking, except at end of payload
|
|
|
|
*/
|
|
|
|
if (ioc->encinput.offset < ioc->payload_remain) {
|
2017-09-12 23:21:52 +08:00
|
|
|
/* Wait for the entire payload before processing control frames
|
|
|
|
* because the payload will most likely be echoed back. */
|
|
|
|
if (ioc->opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) {
|
|
|
|
return QIO_CHANNEL_ERR_BLOCK;
|
|
|
|
}
|
2017-09-12 23:21:50 +08:00
|
|
|
payload_len = ioc->encinput.offset - (ioc->encinput.offset % 4);
|
|
|
|
} else {
|
|
|
|
payload_len = ioc->payload_remain;
|
|
|
|
}
|
|
|
|
if (payload_len == 0) {
|
|
|
|
return QIO_CHANNEL_ERR_BLOCK;
|
|
|
|
}
|
2015-03-04 23:57:41 +08:00
|
|
|
|
2017-09-12 23:21:50 +08:00
|
|
|
ioc->payload_remain -= payload_len;
|
2015-03-04 23:57:41 +08:00
|
|
|
|
2017-09-12 23:21:50 +08:00
|
|
|
/* unmask frame */
|
|
|
|
/* process 1 frame (32 bit op) */
|
|
|
|
payload32 = (uint32_t *)ioc->encinput.buffer;
|
|
|
|
for (i = 0; i < payload_len / 4; i++) {
|
|
|
|
payload32[i] ^= ioc->mask.u;
|
|
|
|
}
|
|
|
|
/* process the remaining bytes (if any) */
|
|
|
|
for (i *= 4; i < payload_len; i++) {
|
|
|
|
ioc->encinput.buffer[i] ^= ioc->mask.c[i % 4];
|
|
|
|
}
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
2017-09-21 18:00:47 +08:00
|
|
|
trace_qio_channel_websock_payload_decode(
|
|
|
|
ioc, ioc->opcode, ioc->payload_remain);
|
|
|
|
|
2017-09-12 23:21:51 +08:00
|
|
|
if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) {
|
|
|
|
if (payload_len) {
|
2017-09-12 23:21:52 +08:00
|
|
|
/* binary frames are passed on */
|
2017-09-12 23:21:51 +08:00
|
|
|
buffer_reserve(&ioc->rawinput, payload_len);
|
|
|
|
buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len);
|
|
|
|
}
|
2017-09-12 23:21:53 +08:00
|
|
|
} else if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) {
|
|
|
|
/* close frames are echoed back */
|
|
|
|
error_setg(errp, "websocket closed by peer");
|
|
|
|
if (payload_len) {
|
|
|
|
/* echo client status */
|
2017-10-09 23:33:20 +08:00
|
|
|
struct iovec iov = { .iov_base = ioc->encinput.buffer,
|
|
|
|
.iov_len = ioc->encinput.offset };
|
|
|
|
qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE,
|
|
|
|
&iov, 1, iov.iov_len);
|
2017-09-12 23:21:53 +08:00
|
|
|
qio_channel_websock_write_wire(ioc, NULL);
|
|
|
|
qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
|
|
|
|
} else {
|
|
|
|
/* send our own status */
|
|
|
|
qio_channel_websock_write_close(
|
|
|
|
ioc, QIO_CHANNEL_WEBSOCK_STATUS_NORMAL, "peer requested close");
|
|
|
|
}
|
|
|
|
return -1;
|
2017-09-12 23:21:52 +08:00
|
|
|
} else if (ioc->opcode == QIO_CHANNEL_WEBSOCK_OPCODE_PING) {
|
2017-10-09 22:34:06 +08:00
|
|
|
/* ping frames produce an immediate reply, as long as we've not still
|
|
|
|
* got a previous pong queued, in which case we drop the new pong */
|
|
|
|
if (ioc->pong_remain == 0) {
|
2017-10-09 23:33:20 +08:00
|
|
|
struct iovec iov = { .iov_base = ioc->encinput.buffer,
|
|
|
|
.iov_len = ioc->encinput.offset };
|
|
|
|
qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_PONG,
|
|
|
|
&iov, 1, iov.iov_len);
|
2017-10-09 22:34:06 +08:00
|
|
|
ioc->pong_remain = ioc->encoutput.offset;
|
|
|
|
}
|
2017-09-12 23:21:52 +08:00
|
|
|
} /* pong frames are ignored */
|
2017-09-12 23:21:51 +08:00
|
|
|
|
2017-09-12 23:21:50 +08:00
|
|
|
if (payload_len) {
|
|
|
|
buffer_advance(&ioc->encinput, payload_len);
|
|
|
|
}
|
2017-09-12 23:21:48 +08:00
|
|
|
return 0;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QIOChannelWebsock *
|
|
|
|
qio_channel_websock_new_server(QIOChannel *master)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *wioc;
|
|
|
|
QIOChannel *ioc;
|
|
|
|
|
|
|
|
wioc = QIO_CHANNEL_WEBSOCK(object_new(TYPE_QIO_CHANNEL_WEBSOCK));
|
|
|
|
ioc = QIO_CHANNEL(wioc);
|
|
|
|
|
|
|
|
wioc->master = master;
|
2016-09-29 23:52:36 +08:00
|
|
|
if (qio_channel_has_feature(master, QIO_CHANNEL_FEATURE_SHUTDOWN)) {
|
2016-09-29 23:52:37 +08:00
|
|
|
qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
object_ref(OBJECT(master));
|
|
|
|
|
|
|
|
trace_qio_channel_websock_new_server(wioc, master);
|
|
|
|
return wioc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qio_channel_websock_handshake(QIOChannelWebsock *ioc,
|
|
|
|
QIOTaskFunc func,
|
|
|
|
gpointer opaque,
|
|
|
|
GDestroyNotify destroy)
|
|
|
|
{
|
|
|
|
QIOTask *task;
|
|
|
|
|
|
|
|
task = qio_task_new(OBJECT(ioc),
|
|
|
|
func,
|
|
|
|
opaque,
|
|
|
|
destroy);
|
|
|
|
|
|
|
|
trace_qio_channel_websock_handshake_start(ioc);
|
|
|
|
trace_qio_channel_websock_handshake_pending(ioc, G_IO_IN);
|
|
|
|
qio_channel_add_watch(ioc->master,
|
|
|
|
G_IO_IN,
|
|
|
|
qio_channel_websock_handshake_io,
|
|
|
|
task,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void qio_channel_websock_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *ioc = QIO_CHANNEL_WEBSOCK(obj);
|
|
|
|
|
|
|
|
buffer_free(&ioc->encinput);
|
|
|
|
buffer_free(&ioc->encoutput);
|
|
|
|
buffer_free(&ioc->rawinput);
|
|
|
|
object_unref(OBJECT(ioc->master));
|
|
|
|
if (ioc->io_tag) {
|
|
|
|
g_source_remove(ioc->io_tag);
|
|
|
|
}
|
|
|
|
if (ioc->io_err) {
|
|
|
|
error_free(ioc->io_err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (ioc->encinput.offset < 4096) {
|
|
|
|
size_t want = 4096 - ioc->encinput.offset;
|
|
|
|
|
|
|
|
buffer_reserve(&ioc->encinput, want);
|
|
|
|
ret = qio_channel_read(ioc->master,
|
|
|
|
(char *)ioc->encinput.buffer +
|
|
|
|
ioc->encinput.offset,
|
|
|
|
want,
|
|
|
|
errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-09-12 23:21:48 +08:00
|
|
|
if (ret == 0 && ioc->encinput.offset == 0) {
|
|
|
|
ioc->io_eof = TRUE;
|
2015-03-04 23:57:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ioc->encinput.offset += ret;
|
|
|
|
}
|
|
|
|
|
2017-01-28 02:11:32 +08:00
|
|
|
while (ioc->encinput.offset != 0) {
|
|
|
|
if (ioc->payload_remain == 0) {
|
|
|
|
ret = qio_channel_websock_decode_header(ioc, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = qio_channel_websock_decode_payload(ioc, errp);
|
2015-03-04 23:57:41 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2017-01-28 02:11:32 +08:00
|
|
|
return 1;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *ioc,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
ssize_t done = 0;
|
2017-09-12 23:21:52 +08:00
|
|
|
|
2015-03-04 23:57:41 +08:00
|
|
|
while (ioc->encoutput.offset > 0) {
|
|
|
|
ret = qio_channel_write(ioc->master,
|
|
|
|
(char *)ioc->encoutput.buffer,
|
|
|
|
ioc->encoutput.offset,
|
|
|
|
errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == QIO_CHANNEL_ERR_BLOCK &&
|
|
|
|
done > 0) {
|
|
|
|
return done;
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer_advance(&ioc->encoutput, ret);
|
|
|
|
done += ret;
|
2017-10-09 22:34:06 +08:00
|
|
|
if (ioc->pong_remain < ret) {
|
|
|
|
ioc->pong_remain = 0;
|
|
|
|
} else {
|
|
|
|
ioc->pong_remain -= ret;
|
|
|
|
}
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void qio_channel_websock_flush_free(gpointer user_data)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(user_data);
|
|
|
|
object_unref(OBJECT(wioc));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc);
|
|
|
|
|
|
|
|
static gboolean qio_channel_websock_flush(QIOChannel *ioc,
|
|
|
|
GIOCondition condition,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(user_data);
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (condition & G_IO_OUT) {
|
|
|
|
ret = qio_channel_websock_write_wire(wioc, &wioc->io_err);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (condition & G_IO_IN) {
|
|
|
|
ret = qio_channel_websock_read_wire(wioc, &wioc->io_err);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
qio_channel_websock_set_watch(wioc);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void qio_channel_websock_unset_watch(QIOChannelWebsock *ioc)
|
|
|
|
{
|
|
|
|
if (ioc->io_tag) {
|
|
|
|
g_source_remove(ioc->io_tag);
|
|
|
|
ioc->io_tag = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc)
|
|
|
|
{
|
|
|
|
GIOCondition cond = 0;
|
|
|
|
|
|
|
|
qio_channel_websock_unset_watch(ioc);
|
|
|
|
|
|
|
|
if (ioc->io_err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:34:06 +08:00
|
|
|
if (ioc->encoutput.offset) {
|
2015-03-04 23:57:41 +08:00
|
|
|
cond |= G_IO_OUT;
|
|
|
|
}
|
|
|
|
if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER &&
|
|
|
|
!ioc->io_eof) {
|
|
|
|
cond |= G_IO_IN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cond) {
|
|
|
|
object_ref(OBJECT(ioc));
|
|
|
|
ioc->io_tag =
|
|
|
|
qio_channel_add_watch(ioc->master,
|
|
|
|
cond,
|
|
|
|
qio_channel_websock_flush,
|
|
|
|
ioc,
|
|
|
|
qio_channel_websock_flush_free);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t qio_channel_websock_readv(QIOChannel *ioc,
|
|
|
|
const struct iovec *iov,
|
|
|
|
size_t niov,
|
|
|
|
int **fds,
|
|
|
|
size_t *nfds,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
|
|
|
|
size_t i;
|
|
|
|
ssize_t got = 0;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (wioc->io_err) {
|
2017-06-08 21:39:04 +08:00
|
|
|
error_propagate(errp, error_copy(wioc->io_err));
|
2015-03-04 23:57:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wioc->rawinput.offset) {
|
|
|
|
ret = qio_channel_websock_read_wire(QIO_CHANNEL_WEBSOCK(ioc), errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; i < niov ; i++) {
|
|
|
|
size_t want = iov[i].iov_len;
|
|
|
|
if (want > (wioc->rawinput.offset - got)) {
|
|
|
|
want = (wioc->rawinput.offset - got);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(iov[i].iov_base,
|
|
|
|
wioc->rawinput.buffer + got,
|
|
|
|
want);
|
|
|
|
got += want;
|
|
|
|
|
|
|
|
if (want < iov[i].iov_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_advance(&wioc->rawinput, got);
|
|
|
|
qio_channel_websock_set_watch(wioc);
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t qio_channel_websock_writev(QIOChannel *ioc,
|
|
|
|
const struct iovec *iov,
|
|
|
|
size_t niov,
|
|
|
|
int *fds,
|
|
|
|
size_t nfds,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
|
2017-10-09 23:54:07 +08:00
|
|
|
ssize_t want = iov_size(iov, niov);
|
|
|
|
ssize_t avail;
|
2015-03-04 23:57:41 +08:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (wioc->io_err) {
|
2017-06-08 21:39:04 +08:00
|
|
|
error_propagate(errp, error_copy(wioc->io_err));
|
2015-03-04 23:57:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wioc->io_eof) {
|
|
|
|
error_setg(errp, "%s", "Broken pipe");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:54:07 +08:00
|
|
|
avail = wioc->encoutput.offset >= QIO_CHANNEL_WEBSOCK_MAX_BUFFER ?
|
|
|
|
0 : (QIO_CHANNEL_WEBSOCK_MAX_BUFFER - wioc->encoutput.offset);
|
|
|
|
if (want > avail) {
|
|
|
|
want = avail;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
2017-10-09 23:54:07 +08:00
|
|
|
if (want) {
|
2017-10-09 23:33:20 +08:00
|
|
|
qio_channel_websock_encode(wioc,
|
|
|
|
QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME,
|
2017-10-09 23:54:07 +08:00
|
|
|
iov, niov, want);
|
2017-10-09 21:39:17 +08:00
|
|
|
}
|
2017-10-09 23:54:07 +08:00
|
|
|
|
|
|
|
/* Even if want == 0, we'll try write_wire in case there's
|
|
|
|
* pending data we could usefully flush out
|
|
|
|
*/
|
2015-03-04 23:57:41 +08:00
|
|
|
ret = qio_channel_websock_write_wire(wioc, errp);
|
|
|
|
if (ret < 0 &&
|
|
|
|
ret != QIO_CHANNEL_ERR_BLOCK) {
|
|
|
|
qio_channel_websock_unset_watch(wioc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
qio_channel_websock_set_watch(wioc);
|
|
|
|
|
2017-10-09 23:54:07 +08:00
|
|
|
if (want == 0) {
|
2015-03-04 23:57:41 +08:00
|
|
|
return QIO_CHANNEL_ERR_BLOCK;
|
|
|
|
}
|
|
|
|
|
2017-10-09 23:54:07 +08:00
|
|
|
return want;
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int qio_channel_websock_set_blocking(QIOChannel *ioc,
|
|
|
|
bool enabled,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
|
|
|
|
|
|
|
|
qio_channel_set_blocking(wioc->master, enabled, errp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qio_channel_websock_set_delay(QIOChannel *ioc,
|
|
|
|
bool enabled)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
|
|
|
|
|
|
|
|
qio_channel_set_delay(tioc->master, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qio_channel_websock_set_cork(QIOChannel *ioc,
|
|
|
|
bool enabled)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
|
|
|
|
|
|
|
|
qio_channel_set_cork(tioc->master, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qio_channel_websock_shutdown(QIOChannel *ioc,
|
|
|
|
QIOChannelShutdown how,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *tioc = QIO_CHANNEL_WEBSOCK(ioc);
|
|
|
|
|
|
|
|
return qio_channel_shutdown(tioc->master, how, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qio_channel_websock_close(QIOChannel *ioc,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
|
|
|
|
|
2017-09-21 18:00:47 +08:00
|
|
|
trace_qio_channel_websock_close(ioc);
|
2015-03-04 23:57:41 +08:00
|
|
|
return qio_channel_close(wioc->master, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct QIOChannelWebsockSource QIOChannelWebsockSource;
|
|
|
|
struct QIOChannelWebsockSource {
|
|
|
|
GSource parent;
|
|
|
|
QIOChannelWebsock *wioc;
|
|
|
|
GIOCondition condition;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
2017-09-12 23:21:48 +08:00
|
|
|
qio_channel_websock_source_check(GSource *source)
|
2015-03-04 23:57:41 +08:00
|
|
|
{
|
|
|
|
QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
|
|
|
|
GIOCondition cond = 0;
|
|
|
|
|
2017-09-12 23:21:48 +08:00
|
|
|
if (wsource->wioc->rawinput.offset || wsource->wioc->io_eof) {
|
2015-03-04 23:57:41 +08:00
|
|
|
cond |= G_IO_IN;
|
|
|
|
}
|
2017-10-09 21:43:42 +08:00
|
|
|
if (wsource->wioc->encoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) {
|
2015-03-04 23:57:41 +08:00
|
|
|
cond |= G_IO_OUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cond & wsource->condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2017-09-12 23:21:48 +08:00
|
|
|
qio_channel_websock_source_prepare(GSource *source,
|
|
|
|
gint *timeout)
|
2015-03-04 23:57:41 +08:00
|
|
|
{
|
2017-09-12 23:21:48 +08:00
|
|
|
*timeout = -1;
|
|
|
|
return qio_channel_websock_source_check(source);
|
2015-03-04 23:57:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
qio_channel_websock_source_dispatch(GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
QIOChannelFunc func = (QIOChannelFunc)callback;
|
|
|
|
QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;
|
|
|
|
|
|
|
|
return (*func)(QIO_CHANNEL(wsource->wioc),
|
2017-09-12 23:21:48 +08:00
|
|
|
qio_channel_websock_source_check(source),
|
2015-03-04 23:57:41 +08:00
|
|
|
user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qio_channel_websock_source_finalize(GSource *source)
|
|
|
|
{
|
|
|
|
QIOChannelWebsockSource *ssource = (QIOChannelWebsockSource *)source;
|
|
|
|
|
|
|
|
object_unref(OBJECT(ssource->wioc));
|
|
|
|
}
|
|
|
|
|
|
|
|
GSourceFuncs qio_channel_websock_source_funcs = {
|
|
|
|
qio_channel_websock_source_prepare,
|
|
|
|
qio_channel_websock_source_check,
|
|
|
|
qio_channel_websock_source_dispatch,
|
|
|
|
qio_channel_websock_source_finalize
|
|
|
|
};
|
|
|
|
|
|
|
|
static GSource *qio_channel_websock_create_watch(QIOChannel *ioc,
|
|
|
|
GIOCondition condition)
|
|
|
|
{
|
|
|
|
QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
|
|
|
|
QIOChannelWebsockSource *ssource;
|
|
|
|
GSource *source;
|
|
|
|
|
|
|
|
source = g_source_new(&qio_channel_websock_source_funcs,
|
|
|
|
sizeof(QIOChannelWebsockSource));
|
|
|
|
ssource = (QIOChannelWebsockSource *)source;
|
|
|
|
|
|
|
|
ssource->wioc = wioc;
|
|
|
|
object_ref(OBJECT(wioc));
|
|
|
|
|
|
|
|
ssource->condition = condition;
|
|
|
|
|
|
|
|
qio_channel_websock_set_watch(wioc);
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qio_channel_websock_class_init(ObjectClass *klass,
|
|
|
|
void *class_data G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
|
|
|
|
|
|
|
|
ioc_klass->io_writev = qio_channel_websock_writev;
|
|
|
|
ioc_klass->io_readv = qio_channel_websock_readv;
|
|
|
|
ioc_klass->io_set_blocking = qio_channel_websock_set_blocking;
|
|
|
|
ioc_klass->io_set_cork = qio_channel_websock_set_cork;
|
|
|
|
ioc_klass->io_set_delay = qio_channel_websock_set_delay;
|
|
|
|
ioc_klass->io_close = qio_channel_websock_close;
|
|
|
|
ioc_klass->io_shutdown = qio_channel_websock_shutdown;
|
|
|
|
ioc_klass->io_create_watch = qio_channel_websock_create_watch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo qio_channel_websock_info = {
|
|
|
|
.parent = TYPE_QIO_CHANNEL,
|
|
|
|
.name = TYPE_QIO_CHANNEL_WEBSOCK,
|
|
|
|
.instance_size = sizeof(QIOChannelWebsock),
|
|
|
|
.instance_finalize = qio_channel_websock_finalize,
|
|
|
|
.class_init = qio_channel_websock_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void qio_channel_websock_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&qio_channel_websock_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(qio_channel_websock_register_types);
|