From nobody Sun Feb 8 19:59:46 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.zoho.com; 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 1486138054461164.54547537314113; Fri, 3 Feb 2017 08:07:34 -0800 (PST) Received: from localhost ([::1]:35499 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgOZ-0007MX-Ka for importer@patchew.org; Fri, 03 Feb 2017 11:07:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36695) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZg5n-0007nZ-Bm for qemu-devel@nongnu.org; Fri, 03 Feb 2017 10:48:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZg5j-00081z-4U for qemu-devel@nongnu.org; Fri, 03 Feb 2017 10:48:07 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:42403 helo=relay.sw.ru) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZg5i-0007zf-P0 for qemu-devel@nongnu.org; Fri, 03 Feb 2017 10:48:03 -0500 Received: from kvm.qa.sw.ru. (msk-vpn.virtuozzo.com [195.214.232.6]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id v13Flv7j011039; Fri, 3 Feb 2017 18:47:57 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 18:47:43 +0300 Message-Id: <20170203154757.36140-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170203154757.36140-1-vsementsov@virtuozzo.com> References: <20170203154757.36140-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 04/18] nbd/client: refactor nbd_receive_starttls 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: kwolf@redhat.com, famz@redhat.com, den@virtuozzo.com, armbru@redhat.com, mreitz@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Split out nbd_receive_simple_option to be reused for structured reply option. Signed-off-by: Vladimir Sementsov-Ogievskiy --- nbd/client.c | 54 +++++++++++++++++++++++++++++++++++---------------= ---- nbd/nbd-internal.h | 14 ++++++++++++++ 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index de5c9366c7..6caf6bda6d 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -395,39 +395,55 @@ static int nbd_receive_query_exports(QIOChannel *ioc, } } =20 -static QIOChannel *nbd_receive_starttls(QIOChannel *ioc, - QCryptoTLSCreds *tlscreds, - const char *hostname, Error **errp) +static int nbd_receive_simple_option(QIOChannel *ioc, int opt, + bool abort_on_notsup, Error **errp) { nbd_opt_reply reply; - QIOChannelTLS *tioc; - struct NBDTLSHandshakeData data =3D { 0 }; =20 - TRACE("Requesting TLS from server"); - if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0)= { - return NULL; + TRACE("Requesting '%s' option from server", nbd_opt_name(opt)); + if (nbd_send_option_request(ioc, opt, 0, NULL, errp) < 0) { + return -1; } =20 - TRACE("Getting TLS reply from server"); - if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0)= { - return NULL; + TRACE("Getting '%s' option reply from server", nbd_opt_name(opt)); + if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) { + return -1; } =20 if (reply.type !=3D NBD_REP_ACK) { - error_setg(errp, "Server rejected request to start TLS %" PRIx32, - reply.type); - nbd_send_opt_abort(ioc); - return NULL; + error_setg(errp, "Server rejected request for '%s' option: %" PRIx= 32, + nbd_opt_name(opt), reply.type); + if (abort_on_notsup) { + nbd_send_opt_abort(ioc); + } + return -1; } =20 if (reply.length !=3D 0) { - error_setg(errp, "Start TLS response was not zero %" PRIu32, - reply.length); - nbd_send_opt_abort(ioc); + error_setg(errp, "'%s' option response was not zero %" PRIu32, + nbd_opt_name(opt), reply.length); + if (abort_on_notsup) { + nbd_send_opt_abort(ioc); + } + return -1; + } + + TRACE("%s 'option' approved", nbd_opt_name(opt)); + return 0; +} + +static QIOChannel *nbd_receive_starttls(QIOChannel *ioc, + QCryptoTLSCreds *tlscreds, + const char *hostname, Error **errp) +{ + QIOChannelTLS *tioc; + struct NBDTLSHandshakeData data =3D { 0 }; + + if (nbd_receive_simple_option(ioc, NBD_OPT_STARTTLS, true, errp) < 0) { return NULL; } =20 - TRACE("TLS request approved, setting up TLS"); + TRACE("Setting up TLS"); tioc =3D qio_channel_tls_new_client(ioc, tlscreds, hostname, errp); if (!tioc) { return NULL; diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h index 489eeaf887..3284bfc85a 100644 --- a/nbd/nbd-internal.h +++ b/nbd/nbd-internal.h @@ -96,6 +96,20 @@ #define NBD_ENOSPC 28 #define NBD_ESHUTDOWN 108 =20 +static inline const char *nbd_opt_name(int opt) +{ + switch (opt) { + case NBD_OPT_EXPORT_NAME: return "export_name"; + case NBD_OPT_ABORT: return "abort"; + case NBD_OPT_LIST: return "list"; + case NBD_OPT_PEEK_EXPORT: return "peek_export"; + case NBD_OPT_STARTTLS: return "tls"; + case NBD_OPT_STRUCTURED_REPLY: return "structured_reply"; + } + + return ""; +} + static inline ssize_t read_sync(QIOChannel *ioc, void *buffer, size_t size) { struct iovec iov =3D { .iov_base =3D buffer, .iov_len =3D size }; --=20 2.11.0