aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nbd/client.c22
-rw-r--r--nbd/nbd-internal.h3
2 files changed, 9 insertions, 16 deletions
diff --git a/nbd/client.c b/nbd/client.c
index 5a03a84418..f625c207c5 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -733,7 +733,6 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
QIOChannel **outioc, NBDExportInfo *info,
Error **errp)
{
- char buf[256];
uint64_t magic;
int rc;
bool zeroes = true;
@@ -754,27 +753,20 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
goto fail;
}
- if (nbd_read(ioc, buf, 8, errp) < 0) {
- error_prepend(errp, "Failed to read data: ");
- goto fail;
- }
-
- buf[8] = '\0';
- if (strlen(buf) == 0) {
- error_setg(errp, "Server connection closed unexpectedly");
+ if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) {
+ error_prepend(errp, "Failed to read initial magic: ");
goto fail;
}
-
- magic = ldq_be_p(buf);
+ magic = be64_to_cpu(magic);
trace_nbd_receive_negotiate_magic(magic);
- if (memcmp(buf, "NBDMAGIC", 8) != 0) {
- error_setg(errp, "Invalid magic received");
+ if (magic != NBD_INIT_MAGIC) {
+ error_setg(errp, "Bad initial magic received: 0x%" PRIx64, magic);
goto fail;
}
if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) {
- error_prepend(errp, "Failed to read magic: ");
+ error_prepend(errp, "Failed to read server magic: ");
goto fail;
}
magic = be64_to_cpu(magic);
@@ -913,7 +905,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
}
info->flags = oldflags;
} else {
- error_setg(errp, "Bad magic received");
+ error_setg(errp, "Bad server magic received: 0x%" PRIx64, magic);
goto fail;
}
diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h
index f38be9ebaa..82aa221227 100644
--- a/nbd/nbd-internal.h
+++ b/nbd/nbd-internal.h
@@ -46,8 +46,9 @@
/* Size of oldstyle negotiation */
#define NBD_OLDSTYLE_NEGOTIATE_SIZE (8 + 8 + 8 + 4 + 124)
+#define NBD_INIT_MAGIC 0x4e42444d41474943LL /* ASCII "NBDMAGIC" */
#define NBD_REQUEST_MAGIC 0x25609513
-#define NBD_OPTS_MAGIC 0x49484156454F5054LL
+#define NBD_OPTS_MAGIC 0x49484156454F5054LL /* ASCII "IHAVEOPT" */
#define NBD_CLIENT_MAGIC 0x0000420281861253LL
#define NBD_REP_MAGIC 0x0003e889045565a9LL