[Libguestfs] [PATCH libnbd 1/3] states: Factor out common code for setting export size and eflags.

Richard W.M. Jones rjones at redhat.com
Thu May 23 09:32:11 UTC 2019


Simple refactoring.
---
 generator/states-newstyle-opt-export-name.c | 12 +++++------
 generator/states-newstyle-opt-go.c          | 13 ++++++------
 generator/states-oldstyle.c                 | 10 +++-------
 lib/flags.c                                 | 22 +++++++++++++++++++++
 lib/internal.h                              |  5 +++++
 5 files changed, 42 insertions(+), 20 deletions(-)

diff --git a/generator/states-newstyle-opt-export-name.c b/generator/states-newstyle-opt-export-name.c
index 8ff1c1c..07b6c9e 100644
--- a/generator/states-newstyle-opt-export-name.c
+++ b/generator/states-newstyle-opt-export-name.c
@@ -58,13 +58,13 @@
   return 0;
 
  NEWSTYLE.OPT_EXPORT_NAME.CHECK_REPLY:
-  conn->h->exportsize = be64toh (conn->sbuf.export_name_reply.exportsize);
-  conn->h->eflags = be16toh (conn->sbuf.export_name_reply.eflags);
-  debug (conn->h, "exportsize: %" PRIu64 " eflags: 0x%" PRIx16,
-         conn->h->exportsize, conn->h->eflags);
-  if (conn->h->eflags == 0) {
+  uint64_t exportsize;
+  uint16_t eflags;
+
+  exportsize = be64toh (conn->sbuf.export_name_reply.exportsize);
+  eflags = be16toh (conn->sbuf.export_name_reply.eflags);
+  if (nbd_internal_set_size_and_flags (conn->h, exportsize, eflags) == -1) {
     SET_NEXT_STATE (%.DEAD);
-    set_error (EINVAL, "handshake: invalid eflags == 0 from server");
     return -1;
   }
   SET_NEXT_STATE (%.READY);
diff --git a/generator/states-newstyle-opt-go.c b/generator/states-newstyle-opt-go.c
index 4dbeee9..97f25f8 100644
--- a/generator/states-newstyle-opt-go.c
+++ b/generator/states-newstyle-opt-go.c
@@ -106,6 +106,8 @@
   uint32_t reply;
   uint32_t len;
   const size_t maxpayload = sizeof conn->sbuf.or.payload;
+  uint64_t exportsize;
+  uint16_t eflags;
 
   magic = be64toh (conn->sbuf.or.option_reply.magic);
   option = be32toh (conn->sbuf.or.option_reply.option);
@@ -129,14 +131,11 @@
     if (len <= maxpayload /* see RECV_NEWSTYLE_OPT_GO_REPLY */) {
       if (len >= sizeof conn->sbuf.or.payload.export) {
         if (be16toh (conn->sbuf.or.payload.export.info) == NBD_INFO_EXPORT) {
-          conn->h->exportsize =
-            be64toh (conn->sbuf.or.payload.export.exportsize);
-          conn->h->eflags = be16toh (conn->sbuf.or.payload.export.eflags);
-          debug (conn->h, "exportsize: %" PRIu64 " eflags: 0x%" PRIx16,
-                 conn->h->exportsize, conn->h->eflags);
-          if (conn->h->eflags == 0) {
+          exportsize = be64toh (conn->sbuf.or.payload.export.exportsize);
+          eflags = be16toh (conn->sbuf.or.payload.export.eflags);
+          if (nbd_internal_set_size_and_flags (conn->h,
+                                               exportsize, eflags) == -1) {
             SET_NEXT_STATE (%.DEAD);
-            set_error (EINVAL, "handshake: invalid eflags == 0 from server");
             return -1;
           }
         }
diff --git a/generator/states-oldstyle.c b/generator/states-oldstyle.c
index 95b7df9..29cb341 100644
--- a/generator/states-oldstyle.c
+++ b/generator/states-oldstyle.c
@@ -47,14 +47,10 @@
   eflags = be16toh (conn->sbuf.old_handshake.eflags);
 
   conn->gflags = gflags;
-  conn->h->exportsize = exportsize;
-  conn->h->eflags = eflags;
-  debug (conn->h, "exportsize: %" PRIu64 " eflags: 0x%" PRIx16
-         " gflags: 0x%" PRIx16,
-         exportsize, eflags, gflags);
-  if (eflags == 0) {
+  debug (conn->h, "gflags: 0x%" PRIx16, gflags);
+
+  if (nbd_internal_set_size_and_flags (conn->h, exportsize, eflags) == -1) {
     SET_NEXT_STATE (%.DEAD);
-    set_error (EINVAL, "handshake: invalid eflags == 0 from server");
     return -1;
   }
 
diff --git a/lib/flags.c b/lib/flags.c
index f0d9dc3..825b326 100644
--- a/lib/flags.c
+++ b/lib/flags.c
@@ -21,10 +21,32 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <stdint.h>
+#include <inttypes.h>
 #include <errno.h>
 
 #include "internal.h"
 
+/* Set the export size and eflags on the handle, validating them.
+ * This is called from the state machine when either the newstyle or
+ * oldstyle negotiation reaches the point that these are available.
+ */
+int
+nbd_internal_set_size_and_flags (struct nbd_handle *h,
+                                 uint64_t exportsize, uint16_t eflags)
+{
+  debug (h, "exportsize: %" PRIu64 " eflags: 0x%" PRIx16, exportsize, eflags);
+
+  if (eflags == 0) {
+    set_error (EINVAL, "handshake: invalid eflags == 0 from server");
+    return -1;
+  }
+
+  h->exportsize = exportsize;
+  h->eflags = eflags;
+  return 0;
+}
+
 static int
 get_flag (struct nbd_handle *h, uint16_t flag)
 {
diff --git a/lib/internal.h b/lib/internal.h
index 8eadada..f0705ef 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -265,6 +265,11 @@ extern void nbd_internal_set_last_error (int errnum, char *error);
       nbd_internal_set_last_error ((errnum), _errp);                    \
   } while (0)
 
+/* flags.c */
+extern int nbd_internal_set_size_and_flags (struct nbd_handle *h,
+                                            uint64_t exportsize,
+                                            uint16_t eflags);
+
 /* protocol.c */
 extern int nbd_internal_errno_of_nbd_error (uint32_t error);
 extern const char *nbd_internal_name_of_nbd_cmd (uint16_t type);
-- 
2.21.0




More information about the Libguestfs mailing list