From nobody Mon Feb 9 02:12:09 2026 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.