qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-devel@nongnu.org, qemu-block@nongnu.org
Cc: mreitz@redhat.com, kwolf@redhat.com, pbonzini@redhat.com,
	eblake@redhat.com, vsementsov@virtuozzo.com, den@openvz.org
Subject: [Qemu-devel] [PATCH v2 2/8] nbd/server: add nbd_read_opt_name helper
Date: Mon, 12 Mar 2018 18:21:20 +0300	[thread overview]
Message-ID: <20180312152126.286890-3-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20180312152126.286890-1-vsementsov@virtuozzo.com>

Add helper to read name in format:

  uint32 len       (<= NBD_MAX_NAME_SIZE)
  len bytes string (not 0-terminated)

The helper would be reused in following patch.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---

v2: splitted and changed a lot helper from larger patch of v1

 nbd/server.c | 55 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 42 insertions(+), 13 deletions(-)

diff --git a/nbd/server.c b/nbd/server.c
index d163964cf9..085e14afbf 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -273,6 +273,47 @@ static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
     return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
 }
 
+/* nbd_opt_read_name
+ *
+ * Read string in format:
+ *   uint32_t len     (<= NBD_MAX_NAME_SIZE)
+ *   len bytes string (not 0-terminated)
+ *
+ * @name should be enough to store NBD_MAX_NAME_SIZE+1.
+ * if @length is non-zero, it would be set to read string length.
+ *
+ * Return -errno on I/O error, 0 if option was completely handled by
+ * sending a reply about inconsistent lengths, or 1 on success. */
+static int nbd_opt_read_name(NBDClient *client, char *name, uint32_t *length,
+                             Error **errp)
+{
+    int ret;
+    uint32_t len;
+
+    ret = nbd_opt_read(client, &len, sizeof(len), errp);
+    if (ret <= 0) {
+        return ret;
+    }
+    cpu_to_be32s(&len);
+
+    if (len > NBD_MAX_NAME_SIZE) {
+        return nbd_opt_invalid(client, errp,
+                               "Invalid name length: %" PRIu32, len);
+    }
+
+    ret = nbd_opt_read(client, name, len, errp);
+    if (ret <= 0) {
+        return ret;
+    }
+    name[len] = '\0';
+
+    if (length) {
+        *length = len;
+    }
+
+    return 1;
+}
+
 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
  * Return -errno on error, 0 on success. */
 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
@@ -455,19 +496,7 @@ static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags,
         2 bytes: N, number of requests (can be 0)
         N * 2 bytes: N requests
     */
-    rc = nbd_opt_read(client, &namelen, sizeof(namelen), errp);
-    if (rc <= 0) {
-        return rc;
-    }
-    be32_to_cpus(&namelen);
-    if (namelen >= sizeof(name)) {
-        return nbd_opt_invalid(client, errp, "name too long for qemu");
-    }
-    rc = nbd_opt_read(client, name, namelen, errp);
-    if (rc <= 0) {
-        return rc;
-    }
-    name[namelen] = '\0';
+    rc = nbd_opt_read_name(client, name, &namelen, errp);
     trace_nbd_negotiate_handle_export_name_request(name);
 
     rc = nbd_opt_read(client, &requests, sizeof(requests), errp);
-- 
2.11.1

  parent reply	other threads:[~2018-03-12 15:21 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-12 15:21 [Qemu-devel] [PATCH v2 0/8] nbd block status base:allocation Vladimir Sementsov-Ogievskiy
2018-03-12 15:21 ` [Qemu-devel] [PATCH v2 1/8] nbd/server: add nbd_opt_invalid helper Vladimir Sementsov-Ogievskiy
2018-03-13  2:20   ` Eric Blake
2018-03-12 15:21 ` Vladimir Sementsov-Ogievskiy [this message]
2018-03-13  2:31   ` [Qemu-devel] [PATCH v2 2/8] nbd/server: add nbd_read_opt_name helper Eric Blake
2018-03-12 15:21 ` [Qemu-devel] [PATCH v2 3/8] nbd: BLOCK_STATUS for standard get_block_status function: server part Vladimir Sementsov-Ogievskiy
2018-03-13 13:47   ` Eric Blake
2018-03-13 13:56     ` Eric Blake
2018-03-12 15:21 ` [Qemu-devel] [PATCH v2 4/8] block/nbd-client: save first fatal error in nbd_iter_error Vladimir Sementsov-Ogievskiy
2018-03-12 15:21 ` [Qemu-devel] [PATCH v2 5/8] nbd: BLOCK_STATUS for standard get_block_status function: client part Vladimir Sementsov-Ogievskiy
2018-03-13 15:38   ` Eric Blake
2018-04-27 12:50   ` Peter Maydell
2018-04-27 13:22     ` Vladimir Sementsov-Ogievskiy
2018-03-12 15:21 ` [Qemu-devel] [PATCH v2 6/8] iotests.py: tiny refactor: move system imports up Vladimir Sementsov-Ogievskiy
2018-03-12 15:21 ` [Qemu-devel] [PATCH v2 7/8] iotests: add file_path helper Vladimir Sementsov-Ogievskiy
2018-03-13 15:43   ` Eric Blake
2018-03-12 15:21 ` [Qemu-devel] [PATCH v2 8/8] iotests: new test 209 for NBD BLOCK_STATUS Vladimir Sementsov-Ogievskiy
2018-03-13 15:50   ` Eric Blake
2018-03-12 15:28 ` [Qemu-devel] [PATCH v2 0/8] nbd block status base:allocation Vladimir Sementsov-Ogievskiy
2018-03-13  1:58   ` Eric Blake
2018-03-13  2:06 ` no-reply
2018-03-13  3:11   ` Eric Blake
2018-03-13 20:16     ` Eric Blake
2018-03-13  7:55 ` no-reply
2018-03-13 15:55 ` Eric Blake
2018-03-13 16:15   ` Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180312152126.286890-3-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).