[Libguestfs] [nbdkit PATCH 1/2] common/protocol: Switch nbdmagic to uint64_t

Eric Blake eblake at redhat.com
Wed Sep 25 14:49:34 UTC 2019


Stating that our magic is the C string "NBDMAGIC" is ambiguous, since
not all the world uses ASCII.  Switch to something that is
wire-compatible even if compiled in an EBCDIC environment.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 common/protocol/nbd-protocol.h       | 9 +++++----
 server/protocol-handshake-newstyle.c | 2 +-
 server/protocol-handshake-oldstyle.c | 2 +-
 plugins/nbd/nbd-standalone.c         | 2 +-
 tests/test-layers.c                  | 2 +-
 5 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/common/protocol/nbd-protocol.h b/common/protocol/nbd-protocol.h
index a7a70116..9bf7171e 100644
--- a/common/protocol/nbd-protocol.h
+++ b/common/protocol/nbd-protocol.h
@@ -50,7 +50,7 @@

 /* Old-style handshake. */
 struct nbd_old_handshake {
-  char nbdmagic[8];           /* "NBDMAGIC" */
+  uint64_t nbdmagic;          /* NBD_MAGIC */
   uint64_t version;           /* NBD_OLD_VERSION */
   uint64_t exportsize;
   uint16_t gflags;            /* global flags */
@@ -58,20 +58,21 @@ struct nbd_old_handshake {
   char zeroes[124];           /* must be sent as zero bytes */
 } NBD_ATTRIBUTE_PACKED;

+#define NBD_MAGIC       UINT64_C(0x4e42444d41474943) /* ASCII "NBDMAGIC" */
 #define NBD_OLD_VERSION UINT64_C(0x420281861253)

 /* New-style handshake. */
 struct nbd_new_handshake {
-  char nbdmagic[8];           /* "NBDMAGIC" */
+  uint64_t nbdmagic;          /* NBD_MAGIC */
   uint64_t version;           /* NBD_NEW_VERSION */
   uint16_t gflags;            /* global flags */
 } NBD_ATTRIBUTE_PACKED;

-#define NBD_NEW_VERSION UINT64_C(0x49484156454F5054)
+#define NBD_NEW_VERSION UINT64_C(0x49484156454F5054) /* ASCII "IHAVEOPT" */

 /* New-style handshake option (sent by the client to us). */
 struct nbd_new_option {
-  uint64_t version;           /* NEW_VERSION */
+  uint64_t version;           /* NBD_NEW_VERSION */
   uint32_t option;            /* NBD_OPT_* */
   uint32_t optlen;            /* option data length */
   /* option data follows */
diff --git a/server/protocol-handshake-newstyle.c b/server/protocol-handshake-newstyle.c
index e46b8255..d0fb4dd7 100644
--- a/server/protocol-handshake-newstyle.c
+++ b/server/protocol-handshake-newstyle.c
@@ -677,7 +677,7 @@ protocol_handshake_newstyle (struct connection *conn)

   debug ("newstyle negotiation: flags: global 0x%x", gflags);

-  memcpy (handshake.nbdmagic, "NBDMAGIC", 8);
+  handshake.nbdmagic = htobe64 (NBD_MAGIC);
   handshake.version = htobe64 (NBD_NEW_VERSION);
   handshake.gflags = htobe16 (gflags);

diff --git a/server/protocol-handshake-oldstyle.c b/server/protocol-handshake-oldstyle.c
index 8faa4f03..4efc668b 100644
--- a/server/protocol-handshake-oldstyle.c
+++ b/server/protocol-handshake-oldstyle.c
@@ -60,7 +60,7 @@ protocol_handshake_oldstyle (struct connection *conn)
          gflags, eflags);

   memset (&handshake, 0, sizeof handshake);
-  memcpy (handshake.nbdmagic, "NBDMAGIC", 8);
+  handshake.nbdmagic = htobe64 (NBD_MAGIC);
   handshake.version = htobe64 (NBD_OLD_VERSION);
   handshake.exportsize = htobe64 (exportsize);
   handshake.gflags = htobe16 (gflags);
diff --git a/plugins/nbd/nbd-standalone.c b/plugins/nbd/nbd-standalone.c
index 1789e39c..ab74e1e6 100644
--- a/plugins/nbd/nbd-standalone.c
+++ b/plugins/nbd/nbd-standalone.c
@@ -1018,7 +1018,7 @@ nbd_open_handle (int readonly)
     nbdkit_error ("unable to read magic: %m");
     goto err;
   }
-  if (strncmp (old.nbdmagic, "NBDMAGIC", sizeof old.nbdmagic)) {
+  if (be64toh (old.nbdmagic) != NBD_MAGIC) {
     nbdkit_error ("wrong magic, %s is not an NBD server", servname);
     goto err;
   }
diff --git a/tests/test-layers.c b/tests/test-layers.c
index 9bc6532c..93b7770c 100644
--- a/tests/test-layers.c
+++ b/tests/test-layers.c
@@ -174,7 +174,7 @@ main (int argc, char *argv[])
     perror ("recv: handshake");
     exit (EXIT_FAILURE);
   }
-  if (memcmp (handshake.nbdmagic, "NBDMAGIC", 8) != 0 ||
+  if (be64toh (handshake.nbdmagic) != NBD_MAGIC ||
       be64toh (handshake.version) != NBD_NEW_VERSION) {
     fprintf (stderr, "%s: unexpected NBDMAGIC or version\n",
              program_name);
-- 
2.21.0




More information about the Libguestfs mailing list