[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 4/5] p2v: Don't hard code "localhost" (NBD listening address) everywhere.



No functional change.
---
 p2v/conversion.c | 14 +++++++++-----
 p2v/nbd.c        | 35 ++++++++++++++++++-----------------
 p2v/p2v.h        |  6 +++---
 p2v/ssh.c        |  6 ++++--
 4 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/p2v/conversion.c b/p2v/conversion.c
index 26d321f..0c17ef2 100644
--- a/p2v/conversion.c
+++ b/p2v/conversion.c
@@ -235,6 +235,7 @@ start_conversion (struct config *config,
 
   /* Start the data connections and NBD server processes, one per disk. */
   for (i = 0; config->disks[i] != NULL; ++i) {
+    const char *nbd_local_ipaddr;
     int nbd_local_port;
     CLEANUP_FREE char *device = NULL;
 
@@ -262,14 +263,15 @@ start_conversion (struct config *config,
     }
 
     /* Start NBD server listening on the given port number. */
-    data_conns[i].nbd_pid = start_nbd_server (&nbd_local_port, device);
+    data_conns[i].nbd_pid =
+      start_nbd_server (&nbd_local_ipaddr, &nbd_local_port, device);
     if (data_conns[i].nbd_pid == 0) {
       set_conversion_error ("NBD server error: %s", get_nbd_error ());
       goto out;
     }
 
     /* Wait for NBD server to start up and listen. */
-    if (wait_for_nbd_server_to_start (nbd_local_port) == -1) {
+    if (wait_for_nbd_server_to_start (nbd_local_ipaddr, nbd_local_port) == -1) {
       set_conversion_error ("NBD server error: %s", get_nbd_error ());
       goto out;
     }
@@ -286,7 +288,8 @@ start_conversion (struct config *config,
     /* Open the SSH data connection, with reverse port forwarding
      * back to the NBD server.
      */
-    data_conns[i].h = open_data_connection (config, nbd_local_port,
+    data_conns[i].h = open_data_connection (config,
+                                            nbd_local_ipaddr, nbd_local_port,
                                             &data_conns[i].nbd_remote_port);
     if (data_conns[i].h == NULL) {
       const char *err = get_ssh_error ();
@@ -297,9 +300,10 @@ start_conversion (struct config *config,
 
 #if DEBUG_STDERR
     fprintf (stderr,
-             "%s: data connection for %s: SSH remote port %d, local port %d\n",
+             "%s: data connection for %s: SSH remote port %d, local port %s:%d\n",
              getprogname (), device,
-             data_conns[i].nbd_remote_port, nbd_local_port);
+             data_conns[i].nbd_remote_port,
+             nbd_local_ipaddr, nbd_local_port);
 #endif
   }
 
diff --git a/p2v/nbd.c b/p2v/nbd.c
index ed6f9a1..92f864a 100644
--- a/p2v/nbd.c
+++ b/p2v/nbd.c
@@ -69,8 +69,8 @@ static const enum nbd_server standard_servers[] =
  */
 static enum nbd_server use_server;
 
-static pid_t start_qemu_nbd (int nbd_local_port, const char *device);
-static pid_t start_nbdkit (int nbd_local_port, const char *device);
+static pid_t start_qemu_nbd (const char *ipaddr, int nbd_local_port, const char *device);
+static pid_t start_nbdkit (const char *ipaddr, int nbd_local_port, const char *device);
 static int connect_with_source_port (const char *hostname, int dest_port, int source_port);
 static int bind_source_port (int sockfd, int family, int source_port);
 
@@ -229,7 +229,7 @@ test_nbd_servers (void)
  * Returns the process ID (E<gt> 0) or C<0> if there is an error.
  */
 pid_t
-start_nbd_server (int *port, const char *device)
+start_nbd_server (const char **ipaddr, int *port, const char *device)
 {
   /* Choose a local port. */
   *port = nbd_local_port;
@@ -237,10 +237,12 @@ start_nbd_server (int *port, const char *device)
 
   switch (use_server) {
   case QEMU_NBD:
-    return start_qemu_nbd (*port, device);
+    *ipaddr = "localhost";
+    return start_qemu_nbd (*ipaddr, *port, device);
 
   case NBDKIT:
-    return start_nbdkit (*port, device);
+    *ipaddr = "localhost";
+    return start_nbdkit (*ipaddr, *port, device);
 
   default:
     abort ();
@@ -253,7 +255,7 @@ start_nbd_server (int *port, const char *device)
  * Returns the process ID (E<gt> 0) or C<0> if there is an error.
  */
 static pid_t
-start_qemu_nbd (int port, const char *device)
+start_qemu_nbd (const char *ipaddr, int port, const char *device)
 {
   pid_t pid;
   char port_str[64];
@@ -280,7 +282,7 @@ start_qemu_nbd (int port, const char *device)
             "-p", port_str,     /* listening port */
             "-t",               /* persistent */
             "-f", "raw",        /* force raw format */
-            "-b", "localhost",  /* listen only on loopback interface */
+            "-b", ipaddr,       /* listen only on loopback interface */
             "--cache=unsafe",   /* use unsafe caching for speed */
             device,             /* a device like /dev/sda */
             NULL);
@@ -299,7 +301,7 @@ start_qemu_nbd (int port, const char *device)
  * Returns the process ID (E<gt> 0) or C<0> if there is an error.
  */
 static pid_t
-start_nbdkit (int port, const char *device)
+start_nbdkit (const char *ipaddr, int port, const char *device)
 {
   pid_t pid;
   char port_str[64];
@@ -328,7 +330,7 @@ start_nbdkit (int port, const char *device)
             "nbdkit",
             "-r",               /* readonly (vital!) */
             "-p", port_str,     /* listening port */
-            "-i", "localhost",  /* listen only on loopback interface */
+            "-i", ipaddr     ,  /* listen only on loopback interface */
             "-f",               /* don't fork */
             "file",             /* file plugin */
             file_str,           /* a device like file=/dev/sda */
@@ -346,7 +348,7 @@ start_nbdkit (int port, const char *device)
  * connections.
  */
 int
-wait_for_nbd_server_to_start (int nbd_local_port)
+wait_for_nbd_server_to_start (const char *ipaddr, int port)
 {
   int sockfd = -1;
   int result = -1;
@@ -368,15 +370,14 @@ wait_for_nbd_server_to_start (int nbd_local_port)
     }
 
     /* Source port for probing NBD server should be one greater than
-     * nbd_local_port.  It's not guaranteed to always bind to this
-     * port, but it will hint the kernel to start there and try
-     * incrementally higher ports if needed.  This avoids the case
-     * where the kernel selects nbd_local_port as our source port, and
-     * we immediately connect to ourself.  See:
+     * port.  It's not guaranteed to always bind to this port, but it
+     * will hint the kernel to start there and try incrementally
+     * higher ports if needed.  This avoids the case where the kernel
+     * selects port as our source port, and we immediately connect to
+     * ourself.  See:
      * https://bugzilla.redhat.com/show_bug.cgi?id=1167774#c9
      */
-    sockfd = connect_with_source_port ("localhost", nbd_local_port,
-                                       nbd_local_port+1);
+    sockfd = connect_with_source_port (ipaddr, port, port+1);
     if (sockfd >= 0)
       break;
 
diff --git a/p2v/p2v.h b/p2v/p2v.h
index 1cde808..5223aa2 100644
--- a/p2v/p2v.h
+++ b/p2v/p2v.h
@@ -125,7 +125,7 @@ extern int inhibit_power_saving (void);
 
 /* ssh.c */
 extern int test_connection (struct config *);
-extern mexp_h *open_data_connection (struct config *, int local_port, int *remote_port);
+extern mexp_h *open_data_connection (struct config *, const char *local_ipaddr, int local_port, int *remote_port);
 extern mexp_h *start_remote_connection (struct config *, const char *remote_dir);
 extern const char *get_ssh_error (void);
 extern int scp_file (struct config *config, const char *localfile, const char *remotefile);
@@ -133,8 +133,8 @@ extern int scp_file (struct config *config, const char *localfile, const char *r
 /* nbd.c */
 extern void set_nbd_option (const char *opt);
 extern void test_nbd_servers (void);
-extern pid_t start_nbd_server (int *nbd_local_port, const char *device);
-extern int wait_for_nbd_server_to_start (int nbd_local_port);
+extern pid_t start_nbd_server (const char **ipaddr, int *port, const char *device);
+extern int wait_for_nbd_server_to_start (const char *ipaddr, int port);
 const char *get_nbd_error (void);
 
 /* utils.c */
diff --git a/p2v/ssh.c b/p2v/ssh.c
index d29aa40..20acb03 100644
--- a/p2v/ssh.c
+++ b/p2v/ssh.c
@@ -1039,7 +1039,9 @@ compatible_version (const char *v2v_version)
 }
 
 mexp_h *
-open_data_connection (struct config *config, int local_port, int *remote_port)
+open_data_connection (struct config *config,
+                      const char *local_ipaddr, int local_port,
+                      int *remote_port)
 {
   mexp_h *h;
   char remote_arg[32];
@@ -1052,7 +1054,7 @@ open_data_connection (struct config *config, int local_port, int *remote_port)
   const int ovecsize = 12;
   int ovector[ovecsize];
 
-  snprintf (remote_arg, sizeof remote_arg, "0:localhost:%d", local_port);
+  snprintf (remote_arg, sizeof remote_arg, "0:%s:%d", local_ipaddr, local_port);
 
   h = start_ssh (0, config, (char **) extra_args, 0);
   if (h == NULL)
-- 
2.9.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]