From nobody Fri May 17 09:38:15 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 150522987227020.122563551780786; Tue, 12 Sep 2017 08:24:32 -0700 (PDT) Received: from localhost ([::1]:36501 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn37-00068u-8x for importer@patchew.org; Tue, 12 Sep 2017 11:24:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40476) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn12-0004jY-J9 for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drn10-0006zk-23 for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:20 -0400 Received: from mail-pf0-x230.google.com ([2607:f8b0:400e:c00::230]:35018) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1drn0z-0006zA-Rg for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:17 -0400 Received: by mail-pf0-x230.google.com with SMTP id q76so5570352pfq.2 for ; Tue, 12 Sep 2017 08:22: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 d18sm12753054pfk.11.2017.09.12.08.22.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Sep 2017 08:22: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; bh=7v8W86ncP3U13C6xTc1W8Y2BQWzG/+kj5N+vW6FffrM=; b=Ej4eRkvHg8mEXWtimMiEKeoZDvM1G0rkejGo171zIBYhk14tA+J+tav9Jqo2rD3z5O 3NihWpaTkcqiBfhPF2g56cZr/UENJt/19z9vB3sOjf1CbLFJEGM9H9ZOhX7HwxMYBF6Y wgTNQFm0eEbr1sdhkLbhO37NHKN8fnqH73p3g= 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; bh=7v8W86ncP3U13C6xTc1W8Y2BQWzG/+kj5N+vW6FffrM=; b=octTNLDrMf7YhP7Zesb8Tena2MALsM/iCyQdkaGU/C4MKlswqjob53oOesUW8c9tmz D6juLEXR6pJfGEB3BwW6Xk+Q8YOZgnI364ChmpesbXuEYta5X17K+goqB8j8VrLtJBo7 vV0OYFdO5j2Qu4on9Z38l2AeYTDtgUFC5i2RpJ/g0/mvcvRzTCnWJDcSSY8qGAH3E5zu 6ktq0l+ED/wxzNxKyvehk7Yi084oLVwRHulmVIvL7/8xTDlKAz/JtpmfpOSKzrSIbNTB 7KlGB+X5GP6oKL+I/byIkLlLpbvEIoQbvBi6maXabz7QdWHG3ugGWj2ErSXieW45Ihpd uBNg== X-Gm-Message-State: AHPjjUi9z5hoekeicsSEFe+U+nCyzxq4bfcKLlgiYBZEqp9jfiUOefu4 fH2WIz2OvX+qwhWKhpfmnnKXMLsEZz4MChcCaDoaMDwDvQ15noQ6M+uHNnFvJHl6mXfPp3i3rFw hrts1 MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb6OgaoU+P3BPlAlOX/a5SSaqWRPLgd4PnbwKWNlyT9Xt3HSFDvkfholleluWI9aaY62cslJEA== X-Received: by 10.98.206.15 with SMTP id y15mr10239857pfg.25.1505229736709; Tue, 12 Sep 2017 08:22:16 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 08:21:47 -0700 Message-Id: <20170912152153.7729-2-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170912152153.7729-1-brandon.carpenter@cypherpath.com> References: <20170912152153.7729-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::230 Subject: [Qemu-devel] [PATCH v3 1/7] 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 Fri May 17 09:38:15 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 1505229858091810.3212427673321; Tue, 12 Sep 2017 08:24:18 -0700 (PDT) Received: from localhost ([::1]:36499 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn2v-0005yJ-0x for importer@patchew.org; Tue, 12 Sep 2017 11:24:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40480) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn12-0004jZ-Km for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drn11-00070Q-5v for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:20 -0400 Received: from mail-pg0-x232.google.com ([2607:f8b0:400e:c05::232]:38468) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1drn10-0006zw-UA for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:19 -0400 Received: by mail-pg0-x232.google.com with SMTP id v66so21807360pgb.5 for ; Tue, 12 Sep 2017 08:22: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 d18sm12753054pfk.11.2017.09.12.08.22.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Sep 2017 08:22: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; bh=JZZmlLubV6Q+PMzYYoDbfyYNWN1sqMop387Le6nPk50=; b=oe2eTw40o+nPvKCtLhxkT4FbvXnUuWuZ3hgIFeivKbIYAbpryWZunleO5PWt64ktXp bVSAMFPPYNs4gbo2z7VdT541XRguaTfg+tz+frDVodxctTcF9QklsybI2dsxihKmxawD BKhWhS8kME4VhZAKABFuI5Muo/VvrVAYU+Il0= 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; bh=JZZmlLubV6Q+PMzYYoDbfyYNWN1sqMop387Le6nPk50=; b=DNFUK69yC3kvsgDk1gsbWSV2PEmxFJxAUluRSYJBcv0FlTQ4BMyCm0oXbpXWb2/mZv 1HbLIb7E2h3TklkuB0KjbcV/lSM0DD8HD2wlqeCJQGQRsJ/cjAgRYX49oO4ELL52jSmz EHeKbL/o4dsIpLn86ftNjJsciolvJqCzYqdFCcwUlNphD7EPTsrlHkOpl7hg3huRzcKW 3DcLycGxlq3s5KTgZ7G0FDYmL6yPGCYN69IM24Z+u869pywLaLd6dwDewnwS2kdfIbW1 nEgSEguUva0VQF8l4PLf9MvCmeecclLbzt+KRpAxSamB8+BRRfElqWzFmbgqhaqBi6E9 MHiQ== X-Gm-Message-State: AHPjjUjuYtdQdTyxk2Jb34GBKrakPNUemR5zZPsSexrBxCLUrzSNIKE8 Zw9W8jUpP/8F78sGLsmk+b6xCP50rkONpc13l5Vi0CvwFpHfVY5Jw+PzGvrKd+Apg3CNWOgPtbv gPigR MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb6vCiryWAj8cnT8Xm9tdCMd1yH1VJ0Z/j8OzxdbHDC+p3ru0lhM+gG4T3eFLAcig34s0fZWeA== X-Received: by 10.99.42.72 with SMTP id q69mr14864537pgq.379.1505229737765; Tue, 12 Sep 2017 08:22:17 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 08:21:48 -0700 Message-Id: <20170912152153.7729-3-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170912152153.7729-1-brandon.carpenter@cypherpath.com> References: <20170912152153.7729-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::232 Subject: [Qemu-devel] [PATCH v3 2/7] 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 Fri May 17 09:38:15 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 1505230030389290.47563573251193; Tue, 12 Sep 2017 08:27:10 -0700 (PDT) Received: from localhost ([::1]:36512 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn5h-0000DR-62 for importer@patchew.org; Tue, 12 Sep 2017 11:27:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40485) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn13-0004jd-5I for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drn12-00071B-7B for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:21 -0400 Received: from mail-pg0-x231.google.com ([2607:f8b0:400e:c05::231]:37854) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1drn12-00070d-0q for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:20 -0400 Received: by mail-pg0-x231.google.com with SMTP id d8so21859067pgt.4 for ; Tue, 12 Sep 2017 08:22: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 d18sm12753054pfk.11.2017.09.12.08.22.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Sep 2017 08:22: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; bh=ynGU1w3AUPeqi+qwRQdEmX20o6+XJQrZapNWIyIuUgE=; b=ZHUj937u+yUZDzFGFJfcSYbGbqZDnbHu7FIzRRLmLgCy8g7AZe6/p/GSfn9ELnbqto uXu64pvo0fiOXFAFF+z8ywRjzZKV94jM5y/NDtrwHltMqHVzm8ArrrDAFX/nmEnPDgjN 51ZOOyVK4mLSAWXiBaDjgR+8nC4pce1ODHqHk= 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; bh=ynGU1w3AUPeqi+qwRQdEmX20o6+XJQrZapNWIyIuUgE=; b=TVlSZOll6YdVQKaIJdJ9hf/dzcY4i9tVjAnUy+rxd++b8w36lloQcNQOLzMn/sN3e8 URGpW109hbuhVxcvIrYmyFpkOQ+0FGfpP2PGVkedhQVUgwJYWhUd8zedDb64NXg7H9/4 afh0CkyOmRdn7RccV325u+Q5sP99mDT7opPYQSC7fbbV/uJwBct7CKegQM8eGGG/b9vC 6s5rUb05v7mRGw0sRvE9O+EbnSHixcPdROyJRZRqG5F/6ygQyudQ+lop9z7nVZCHyMi/ f1ePlUZoHbcpk4Gf/K/L3jbiPj7X7G9DwCL+SaqARXlRtv2rvCfrDuYvqf/58SkI74++ WIxQ== X-Gm-Message-State: AHPjjUgnA0QgBwGqsKjU7NRNsj+jdrMdUz6v6/ygMqcvmVCiFF6tkLoj 398Fc+YfteVDII+O5LNFDXRBqrnXlh41qbOBJWpGliLStgsnDeyIZa8+RKtSkoFm7Jeu6GdUPuW BHrw7 MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb6iImUTblM0l0cwlYjEEx9l0rpES7pQfowKTjFgmTvrDM0tN5DACYeWlkVQTOJNTAjYFiMbHQ== X-Received: by 10.84.238.198 with SMTP id l6mr17261859pln.152.1505229738887; Tue, 12 Sep 2017 08:22:18 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 08:21:49 -0700 Message-Id: <20170912152153.7729-4-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170912152153.7729-1-brandon.carpenter@cypherpath.com> References: <20170912152153.7729-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::231 Subject: [Qemu-devel] [PATCH v3 3/7] 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 Fri May 17 09:38:15 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 1505230317218310.14285784828655; Tue, 12 Sep 2017 08:31:57 -0700 (PDT) Received: from localhost ([::1]:36543 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drnAK-0004ge-8X for importer@patchew.org; Tue, 12 Sep 2017 11:31:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40530) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn18-0004mG-9E for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drn14-00072Y-8H for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:26 -0400 Received: from mail-pg0-x22e.google.com ([2607:f8b0:400e:c05::22e]:33058) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1drn14-000727-1e for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:22 -0400 Received: by mail-pg0-x22e.google.com with SMTP id u18so5254446pgo.0 for ; Tue, 12 Sep 2017 08:22:21 -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 d18sm12753054pfk.11.2017.09.12.08.22.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Sep 2017 08:22:19 -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; bh=r14jgAiyO+0nsjjc9Wc+WdEfYoQD51qE/EukutcUoUA=; b=l8+B9alwkpP2HjEY8ictC1AJTeQ0gKqPEem18u+bOSzkmY8nIoXCTln0Qnhi0A3G1O d1wKkI3VjN867gwi3gfZGgrL75vAqx20k2EVA0fVte0kncdQPOWkk3o+dmDMMxlJGUIt y8MUnXAQQGMqINlTLM6ufNoYQOhf5lejuRInQ= 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; bh=r14jgAiyO+0nsjjc9Wc+WdEfYoQD51qE/EukutcUoUA=; b=afEESAGm3mmHfzkMiAK3mxJZtZuYm5yMeo9orvMzXZyHfHyc6n/KOPCae4HKaH3EUb 3ECmfQcK/wh1QWHy6N0P3SxXcQTYV1jvzaU/LKXaTE2QbbKugTdp9rq294ko6mXvvOBm yFYCCIar/7u8JZ+VLX4iXv6Ry/gxUy4twKdB3FQDEMzkqO/o4ZVedDpIatyUPlXhW+mM fETb5z9qht4znPg9e4/jDdNsT4hds7Tt1kBBEoAvyEW9PTgM3tzXkAmzyUvtqjpnaXmF xfZh+FMuRSsJenuONI3Dr4pT8Ki4QbhJReTC/M0v8MCBP8bje0pOsb2ubr614L3Tt8yC Z2mA== X-Gm-Message-State: AHPjjUh2lZEjHKpsVb841zKIgsjXsZ01oAZMnYlr0nHcuD3rA6f3si8t dZl1xExkrzJ4onezSubHksZmTLfhElDzgVcd13Uu2iuUPfX/t/rI8S140fZhse0z/KjeHrnpW4W ITEsQ MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb6VFs+4aN4nkJUQ5sylCiYmkZYOlkEwxgt6i2z6tRQvo0n32LYt4pVhEZeNcdC3WXuQgqZY3A== X-Received: by 10.84.131.2 with SMTP id 2mr17194537pld.120.1505229740759; Tue, 12 Sep 2017 08:22:20 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 08:21:50 -0700 Message-Id: <20170912152153.7729-5-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170912152153.7729-1-brandon.carpenter@cypherpath.com> References: <20170912152153.7729-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 v3 4/7] 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 Fri May 17 09:38:15 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 150522987829520.059107399912364; Tue, 12 Sep 2017 08:24:38 -0700 (PDT) Received: from localhost ([::1]:36502 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn3F-0006HN-Ev for importer@patchew.org; Tue, 12 Sep 2017 11:24:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40513) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn16-0004ky-7c for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drn15-000731-27 for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:24 -0400 Received: from mail-pg0-x22a.google.com ([2607:f8b0:400e:c05::22a]:37855) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1drn14-00072e-Rd for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:22 -0400 Received: by mail-pg0-x22a.google.com with SMTP id d8so21859357pgt.4 for ; Tue, 12 Sep 2017 08:22:22 -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 d18sm12753054pfk.11.2017.09.12.08.22.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Sep 2017 08:22:21 -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; bh=0VmBw3vqQ+DycFtgUChZiVJJOdmH1XwXIJ2fJl1tV2Y=; b=k18e2+CUcRcZZn7aFFp+a2JqaVn62GTGMP2YX1cXRHBiX2IkmRtGi7MxOkTQUnu0zY GXksDfkY/3V1VIScv5WDbPvoBYgwuQpwIVHrXovPvLj+ZYKfY7TZs7sxoGgnJSqNBlRv q9qSaxqlL8T9TutKHEUb31G8XQ2kNz8g3619g= 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; bh=0VmBw3vqQ+DycFtgUChZiVJJOdmH1XwXIJ2fJl1tV2Y=; b=JfLtp3pUB6fMJ9yMltMGAiMLbM3+yvrwnKdXMyFsrPlYtSUhoBxDfqIA3jh/TXqTzp uySCu3lkdaLhxVAgtxBE9TObkaQRz2XQAaUrDZtNP5BDUoqQBJCc79sNnUKT7eNYc/HY GTWIbfx90JhhuJ1wVtEi9HkxdTwpPkgiiKPXXsbQC6bHV6cvJbcKhLNPeWwD9pT5+lBl n6UEUFp7ktJltbwxaWtyqje63867dFKCSo5M0FYpmCTfyHUvEPD48JiLqk37pUjcPvke TVkCkl/BH6wHCSCqD+wd4/21aXp+7z1yfMeyPPc3gSrGTOKvOT3CzDksWkcuHAhm5q5M 8Z3w== X-Gm-Message-State: AHPjjUg89wPSCkcna8e7pcsYioaKvouXurdEqn+1OR1+maY+fCIsjP8O 0WKmqUio1tcuAne4P6XvhvAtLUITkcaA76OizHruZCwe4ww7IC5ojZ36vJgG++o56uzTvQlI8wu j8Bss MIME-Version: 1.0 X-Google-Smtp-Source: AOwi7QCba1RkEpsOHy9dcUF6BoyRF+8i2OmlFU7U8uTzekyyxW1/H6759Tw+D/3sCIefWFO1k/qT+A== X-Received: by 10.84.235.65 with SMTP id g1mr1419404plt.353.1505229741742; Tue, 12 Sep 2017 08:22:21 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 08:21:51 -0700 Message-Id: <20170912152153.7729-6-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170912152153.7729-1-brandon.carpenter@cypherpath.com> References: <20170912152153.7729-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 v3 5/7] 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 Fri May 17 09:38:15 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 1505229949942319.11372460623056; Tue, 12 Sep 2017 08:25:49 -0700 (PDT) Received: from localhost ([::1]:36509 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn4O-0007RJ-Va for importer@patchew.org; Tue, 12 Sep 2017 11:25:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40550) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn1A-0004pD-NB for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drn16-00073e-91 for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:28 -0400 Received: from mail-pg0-x234.google.com ([2607:f8b0:400e:c05::234]:33058) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1drn16-00073F-0E for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:24 -0400 Received: by mail-pg0-x234.google.com with SMTP id u18so5254648pgo.0 for ; Tue, 12 Sep 2017 08:22:23 -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 d18sm12753054pfk.11.2017.09.12.08.22.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Sep 2017 08:22:22 -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; bh=pr1gcsihuNwMkpDDraJqO+C1j0S5FdnEQNkZQffyli4=; b=GSd3B8Wgsz6oyhD3UQ8sKo35ALxbE/7uZ2GXnko4NVmb0dpwq+kB5yl6KJiZmvQUwb kLfV5GDR628YDAOoEMzuVm3Rp4ya//9IYKyyS0sYoIWwF20iFnj4uVb1WX3zpPMPMT/g cimCb2cFOgQO2/a16BZdqAV7+MKsyL8Vq896A= 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; bh=pr1gcsihuNwMkpDDraJqO+C1j0S5FdnEQNkZQffyli4=; b=hEkiB6bJtcc5z9DeJd3kzD9tOoWp2W9Gq1G+ff/ZbXu8H5Y6sSiWlXv1hFkI3uI38r xSH++abr78M8BRyRo5PH/qpEdgkzg7qehbXc/GT33eoAjNBl+x6qrX7VYhgBeQlGsI8Z RiK83g6T/akOHa/y/PaFN23zYvWl72PmriwevBhU2JfzBrQ+pnqE2AKt28lYslrVT/mO Mgkdp9G6jv1cE6TlvqlSUlYbzlPL8heroX74qgxU75pAqOi1fcGWrZ0VVGlhfvIKNb15 VErvYOn6voq2w4dUnEq67AGxC4AMr8p71UOF6DIGx+VJeNmTfIvLSMhWeBNvYH8uus8J mwGQ== X-Gm-Message-State: AHPjjUg6uPy1VWLzW5bgSY1dRyYG4o64zud6ciFdB0wZDBLupjv+CZSJ 7PmxTmfxJg4zqQ13fM0rYynWOCYneGUDqSHIEfkL8mD+aRN5ajAw653lIBWszy3prkJvgf40fg8 +b7Nb MIME-Version: 1.0 X-Google-Smtp-Source: AOwi7QDbO6CSyhR6PFvDc1y01gmRugBdBSbjCz04ZjhXo6xKQ7MBSlsCdHl+dY1ucbr0UVIspCorrQ== X-Received: by 10.84.252.129 with SMTP id y1mr2734489pll.107.1505229742888; Tue, 12 Sep 2017 08:22:22 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 08:21:52 -0700 Message-Id: <20170912152153.7729-7-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170912152153.7729-1-brandon.carpenter@cypherpath.com> References: <20170912152153.7729-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::234 Subject: [Qemu-devel] [PATCH v3 6/7] 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 --- include/io/channel-websock.h | 1 + io/channel-websock.c | 64 +++++++++++++++++++++++++++++-----------= ---- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/include/io/channel-websock.h b/include/io/channel-websock.h index 7c896557c5..ff32d8651b 100644 --- a/include/io/channel-websock.h +++ b/include/io/channel-websock.h @@ -60,6 +60,7 @@ struct QIOChannelWebsock { Buffer encoutput; Buffer rawinput; Buffer rawoutput; + Buffer ping_reply; size_t payload_remain; QIOChannelWebsockMask mask; guint io_tag; diff --git a/io/channel-websock.c b/io/channel-websock.c index 50387050d5..a29fee42d5 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(Buffer *output, + 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_append(&ioc->encoutput, header.buf, header_size); - buffer_append(&ioc->encoutput, ioc->rawoutput.buffer, - ioc->rawoutput.offset); + buffer_reserve(output, header_size + buffer->offset); + buffer_append(output, header.buf, header_size); + buffer_append(output, buffer->buffer, buffer->offset); +} + + +static void qio_channel_websock_encode(QIOChannelWebsock *ioc) +{ + if (!ioc->rawoutput.offset) { + return; + } + qio_channel_websock_encode_buffer(&ioc->encoutput, + 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,18 @@ 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 reply */ + buffer_reset(&ioc->ping_reply); + qio_channel_websock_encode_buffer(&ioc->ping_reply, + QIO_CHANNEL_WEBSOCK_OPCODE_PONG, &ioc->encinput); + } /* pong frames are ignored */ =20 if (payload_len) { buffer_advance(&ioc->encinput, payload_len); @@ -705,6 +720,7 @@ static void qio_channel_websock_finalize(Object *obj) buffer_free(&ioc->encoutput); buffer_free(&ioc->rawinput); buffer_free(&ioc->rawoutput); + buffer_free(&ioc->ping_reply); object_unref(OBJECT(ioc->master)); if (ioc->io_tag) { g_source_remove(ioc->io_tag); @@ -761,7 +777,13 @@ static ssize_t qio_channel_websock_write_wire(QIOChann= elWebsock *ioc, { ssize_t ret; ssize_t done =3D 0; - qio_channel_websock_encode(ioc); + + /* ping replies take priority over binary data */ + if (!ioc->ping_reply.offset) { + qio_channel_websock_encode(ioc); + } else if (!ioc->encoutput.offset) { + buffer_move_empty(&ioc->encoutput, &ioc->ping_reply); + } =20 while (ioc->encoutput.offset > 0) { ret =3D qio_channel_write(ioc->master, @@ -836,7 +858,7 @@ static void qio_channel_websock_set_watch(QIOChannelWeb= sock *ioc) return; } =20 - if (ioc->encoutput.offset) { + if (ioc->encoutput.offset || ioc->ping_reply.offset) { cond |=3D G_IO_OUT; } if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_MAX_BUFFER && --=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 Fri May 17 09:38:15 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 1505230038265954.3977212913657; Tue, 12 Sep 2017 08:27:18 -0700 (PDT) Received: from localhost ([::1]:36514 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn5p-0000K0-Dh for importer@patchew.org; Tue, 12 Sep 2017 11:27:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40532) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1drn18-0004mP-IO for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1drn17-00074I-Bn for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:26 -0400 Received: from mail-pg0-x230.google.com ([2607:f8b0:400e:c05::230]:37855) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1drn17-00073q-44 for qemu-devel@nongnu.org; Tue, 12 Sep 2017 11:22:25 -0400 Received: by mail-pg0-x230.google.com with SMTP id d8so21859635pgt.4 for ; Tue, 12 Sep 2017 08:22:25 -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 d18sm12753054pfk.11.2017.09.12.08.22.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Sep 2017 08:22:23 -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; bh=WgZWP+VIHu3GEE+meSJFmP4+gg340cZljkTTvvKN/oE=; b=D+AS2JV3Nh8AFd8QSsc2gkH4o3Jgc3EUnrhomdt0uh7V5PpioarVgGxmR5ATNUOepP 5EXTOvRjQKDF9rQimTaKsEBNKd2FKboEOvXBRMcE82vdsbv+02wwc/pmfzH+/1Te3eTP xbexPBqRHjSMjbhMOTjojyPxhv1o8rEg6xH3Y= 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; bh=WgZWP+VIHu3GEE+meSJFmP4+gg340cZljkTTvvKN/oE=; b=HTGFct3s4Ib1c92XzjNUnJ82a2azPgdBs6C1e4CVOaLG1xR34fAHLKcINMrQQAAly3 rE5r0K1Gw2YtdsRsJDiqCkCmPXJtCOt37GMEuV7OqIcyJwHL+Rr+kDvLD0mYLlUlSgRR JNq9YYEnv1KPSfCirDk371atwSggQReWWDrKT0z1j1RQftnoIiItFy5q+cGKNN4x7C89 /xmwLJDCITAfzrIaIy8g+ytFydu7rK28fxhBjKrx3lAtruSa5RAykHxo/IxA+IXM2ZPe A69EvIqoa6NEa+IK36hsWmDwMgnTxtrOD5KzT452J+PW2j0o7MS9btrFFMdLYmNXtG4W ITgg== X-Gm-Message-State: AHPjjUjMC6Oghy/OqzPB/Tzr094utxGiR+XCZpk5T3KzBe6GJNvyl5my 5N99WjVxFKdLhveNEyfWNEwUei1MMoFBoEUazYZPbEFA6LuYUWlORzmJJh3C18KRh9MYSZsdw2v IxuVI MIME-Version: 1.0 X-Google-Smtp-Source: ADKCNb5Q7RHIjj2VCtLBqjIk1SNW2YMQnK7dc4kJ6s4RS30+PzTKKPHLMXJ6cbkH1LmBHG/WT2uEPQ== X-Received: by 10.99.114.20 with SMTP id n20mr14991528pgc.448.1505229743860; Tue, 12 Sep 2017 08:22:23 -0700 (PDT) From: Brandon Carpenter To: qemu-devel@nongnu.org Date: Tue, 12 Sep 2017 08:21:53 -0700 Message-Id: <20170912152153.7729-8-brandon.carpenter@cypherpath.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170912152153.7729-1-brandon.carpenter@cypherpath.com> References: <20170912152153.7729-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::230 Subject: [Qemu-devel] [PATCH v3 7/7] io: Attempt to send websocket close messages to client 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" Make a best effort attempt to close websocket connections according to the RFC. Sends the close message, as room permits in the socket buffer, and immediately closes the socket. Signed-off-by: Brandon Carpenter --- io/channel-websock.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/io/channel-websock.c b/io/channel-websock.c index a29fee42d5..c50f8c6c50 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -122,6 +122,15 @@ enum { QIO_CHANNEL_WEBSOCK_OPCODE_PONG =3D 0xA }; =20 +enum { + QIO_CHANNEL_WEBSOCK_STATUS_NORMAL =3D 1000, + QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR =3D 1002, + QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA =3D 1003, + QIO_CHANNEL_WEBSOCK_STATUS_POLICY =3D 1008, + QIO_CHANNEL_WEBSOCK_STATUS_TOO_LARGE =3D 1009, + QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR =3D 1011, +}; + static size_t qio_channel_websock_extract_headers(char *buffer, QIOChannelWebsockHTTPHeader *hdrs, @@ -523,6 +532,26 @@ static void qio_channel_websock_encode(QIOChannelWebso= ck *ioc) } =20 =20 +static ssize_t qio_channel_websock_write_wire(QIOChannelWebsock *, Error *= *); + + +static void qio_channel_websock_write_close(QIOChannelWebsock *ioc, + uint16_t code, const char *r= eason) +{ + buffer_reserve(&ioc->rawoutput, 2 + (reason ? strlen(reason) : 0)); + *(uint16_t *)(ioc->rawoutput.buffer + ioc->rawoutput.offset) =3D cpu_t= o_be16(code); + ioc->rawoutput.offset +=3D 2; + if (reason) { + buffer_append(&ioc->rawoutput, reason, strlen(reason)); + } + qio_channel_websock_encode_buffer(&ioc->encoutput, + QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, &ioc->rawoutput); + buffer_reset(&ioc->rawoutput); + qio_channel_websock_write_wire(ioc, NULL); + qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); +} + + static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc, Error **errp) { @@ -536,6 +565,8 @@ static int qio_channel_websock_decode_header(QIOChannel= Websock *ioc, error_setg(errp, "Decoding header but %zu bytes of payload remain", ioc->payload_remain); + qio_channel_websock_write_close(ioc, + QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR, "internal server er= ror"); return -1; } if (ioc->encinput.offset < QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT) { @@ -568,19 +599,29 @@ static int qio_channel_websock_decode_header(QIOChann= elWebsock *ioc, if (!fin) { if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME) { error_setg(errp, "only binary websocket frames may be fragment= ed"); + qio_channel_websock_write_close(ioc, + QIO_CHANNEL_WEBSOCK_STATUS_POLICY , + "only binary frames may be fragmented"); return -1; } } else { if (opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME && + opcode !=3D QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE && 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); + error_setg(errp, "unsupported opcode: %#04x; only binary, clos= e, " + "ping, and pong websocket frames are supported", op= code); + qio_channel_websock_write_close(ioc, + QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA , + "only binary, close, ping, and pong frames are support= ed"); return -1; } } if (!has_mask) { error_setg(errp, "client websocket frames must be masked"); + qio_channel_websock_write_close(ioc, + QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR, + "client frames must be masked"); return -1; } =20 @@ -590,6 +631,9 @@ static int qio_channel_websock_decode_header(QIOChannel= Websock *ioc, ioc->mask =3D header->u.m; } else if (opcode & QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK) { error_setg(errp, "websocket control frame is too large"); + qio_channel_websock_write_close(ioc, + QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR, + "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) { @@ -607,7 +651,7 @@ static int qio_channel_websock_decode_header(QIOChannel= Websock *ioc, } =20 buffer_advance(&ioc->encinput, header_size); - return 1; + return 0; } =20 =20 @@ -657,6 +701,21 @@ static int qio_channel_websock_decode_payload(QIOChann= elWebsock *ioc, 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_CLOSE) { + /* close frames are echoed back */ + error_setg(errp, "websocket closed by peer"); + if (payload_len) { + /* echo client status */ + qio_channel_websock_encode_buffer(&ioc->encoutput, + QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, &ioc->encinput); + qio_channel_websock_write_wire(ioc, NULL); + qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, N= ULL); + } else { + /* send our own status */ + qio_channel_websock_write_close(ioc, + QIO_CHANNEL_WEBSOCK_STATUS_NORMAL, "peer requested clo= se"); + } + return -1; } else if (ioc->opcode =3D=3D QIO_CHANNEL_WEBSOCK_OPCODE_PING) { /* ping frames produce an immediate reply */ buffer_reset(&ioc->ping_reply); --=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.