From nobody Thu May 2 15:03:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504892848927125.57170188383236; Fri, 8 Sep 2017 10:47:28 -0700 (PDT) Received: from localhost ([::1]:46715 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNNH-0007nO-L8 for importer@patchew.org; Fri, 08 Sep 2017 13:47:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40096) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNFQ-0000KJ-6R for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dqNFM-0008Hn-4g for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:20 -0400 Received: from mail-pg0-x235.google.com ([2607:f8b0:400e:c05::235]:35051) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dqNFL-0008Gr-UQ for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:16 -0400 Received: by mail-pg0-x235.google.com with SMTP id 188so5918520pgb.2 for ; Fri, 08 Sep 2017 10:39:15 -0700 (PDT) Received: from servo.cypherpath.com (68-113-0-218.static.knwc.wa.charter.com. [68.113.0.218]) by smtp.gmail.com with ESMTPSA id c30sm4556833pgn.33.2017.09.08.10.39.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Sep 2017 10:39:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cypherpath.com; s=google; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=7v8W86ncP3U13C6xTc1W8Y2BQWzG/+kj5N+vW6FffrM=; b=jn410Qwzvh1lvaZZr4FZgpjMjoLXG9P+1CVF3Ex/0zTjGu5A0HZonVwe9tc20D4Qrm C8t+v7YOfOiL7pkMWi4p4ueJqYAA/usX4bePChCkuCmnhGPYviiqvD9jPPcs57H9eTwO I00HoEPjk6U8zZb4bpNgJj3Tq1s01rHmHBJkY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:to:cc:subject:date:message-id :in-reply-to:references:in-reply-to:references; bh=7v8W86ncP3U13C6xTc1W8Y2BQWzG/+kj5N+vW6FffrM=; b=uj0p5rApQC7ELNhkRjF2MbpQp6QcE9tPIcWwk3rapoIy852XEe4OJVcjw9YYSV/y4L uNOJGMrhdGPyvlGX0OHzkpEb2JMLWXR6/UvXtCLdcT4iGE6FtghE9GlmAi4SXLqnzmfN N8jHtUw6x3dBPar/jF6xJJRVLUKe75BakZB4QlsmskfhcUxw1ofFkI0QmDhqL0RP1MEa rBo5gd552F3txZDGvYL/wXwbtVE5u2PcwD0DzfDBX8nT/llC38iO3J6SUT9PyuFg+hUC lxpbMtexbBZPUmZ/sL+UhabW/vnvwXu2dtP09tKfvW8aNfln2RPL0GYGozkdUOGHddmd 1UeQ== X-Gm-Message-State: AHPjjUg7OqWhNH/ueL/9pqLUpEprv6eTbQicyoQ7NqbIw7jeOlRKyBWB vNjlT02vO3ha33gcjEAxnaMSGbBniJmE1PHVI3ITtntsHXAkRxMdMwL962U2nAKVxj+dRFC/CKw DqMTU MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb4JL0M+ncvGdZfNNc8hvZGn4Z/nnSXkHyx9C2O2OVsQJ1Sj3I4t51w179Wlrv79Ite7q/t/CA== X-Received: by 10.84.217.91 with SMTP id e27mr4091568plj.99.1504892354820; Fri, 08 Sep 2017 10:39:14 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Fri, 8 Sep 2017 10:37:56 -0700 Message-Id: <20170908173801.15205-2-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> References: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> In-Reply-To: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> References: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> <20170724181544.20292-1-brandon.carpenter@cypherpath.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::235 Subject: [Qemu-devel] [PATCH v2 1/6] io: Always remove an old channel watch before adding a new one X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: brandon.carpenter@cypherpath.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Also set saved handle to zero when removing without adding a new watch. Signed-off-by: Brandon Carpenter Reviewed-by: Paolo Bonzini Reviewed-by: Daniel P. Berrange --- ui/vnc-auth-vencrypt.c | 3 +++ ui/vnc-ws.c | 6 ++++++ ui/vnc.c | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/ui/vnc-auth-vencrypt.c b/ui/vnc-auth-vencrypt.c index ffaab57550..c3eece4fa7 100644 --- a/ui/vnc-auth-vencrypt.c +++ b/ui/vnc-auth-vencrypt.c @@ -77,6 +77,9 @@ static void vnc_tls_handshake_done(QIOTask *task, vnc_client_error(vs); error_free(err); } else { + if (vs->ioc_tag) { + g_source_remove(vs->ioc_tag); + } vs->ioc_tag =3D qio_channel_add_watch( vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); start_auth_vencrypt_subauth(vs); diff --git a/ui/vnc-ws.c b/ui/vnc-ws.c index f530cd5474..eaf309553c 100644 --- a/ui/vnc-ws.c +++ b/ui/vnc-ws.c @@ -36,6 +36,9 @@ static void vncws_tls_handshake_done(QIOTask *task, error_free(err); } else { VNC_DEBUG("TLS handshake complete, starting websocket handshake\n"= ); + if (vs->ioc_tag) { + g_source_remove(vs->ioc_tag); + } vs->ioc_tag =3D qio_channel_add_watch( QIO_CHANNEL(vs->ioc), G_IO_IN, vncws_handshake_io, vs, NULL); } @@ -97,6 +100,9 @@ static void vncws_handshake_done(QIOTask *task, } else { VNC_DEBUG("Websock handshake complete, starting VNC protocol\n"); vnc_start_protocol(vs); + if (vs->ioc_tag) { + g_source_remove(vs->ioc_tag); + } vs->ioc_tag =3D qio_channel_add_watch( vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); } diff --git a/ui/vnc.c b/ui/vnc.c index 0b5dbc62e4..62f7a3f30a 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -1121,6 +1121,7 @@ static void vnc_disconnect_start(VncState *vs) vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); if (vs->ioc_tag) { g_source_remove(vs->ioc_tag); + vs->ioc_tag =3D 0; } qio_channel_close(vs->ioc, NULL); vs->disconnecting =3D TRUE; @@ -2931,6 +2932,9 @@ static void vnc_connect(VncDisplay *vd, QIOChannelSoc= ket *sioc, VNC_DEBUG("New client on socket %p\n", vs->sioc); update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); qio_channel_set_blocking(vs->ioc, false, NULL); + if (vs->ioc_tag) { + g_source_remove(vs->ioc_tag); + } if (websocket) { vs->websocket =3D 1; if (vd->tlscreds) { --=20 2.14.1 --=20 CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is=20 for the sole use of the intended recipient(s) and may contain proprietary,=20 confidential or privileged information or otherwise be protected by law.=20 Any unauthorized review, use, disclosure or distribution is prohibited. If=20 you are not the intended recipient, please notify the sender and destroy=20 all copies and the original message. From nobody Thu May 2 15:03:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504893153814557.5537956075394; Fri, 8 Sep 2017 10:52:33 -0700 (PDT) Received: from localhost ([::1]:46742 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNSC-00045h-Ok for importer@patchew.org; Fri, 08 Sep 2017 13:52:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40089) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNFQ-0000KB-4I for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dqNFN-0008Ik-2B for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:20 -0400 Received: from mail-pf0-x231.google.com ([2607:f8b0:400e:c00::231]:34225) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dqNFM-0008Hw-QE for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:16 -0400 Received: by mail-pf0-x231.google.com with SMTP id e1so5553582pfk.1 for ; Fri, 08 Sep 2017 10:39:16 -0700 (PDT) Received: from servo.cypherpath.com (68-113-0-218.static.knwc.wa.charter.com. [68.113.0.218]) by smtp.gmail.com with ESMTPSA id c30sm4556833pgn.33.2017.09.08.10.39.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Sep 2017 10:39:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cypherpath.com; s=google; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=JZZmlLubV6Q+PMzYYoDbfyYNWN1sqMop387Le6nPk50=; b=gE8+R2VTAIpw/jy7jaFSqBdUillq5RTvXt2tty+mkK3gcJcvkAYoTfRq/rsqkE/ZJY p3egMvBRgn4l6lgDNUB9TmnX/jxYwtiJsIeSSeEJUsKIs4Nb3vpc9Osj6pH+tWavQj4C SJ4yvnIu5L7qLV/5NFKbfp5hhxxTOA/sl49OI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:to:cc:subject:date:message-id :in-reply-to:references:in-reply-to:references; bh=JZZmlLubV6Q+PMzYYoDbfyYNWN1sqMop387Le6nPk50=; b=qXj4whfcA3gd9VYlwUHxmvoBJOcKBj3EsrKyL2irpFhMDilowCmN2M+jvoA63AseIg rBbe9XDGstIllMvXUrWeiTSLoL5JJAucgiwLICQaMtiHKYC0I22/+e6frpkgvhKMaeCw sJ3s8aVuIUb6KUlFgHT4CRMdrYtWOgfQoLvP0K+QGCAkJ6i6j/C+vRXoyksswC6Ni7bz dfEZLpHuhvqSluRvTOt5GtKPEPMbB9ciNFggSIe0NawJkNvm2kWUeTQOYXg5JHAIiX80 g0f+6ZvWJoHa20SOm8kTdtXzrK8bND8Edox3ReOLkdV5jl6VSSZCvNQl7qkZ/XOM79u8 2zzg== X-Gm-Message-State: AHPjjUjO1KRYsg1MGkL3Uu9r8Q2tz9eUJYP7Isvpljwsh6RKHKhNjC6d 4KefwF7N3QhOf5Z9JAid5ZgCXUPyvsxcBfcdkS24jz/6Wi6a1hGWeZjPyyg49hsAoqzlQnvqRuH 486Li MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb4/c9QJB7U46u3oL27i2VnbXhzWBQVk5q1iP+bEFiC1HCfASc15zR/q+IGx8J6rCK/LYZfRwQ== X-Received: by 10.98.212.94 with SMTP id u30mr3954339pfl.297.1504892355637; Fri, 08 Sep 2017 10:39:15 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Fri, 8 Sep 2017 10:37:57 -0700 Message-Id: <20170908173801.15205-3-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> References: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> In-Reply-To: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> References: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::231 Subject: [Qemu-devel] [PATCH v2 2/6] io: Small updates in preparation for websocket changes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: brandon.carpenter@cypherpath.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Gets rid of unnecessary bit shifting and performs proper EOF checking to avoid a large number of repeated calls to recvmsg() when a client abruptly terminates a connection (bug fix). Signed-off-by: Brandon Carpenter --- io/channel-websock.c | 62 +++++++++++++++---------------------------------= ---- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index 5a3badbec2..185bd31be5 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -86,8 +86,6 @@ #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 -#define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN 7 -#define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK 7 =20 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader; =20 @@ -492,7 +490,7 @@ static void qio_channel_websock_encode(QIOChannelWebsoc= k *ioc) return; } =20 - header.ws.b0 =3D (1 << QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN) | + header.ws.b0 =3D QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN | (QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE); if (ioc->rawoutput.offset < @@ -519,8 +517,8 @@ static void qio_channel_websock_encode(QIOChannelWebsoc= k *ioc) } =20 =20 -static ssize_t qio_channel_websock_decode_header(QIOChannelWebsock *ioc, - Error **errp) +static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc, + Error **errp) { unsigned char opcode, fin, has_mask; size_t header_size; @@ -539,11 +537,9 @@ static ssize_t qio_channel_websock_decode_header(QIOCh= annelWebsock *ioc, return QIO_CHANNEL_ERR_BLOCK; } =20 - fin =3D (header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN) >> - QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN; + fin =3D header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN; opcode =3D header->b0 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE; - has_mask =3D (header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK) = >> - QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK; + has_mask =3D header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK; payload_len =3D header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_= LEN; =20 if (opcode =3D=3D QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) { @@ -561,7 +557,7 @@ static ssize_t qio_channel_websock_decode_header(QIOCha= nnelWebsock *ioc, return -1; } if (!has_mask) { - error_setg(errp, "websocket frames must be masked"); + error_setg(errp, "client websocket frames must be masked"); return -1; } if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { @@ -593,8 +589,8 @@ static ssize_t qio_channel_websock_decode_header(QIOCha= nnelWebsock *ioc, } =20 =20 -static ssize_t qio_channel_websock_decode_payload(QIOChannelWebsock *ioc, - Error **errp) +static int qio_channel_websock_decode_payload(QIOChannelWebsock *ioc, + Error **errp) { size_t i; size_t payload_len; @@ -635,7 +631,7 @@ static ssize_t qio_channel_websock_decode_payload(QIOCh= annelWebsock *ioc, buffer_reserve(&ioc->rawinput, payload_len); buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len); buffer_advance(&ioc->encinput, payload_len); - return payload_len; + return 0; } =20 =20 @@ -715,8 +711,8 @@ static ssize_t qio_channel_websock_read_wire(QIOChannel= Websock *ioc, if (ret < 0) { return ret; } - if (ret =3D=3D 0 && - ioc->encinput.offset =3D=3D 0) { + if (ret =3D=3D 0 && ioc->encinput.offset =3D=3D 0) { + ioc->io_eof =3D TRUE; return 0; } ioc->encinput.offset +=3D ret; @@ -728,10 +724,6 @@ static ssize_t qio_channel_websock_read_wire(QIOChanne= lWebsock *ioc, if (ret < 0) { return ret; } - if (ret =3D=3D 0) { - ioc->io_eof =3D TRUE; - break; - } } =20 ret =3D qio_channel_websock_decode_payload(ioc, errp); @@ -996,14 +988,12 @@ struct QIOChannelWebsockSource { }; =20 static gboolean -qio_channel_websock_source_prepare(GSource *source, - gint *timeout) +qio_channel_websock_source_check(GSource *source) { QIOChannelWebsockSource *wsource =3D (QIOChannelWebsockSource *)source; GIOCondition cond =3D 0; - *timeout =3D -1; =20 - if (wsource->wioc->rawinput.offset) { + if (wsource->wioc->rawinput.offset || wsource->wioc->io_eof) { cond |=3D G_IO_IN; } if (wsource->wioc->rawoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) { @@ -1014,19 +1004,11 @@ qio_channel_websock_source_prepare(GSource *source, } =20 static gboolean -qio_channel_websock_source_check(GSource *source) +qio_channel_websock_source_prepare(GSource *source, + gint *timeout) { - QIOChannelWebsockSource *wsource =3D (QIOChannelWebsockSource *)source; - GIOCondition cond =3D 0; - - if (wsource->wioc->rawinput.offset) { - cond |=3D G_IO_IN; - } - if (wsource->wioc->rawoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) { - cond |=3D G_IO_OUT; - } - - return cond & wsource->condition; + *timeout =3D -1; + return qio_channel_websock_source_check(source); } =20 static gboolean @@ -1036,17 +1018,9 @@ qio_channel_websock_source_dispatch(GSource *source, { QIOChannelFunc func =3D (QIOChannelFunc)callback; QIOChannelWebsockSource *wsource =3D (QIOChannelWebsockSource *)source; - GIOCondition cond =3D 0; - - if (wsource->wioc->rawinput.offset) { - cond |=3D G_IO_IN; - } - if (wsource->wioc->rawoutput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER) { - cond |=3D G_IO_OUT; - } =20 return (*func)(QIO_CHANNEL(wsource->wioc), - (cond & wsource->condition), + qio_channel_websock_source_check(source), user_data); } =20 --=20 2.14.1 --=20 CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is=20 for the sole use of the intended recipient(s) and may contain proprietary,=20 confidential or privileged information or otherwise be protected by law.=20 Any unauthorized review, use, disclosure or distribution is prohibited. If=20 you are not the intended recipient, please notify the sender and destroy=20 all copies and the original message. From nobody Thu May 2 15:03:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504893016152746.4618057092067; Fri, 8 Sep 2017 10:50:16 -0700 (PDT) Received: from localhost ([::1]:46727 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNPz-0001yK-7C for importer@patchew.org; Fri, 08 Sep 2017 13:50:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40091) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNFQ-0000KE-4X for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dqNFN-0008JN-Uw for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:20 -0400 Received: from mail-pg0-x22b.google.com ([2607:f8b0:400e:c05::22b]:35052) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dqNFN-0008J1-Ot for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:17 -0400 Received: by mail-pg0-x22b.google.com with SMTP id 188so5918619pgb.2 for ; Fri, 08 Sep 2017 10:39:17 -0700 (PDT) Received: from servo.cypherpath.com (68-113-0-218.static.knwc.wa.charter.com. [68.113.0.218]) by smtp.gmail.com with ESMTPSA id c30sm4556833pgn.33.2017.09.08.10.39.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Sep 2017 10:39:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cypherpath.com; s=google; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=ynGU1w3AUPeqi+qwRQdEmX20o6+XJQrZapNWIyIuUgE=; b=c1tdrJVKAqrUPv1C8T71iD7LAXeuo+3nRECtLiB19TlWNUhIdGLGHnYCXK7gzHZ3jB lzi4aqU6nyWVnxsncnyh/dhMzpTKKc6R7XCPgnrClLFATO96opsKNuDMPlOKSUGNri8N xpfZ7CUjejX5rzPNsX2wMy7CcvHLuizcT517U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:to:cc:subject:date:message-id :in-reply-to:references:in-reply-to:references; bh=ynGU1w3AUPeqi+qwRQdEmX20o6+XJQrZapNWIyIuUgE=; b=Tg7nj4Rum/FL3fZW4LNxvWyV7HC9tSAaeZXAfVqv4PhLTC7IrhZkw4DvqWiFcvFoSq CCARhj/SU4eZrpJCYRMSsGnmgoydKkHDjGXnqzfp4YiAuzh3GL1eTz8W181+/UtSO14r LO26HZTwkD7+gx81TefgqI/YVFn3IZYcFir/BwjyYvozkGXB6bqkdaXJvTjVp5WBEwtw ucVV5LHOPyRGnlnbTNI1ieOARrl5zZLvw7kcE9x2qPTD84ZAla87SGOZEhaOfJCBFE0i 0S08v6vHj837XdvdodH1tzO8B2eUWgLtWoEin04h6PNZQbhW2DuLfZOKOo4aUxqX1KO3 RT4A== X-Gm-Message-State: AHPjjUheiRXekiH8tgVmj9NXUn/cLHAUSbecGywhcSLN9ukyZPdy2nOf aY9lXhZlCgMMcW5AvrEFJmbDGQDtdAD7RQdWM9KwgiitXRZ+AspJeNkX/9fzOEO/vfzT4Y0SOcT zgKgj MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb4AcdqioabXSeoDEzxCr9MHAVAFhEoEJze+oWToDBbfe+L/1DdwAkegZBirNDeZOQ/C9LD1Vg== X-Received: by 10.98.64.138 with SMTP id f10mr3899132pfd.246.1504892356611; Fri, 08 Sep 2017 10:39:16 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Fri, 8 Sep 2017 10:37:58 -0700 Message-Id: <20170908173801.15205-4-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> References: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> In-Reply-To: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> References: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::22b Subject: [Qemu-devel] [PATCH v2 3/6] io: Add support for fragmented websocket binary frames X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: brandon.carpenter@cypherpath.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allows fragmented binary frames by saving the previous opcode. Handles the case where an intermediary (i.e., web proxy) fragments frames originally sent unfragmented by the client. Signed-off-by: Brandon Carpenter --- include/io/channel-websock.h | 1 + io/channel-websock.c | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/io/channel-websock.h b/include/io/channel-websock.h index 3c9ff84727..7c896557c5 100644 --- a/include/io/channel-websock.h +++ b/include/io/channel-websock.h @@ -65,6 +65,7 @@ struct QIOChannelWebsock { guint io_tag; Error *io_err; gboolean io_eof; + uint8_t opcode; }; =20 /** diff --git a/io/channel-websock.c b/io/channel-websock.c index 185bd31be5..ced24135ec 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -542,28 +542,38 @@ static int qio_channel_websock_decode_header(QIOChann= elWebsock *ioc, has_mask =3D header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK; payload_len =3D header->b1 & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_= LEN; =20 + /* Save or restore opcode. */ + if (opcode) { + ioc->opcode =3D opcode; + } else { + opcode =3D ioc->opcode; + } + if (opcode =3D=3D QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE) { /* disconnect */ return 0; } =20 /* Websocket frame sanity check: - * * Websocket fragmentation is not supported. - * * All websockets frames sent by a client have to be masked. + * * Fragmentation is only supported for binary frames. + * * All frames sent by a client MUST be masked. * * Only binary encoding is supported. */ if (!fin) { - error_setg(errp, "websocket fragmentation is not supported"); - return -1; + if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { + error_setg(errp, "only binary websocket frames may be fragment= ed"); + return -1; + } + } else { + if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { + error_setg(errp, "only binary websocket frames are supported"); + return -1; + } } if (!has_mask) { error_setg(errp, "client websocket frames must be masked"); return -1; } - if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { - error_setg(errp, "only binary websocket frames are supported"); - return -1; - } =20 if (payload_len < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT) { ioc->payload_remain =3D payload_len; --=20 2.14.1 --=20 CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is=20 for the sole use of the intended recipient(s) and may contain proprietary,=20 confidential or privileged information or otherwise be protected by law.=20 Any unauthorized review, use, disclosure or distribution is prohibited. If=20 you are not the intended recipient, please notify the sender and destroy=20 all copies and the original message. From nobody Thu May 2 15:03:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504892943627274.79993755765395; Fri, 8 Sep 2017 10:49:03 -0700 (PDT) Received: from localhost ([::1]:46723 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNOo-0000yh-Ju for importer@patchew.org; Fri, 08 Sep 2017 13:49:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40093) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNFQ-0000KF-4k for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dqNFO-0008Jy-MX for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:20 -0400 Received: from mail-pf0-x236.google.com ([2607:f8b0:400e:c00::236]:36684) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dqNFO-0008JU-H7 for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:18 -0400 Received: by mail-pf0-x236.google.com with SMTP id e199so5536540pfh.3 for ; Fri, 08 Sep 2017 10:39:18 -0700 (PDT) Received: from servo.cypherpath.com (68-113-0-218.static.knwc.wa.charter.com. [68.113.0.218]) by smtp.gmail.com with ESMTPSA id c30sm4556833pgn.33.2017.09.08.10.39.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Sep 2017 10:39:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cypherpath.com; s=google; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=r14jgAiyO+0nsjjc9Wc+WdEfYoQD51qE/EukutcUoUA=; b=Zvplk6+k9EOww8Q6SdgR5N+NCgWhCZbNHVHmlqAHPVeJBtYfvzAuKf2T8HzQ/4/7WE KNJ5CwkDeJZrY5S8/uvwnqXjEBD50e07PGl01kvEFSFsylwFzzuAIYBJQPPG4Zc0hm5B gg4vBOPABSTpYhl89fe/z/Om0Iw+VhDA7FKtU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:to:cc:subject:date:message-id :in-reply-to:references:in-reply-to:references; bh=r14jgAiyO+0nsjjc9Wc+WdEfYoQD51qE/EukutcUoUA=; b=sW6DJWtvVUDuJNCu5ITu5qOUI+St6LoAzPwQ4P/UB1lQzXlvxNdyZduaBtoh092Qon /o4i2275dt3MGKEPs1+Z+aLMf6KkQ0AqXkp63YklJCSPtOythaeZmageoKP7FNNbmhgZ kHtrjfQcYO+Q6BD5O5O4z7Q4aWnrg40O72IPSmm4+Ab09RJ8cYAAvoF1XKSwpNYycl48 m8GxQcf7kPLLbKyS81w8EUY9+FlVtdeH/vckcMrYL7VkvPnKFTIQA0IikCsBRhnghWym KnrCIekejz/btoiRACUE8EIhhSktEoKW2+tB8XJFQ908H2lmf94j6dto1rY+snKlChJz zS+Q== X-Gm-Message-State: AHPjjUiIwmaZ+B6R0uet3u+boIcBdYWgtAFo0ns7770PX/Xu1eXmD8Hb kfLbP3SceGJyNoSTve+A7C9Qw8zU23eXOgh74E0c38JMt/YapMxo3nP3HKnsENFnjHRQg8dJxyl VwC1b MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb4UR7ownPTAgZq6XlWdFqoJlmeZQwMlCxam8aNvr6MgyhbmdyVE0FGACjOgNof/ZlpNIV9NJw== X-Received: by 10.98.81.4 with SMTP id f4mr3860015pfb.275.1504892357486; Fri, 08 Sep 2017 10:39:17 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Fri, 8 Sep 2017 10:37:59 -0700 Message-Id: <20170908173801.15205-5-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> References: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> In-Reply-To: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> References: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::236 Subject: [Qemu-devel] [PATCH v2 4/6] io: Allow empty websocket payload X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: brandon.carpenter@cypherpath.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Some browsers send pings/pongs with no payload, so allow empty payloads instead of closing the connection. Signed-off-by: Brandon Carpenter --- io/channel-websock.c | 62 +++++++++++++++++++++++++-----------------------= ---- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index ced24135ec..3183aeff77 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -603,44 +603,42 @@ static int qio_channel_websock_decode_payload(QIOChan= nelWebsock *ioc, Error **errp) { size_t i; - size_t payload_len; + size_t payload_len =3D 0; uint32_t *payload32; =20 - if (!ioc->payload_remain) { - error_setg(errp, - "Decoding payload but no bytes of payload remain"); - return -1; - } - - /* 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) { - payload_len =3D ioc->encinput.offset - (ioc->encinput.offset % 4); - } else { - payload_len =3D ioc->payload_remain; - } - if (payload_len =3D=3D 0) { - return QIO_CHANNEL_ERR_BLOCK; - } + 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) { + payload_len =3D ioc->encinput.offset - (ioc->encinput.offset %= 4); + } else { + payload_len =3D ioc->payload_remain; + } + if (payload_len =3D=3D 0) { + return QIO_CHANNEL_ERR_BLOCK; + } =20 - ioc->payload_remain -=3D payload_len; + ioc->payload_remain -=3D payload_len; =20 - /* unmask frame */ - /* process 1 frame (32 bit op) */ - payload32 =3D (uint32_t *)ioc->encinput.buffer; - for (i =3D 0; i < payload_len / 4; i++) { - payload32[i] ^=3D ioc->mask.u; - } - /* process the remaining bytes (if any) */ - for (i *=3D 4; i < payload_len; i++) { - ioc->encinput.buffer[i] ^=3D ioc->mask.c[i % 4]; + /* unmask frame */ + /* process 1 frame (32 bit op) */ + payload32 =3D (uint32_t *)ioc->encinput.buffer; + for (i =3D 0; i < payload_len / 4; i++) { + payload32[i] ^=3D ioc->mask.u; + } + /* process the remaining bytes (if any) */ + for (i *=3D 4; i < payload_len; i++) { + ioc->encinput.buffer[i] ^=3D ioc->mask.c[i % 4]; + } } =20 - buffer_reserve(&ioc->rawinput, payload_len); - buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len); - buffer_advance(&ioc->encinput, payload_len); + if (payload_len) { + buffer_reserve(&ioc->rawinput, payload_len); + buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len); + buffer_advance(&ioc->encinput, payload_len); + } return 0; } =20 --=20 2.14.1 --=20 CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is=20 for the sole use of the intended recipient(s) and may contain proprietary,=20 confidential or privileged information or otherwise be protected by law.=20 Any unauthorized review, use, disclosure or distribution is prohibited. If=20 you are not the intended recipient, please notify the sender and destroy=20 all copies and the original message. From nobody Thu May 2 15:03:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504893283847830.8815292345322; Fri, 8 Sep 2017 10:54:43 -0700 (PDT) Received: from localhost ([::1]:46749 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNUI-00064H-Sj for importer@patchew.org; Fri, 08 Sep 2017 13:54:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40111) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNFQ-0000L1-Q2 for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dqNFP-0008L2-Ig for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:20 -0400 Received: from mail-pg0-x22a.google.com ([2607:f8b0:400e:c05::22a]:32777) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dqNFP-0008KG-DB for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:19 -0400 Received: by mail-pg0-x22a.google.com with SMTP id t3so5955942pgt.0 for ; Fri, 08 Sep 2017 10:39:19 -0700 (PDT) Received: from servo.cypherpath.com (68-113-0-218.static.knwc.wa.charter.com. [68.113.0.218]) by smtp.gmail.com with ESMTPSA id c30sm4556833pgn.33.2017.09.08.10.39.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Sep 2017 10:39:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cypherpath.com; s=google; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=0VmBw3vqQ+DycFtgUChZiVJJOdmH1XwXIJ2fJl1tV2Y=; b=C+pUs/o6UATmy2etRmysHkM9dB406XIulWlcyKH5RABk4GUlgT7+FboPh7xN5PFAaa zPvGnPd+Q0+1vULkbmv6SjhCeBQocJ2SwASJOP6Nuhsm3Jm8ih6AJYQ70c237gOFCqx3 JVJzbcJPE0OgABq/QHAU/+Okpq/t2aIODnTjI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:to:cc:subject:date:message-id :in-reply-to:references:in-reply-to:references; bh=0VmBw3vqQ+DycFtgUChZiVJJOdmH1XwXIJ2fJl1tV2Y=; b=io28GhFXMo6Ahlq1+/F7WUKykNiMhJukgNA1YSCgmQQF9QLGboq5LWCUMNWnXmCFFJ y7xOk1NfTt7yk5tsZLPw2g7+JXG/FHpOe3/XGF5qrkS+QwE5Vu+i26EmNtk/FrBfifAP 26R+Lm1HmXViOUIKVH4arWprIaduZtx3ByQYS8Z960gWvNVGrG2mu9VLajvgteTZ8/a+ avqI/HQTOWEHWkQKkitW/1cXGSqQXLD+IBSoZt3AuGd6Gkcf5ExmZFOVTD/j4F0K+4Ed GbZsrK98UhNB1mgDRdgwUg4EbzCDmyc+5D9y37PM927YLAgnKOGRKNiQEWCcmnNeOPG5 WdLQ== X-Gm-Message-State: AHPjjUhU9fs2iNb8Zt2DPiGXV1/zxGFu3WHla699fTS6+Yhz8WXmD9lQ tLqnsAr8pHSk5cCGyU6QHy7jtRPhayvPtclJKitNVCRnKznYkL9yjtKGxNxt9VvvjvEWJzs9+// Tvkyr MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb6PeYMMQp82PO5dpEqBv4nG4WPR+7sHHbg6WnEGo+ub+sH/736q9SYFdWf+xtn0T9f0Cd2Ncg== X-Received: by 10.98.152.209 with SMTP id d78mr3907996pfk.58.1504892358323; Fri, 08 Sep 2017 10:39:18 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Fri, 8 Sep 2017 10:38:00 -0700 Message-Id: <20170908173801.15205-6-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> References: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> In-Reply-To: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> References: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::22a Subject: [Qemu-devel] [PATCH v2 5/6] io: Ignore websocket PING and PONG frames X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: brandon.carpenter@cypherpath.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Keep pings and gratuitous pongs generated by web browsers from killing websocket connections. Signed-off-by: Brandon Carpenter --- io/channel-websock.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index 3183aeff77..50387050d5 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -86,6 +86,7 @@ #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 +#define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8 =20 typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader; =20 @@ -565,8 +566,11 @@ static int qio_channel_websock_decode_header(QIOChanne= lWebsock *ioc, return -1; } } else { - if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { - error_setg(errp, "only binary websocket frames are supported"); + if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME && + opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_PING && + opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_PONG) { + error_setg(errp, "unsupported opcode: %#04x; only binary, ping= , " + "and pong websocket frames are supported", op= code); return -1; } } @@ -579,6 +583,9 @@ static int qio_channel_websock_decode_header(QIOChannel= Websock *ioc, ioc->payload_remain =3D payload_len; header_size =3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT; ioc->mask =3D header->u.m; + } else if (opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) { + error_setg(errp, "websocket control frame is too large"); + return -1; } else if (payload_len =3D=3D QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16= _BIT && ioc->encinput.offset >=3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_16= _BIT) { ioc->payload_remain =3D be16_to_cpu(header->u.s16.l16); @@ -634,9 +641,15 @@ static int qio_channel_websock_decode_payload(QIOChann= elWebsock *ioc, } } =20 + /* Drop the payload of ping/pong packets */ + if (ioc->opcode =3D=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { + if (payload_len) { + buffer_reserve(&ioc->rawinput, payload_len); + buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_le= n); + } + } + if (payload_len) { - buffer_reserve(&ioc->rawinput, payload_len); - buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_len); buffer_advance(&ioc->encinput, payload_len); } return 0; --=20 2.14.1 --=20 CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is=20 for the sole use of the intended recipient(s) and may contain proprietary,=20 confidential or privileged information or otherwise be protected by law.=20 Any unauthorized review, use, disclosure or distribution is prohibited. If=20 you are not the intended recipient, please notify the sender and destroy=20 all copies and the original message. From nobody Thu May 2 15:03:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504893106341555.0921063612979; Fri, 8 Sep 2017 10:51:46 -0700 (PDT) Received: from localhost ([::1]:46740 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNRR-0003Kq-6i for importer@patchew.org; Fri, 08 Sep 2017 13:51:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40147) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqNFR-0000Mc-VS for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dqNFQ-0008MB-NC for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:22 -0400 Received: from mail-pg0-x22e.google.com ([2607:f8b0:400e:c05::22e]:37686) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dqNFQ-0008LM-F2 for qemu-devel@nongnu.org; Fri, 08 Sep 2017 13:39:20 -0400 Received: by mail-pg0-x22e.google.com with SMTP id d8so5918000pgt.4 for ; Fri, 08 Sep 2017 10:39:20 -0700 (PDT) Received: from servo.cypherpath.com (68-113-0-218.static.knwc.wa.charter.com. [68.113.0.218]) by smtp.gmail.com with ESMTPSA id c30sm4556833pgn.33.2017.09.08.10.39.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Sep 2017 10:39:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cypherpath.com; s=google; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=ceQl8Hh0r9b20YeK93b9UsGibxD8vOPnOJvLvw8wO34=; b=Y0QgovXzsAFPNUPUy4kHQu92MpOkNiO1kGevTPKIccXo7aMbfVSDVubE6Gzkg3keU/ ZbVvxnekm5KtyKK0I4uecG/BjgV/eLGAGNRQSFpG9NO/PbUB5ZY/eB7rOe1RDCWsRcyF WFxv2GRDlmEIgE37jjLrxE2Qs1r06h4ZeyTzE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:to:cc:subject:date:message-id :in-reply-to:references:in-reply-to:references; bh=ceQl8Hh0r9b20YeK93b9UsGibxD8vOPnOJvLvw8wO34=; b=Zy4PuxOEawCto7d3yjvuCkiP8gR37P0gnYMtiaIzVTK562/DwOf6D4Yn6kU2LrLnpu MKoc2As2PhE7YDWq/sEhqb4FtwBJHTgJmKsCxwfgMhGwETcD+Ryb2ygUIFGmaAam2plj mL7MZJ2wzRsd0xXabN6m7InKPoDrcIhMGpQ3yNlyypgoGdnmVy8q6pkprfnESxlib+6m GhVRSlGqPho20NfRGwQwiB/O3fIMhS3Mwjufy7gPp80IWuFlrmqjbCpaSH0wytSsuxS0 a2HUPNer0U73RwTdfqpo5dAl+S6wknC0Jt1dEiIk0K2jHgLij94yU/KVjjQVXEcwcOV0 RZDg== X-Gm-Message-State: AHPjjUjzXSoYNzkWUS9CYx9QW7tA4+fcFptkvqm72iZ++JiE/4J6qnMf 0Nb90H1X27/2wa6kGszfQN0/xo4La7opmrYk0fTNd28jG6pKZY3RYO7cbmyJLkM/U4J5gxl21T6 XtBAw MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb5vJUGBxdTNZXpor2ZU4rgd4+My9v/Tmzzv9mnBwLIbKTGQvhOlqeXjoO2QGYEOtPC4Mj0liA== X-Received: by 10.98.102.20 with SMTP id a20mr3965690pfc.208.1504892359250; Fri, 08 Sep 2017 10:39:19 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Fri, 8 Sep 2017 10:38:01 -0700 Message-Id: <20170908173801.15205-7-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> References: <20170908173801.15205-1-brandon.carpenter@cypherpath.com> In-Reply-To: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> References: <20170724184217.21381-1-brandon.carpenter@cypherpath.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::22e Subject: [Qemu-devel] [PATCH v2 6/6] io: Reply to ping frames X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: brandon.carpenter@cypherpath.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an immediate ping reply (pong) to the outgoing stream when a ping is received. Unsolicited pongs are ignored. Signed-off-by: Brandon Carpenter --- io/channel-websock.c | 50 ++++++++++++++++++++++++++++++++----------------= -- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index 50387050d5..175f17ce6b 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -479,7 +479,8 @@ static gboolean qio_channel_websock_handshake_io(QIOCha= nnel *ioc, } =20 =20 -static void qio_channel_websock_encode(QIOChannelWebsock *ioc) +static void qio_channel_websock_encode_buffer(QIOChannelWebsock *ioc, + uint8_t opcode, Buffer *buff= er) { size_t header_size; union { @@ -487,33 +488,37 @@ static void qio_channel_websock_encode(QIOChannelWebs= ock *ioc) QIOChannelWebsockHeader ws; } header; =20 - if (!ioc->rawoutput.offset) { - return; - } - header.ws.b0 =3D QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN | - (QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME & - QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE); - if (ioc->rawoutput.offset < + (opcode & QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE); + if (buffer->offset < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_7_BIT) { - header.ws.b1 =3D (uint8_t)ioc->rawoutput.offset; + header.ws.b1 =3D (uint8_t)buffer->offset; header_size =3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT; - } else if (ioc->rawoutput.offset < + } else if (buffer->offset < QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_THRESHOLD_16_BIT) { header.ws.b1 =3D QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT; - header.ws.u.s16.l16 =3D cpu_to_be16((uint16_t)ioc->rawoutput.offse= t); + header.ws.u.s16.l16 =3D cpu_to_be16((uint16_t)buffer->offset); header_size =3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT; } else { header.ws.b1 =3D QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_64_BIT; - header.ws.u.s64.l64 =3D cpu_to_be64(ioc->rawoutput.offset); + header.ws.u.s64.l64 =3D cpu_to_be64(buffer->offset); header_size =3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_64_BIT; } header_size -=3D QIO_CHANNEL_WEBSOCK_HEADER_LEN_MASK; =20 - buffer_reserve(&ioc->encoutput, header_size + ioc->rawoutput.offset); + buffer_reserve(&ioc->encoutput, header_size + buffer->offset); buffer_append(&ioc->encoutput, header.buf, header_size); - buffer_append(&ioc->encoutput, ioc->rawoutput.buffer, - ioc->rawoutput.offset); + buffer_append(&ioc->encoutput, buffer->buffer, buffer->offset); +} + + +static void qio_channel_websock_encode(QIOChannelWebsock *ioc) +{ + if (!ioc->rawoutput.offset) { + return; + } + qio_channel_websock_encode_buffer(ioc, + QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME, &ioc->rawoutput); buffer_reset(&ioc->rawoutput); } =20 @@ -558,7 +563,7 @@ static int qio_channel_websock_decode_header(QIOChannel= Websock *ioc, /* Websocket frame sanity check: * * Fragmentation is only supported for binary frames. * * All frames sent by a client MUST be masked. - * * Only binary encoding is supported. + * * Only binary and ping/pong encoding is supported. */ if (!fin) { if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { @@ -619,6 +624,11 @@ static int qio_channel_websock_decode_payload(QIOChann= elWebsock *ioc, * for purpose of unmasking, except at end of payload */ if (ioc->encinput.offset < ioc->payload_remain) { + /* 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; + } payload_len =3D ioc->encinput.offset - (ioc->encinput.offset %= 4); } else { payload_len =3D ioc->payload_remain; @@ -641,13 +651,17 @@ static int qio_channel_websock_decode_payload(QIOChan= nelWebsock *ioc, } } =20 - /* Drop the payload of ping/pong packets */ if (ioc->opcode =3D=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { if (payload_len) { + /* binary frames are passed on */ buffer_reserve(&ioc->rawinput, payload_len); buffer_append(&ioc->rawinput, ioc->encinput.buffer, payload_le= n); } - } + } else if (ioc->opcode =3D=3D QIO_CHANNEL_WEBSOCK_OPCODE_PING) { + /* ping frames produce an immediate pong reply */ + qio_channel_websock_encode_buffer(ioc, + QIO_CHANNEL_WEBSOCK_OPCODE_PONG, &ioc->encinput); + } /* pong frames are ignored */ =20 if (payload_len) { buffer_advance(&ioc->encinput, payload_len); --=20 2.14.1 --=20 CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is=20 for the sole use of the intended recipient(s) and may contain proprietary,=20 confidential or privileged information or otherwise be protected by law.=20 Any unauthorized review, use, disclosure or distribution is prohibited. If=20 you are not the intended recipient, please notify the sender and destroy=20 all copies and the original message.