[Libguestfs] [libnbd PATCH] lib: Synchronize nbd-protocol.h with nbdkit, again

Eric Blake eblake at redhat.com
Wed Sep 25 14:52:56 UTC 2019


Updated in
https://github.com/libguestfs/nbdkit/commit/XXX

Requires tweaks to our handling of nbd magic.
---

Will update with a real URL, if we like it.

 lib/nbd-protocol.h       | 35 ++++++++++++++++-------------------
 generator/states-magic.c |  2 +-
 2 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/lib/nbd-protocol.h b/lib/nbd-protocol.h
index fe4d451..bdd1ef2 100644
--- a/lib/nbd-protocol.h
+++ b/lib/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,30 +58,33 @@ 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 */
 } NBD_ATTRIBUTE_PACKED;

-/* Newstyle handshake OPT_EXPORT_NAME reply message. */
+/* Newstyle handshake OPT_EXPORT_NAME reply message.
+ * Modern clients use NBD_OPT_GO instead of this.
+ */
 struct nbd_export_name_option_reply {
-  uint64_t exportsize;          /* size of export */
-  uint16_t eflags;              /* per-export flags */
-  char zeroes[124];             /* optional zeroes */
+  uint64_t exportsize;        /* size of export */
+  uint16_t eflags;            /* per-export flags */
+  char zeroes[124];           /* optional zeroes, unless NBD_FLAG_NO_ZEROES */
 } NBD_ATTRIBUTE_PACKED;

 /* Fixed newstyle handshake reply message. */
@@ -95,8 +98,8 @@ struct nbd_fixed_new_option_reply {
 #define NBD_REP_MAGIC UINT64_C(0x3e889045565a9)

 /* Global flags. */
-#define NBD_FLAG_FIXED_NEWSTYLE 1
-#define NBD_FLAG_NO_ZEROES      2
+#define NBD_FLAG_FIXED_NEWSTYLE    (1 << 0)
+#define NBD_FLAG_NO_ZEROES         (1 << 1)

 /* Per-export flags. */
 #define NBD_FLAG_HAS_FLAGS         (1 << 0)
@@ -140,6 +143,9 @@ struct nbd_fixed_new_option_reply {
 #define NBD_REP_ERR_TOO_BIG          NBD_REP_ERR (9)

 #define NBD_INFO_EXPORT      0
+#define NBD_INFO_NAME        1
+#define NBD_INFO_DESCRIPTION 2
+#define NBD_INFO_BLOCK_SIZE  3

 /* NBD_INFO_EXPORT reply (follows fixed_new_option_reply). */
 struct nbd_fixed_new_option_reply_info_export {
@@ -160,15 +166,6 @@ struct nbd_block_descriptor {
   uint32_t status_flags;        /* block type (hole etc) */
 } NBD_ATTRIBUTE_PACKED;

-/* New-style handshake server reply when using NBD_OPT_EXPORT_NAME.
- * Modern clients use NBD_OPT_GO instead of this.
- */
-struct nbd_new_handshake_finish {
-  uint64_t exportsize;
-  uint16_t eflags;            /* per-export flags */
-  char zeroes[124];           /* must be sent as zero bytes */
-} NBD_ATTRIBUTE_PACKED;
-
 /* Request (client -> server). */
 struct nbd_request {
   uint32_t magic;               /* NBD_REQUEST_MAGIC. */
diff --git a/generator/states-magic.c b/generator/states-magic.c
index de8d235..597620d 100644
--- a/generator/states-magic.c
+++ b/generator/states-magic.c
@@ -35,7 +35,7 @@
  MAGIC.CHECK_MAGIC:
   uint64_t version;

-  if (strncmp (h->sbuf.new_handshake.nbdmagic, "NBDMAGIC", 8) != 0) {
+  if (be64toh (h->sbuf.new_handshake.nbdmagic) != NBD_MAGIC) {
     SET_NEXT_STATE (%.DEAD);
     set_error (0, "handshake: server did not send expected NBD magic");
     return 0;
-- 
2.21.0




More information about the Libguestfs mailing list