qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Ashijeet Acharya <ashijeetacharya@gmail.com>
To: kwolf@redhat.com
Cc: rjones@redhat.com, jcody@redhat.com, mreitz@redhat.com,
	eblake@redhat.com, berrange@redhat.com, pbonzini@redhat.com,
	qemu-devel@nongnu.org, qemu-block@nongnu.org,
	Ashijeet Acharya <ashijeetacharya@gmail.com>
Subject: [Qemu-devel] [PATCH v3 3/5] block/ssh: Add InetSocketAddress and accept it
Date: Mon, 17 Oct 2016 23:02:13 +0530	[thread overview]
Message-ID: <1476725535-3350-4-git-send-email-ashijeetacharya@gmail.com> (raw)
In-Reply-To: <1476725535-3350-1-git-send-email-ashijeetacharya@gmail.com>

Add InetSocketAddress compatibility to SSH driver.

Add a new option "server" to the SSH block driver which then accepts
a InetSocketAddress.

"host" and "port" are supported as legacy options and are mapped to
their InetSocketAddress representation.

Signed-off-by: Ashijeet Acharya <ashijeetacharya@gmail.com>
---
 block/ssh.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 81 insertions(+), 13 deletions(-)

diff --git a/block/ssh.c b/block/ssh.c
index 75cb7bc..7963b48 100644
--- a/block/ssh.c
+++ b/block/ssh.c
@@ -30,10 +30,14 @@
 #include "block/block_int.h"
 #include "qapi/error.h"
 #include "qemu/error-report.h"
+#include "qemu/cutils.h"
 #include "qemu/sockets.h"
 #include "qemu/uri.h"
+#include "qapi-visit.h"
 #include "qapi/qmp/qint.h"
 #include "qapi/qmp/qstring.h"
+#include "qapi/qobject-input-visitor.h"
+#include "qapi/qobject-output-visitor.h"
 
 /* DEBUG_SSH=1 enables the DPRINTF (debugging printf) statements in
  * this block driver code.
@@ -74,6 +78,8 @@ typedef struct BDRVSSHState {
      */
     LIBSSH2_SFTP_ATTRIBUTES attrs;
 
+    InetSocketAddress *inet;
+
     /* Used to warn if 'flush' is not supported. */
     char *hostport;
     bool unsafe_flush_warning;
@@ -263,7 +269,8 @@ static bool ssh_has_filename_options_conflict(QDict *options, Error **errp)
             !strcmp(qe->key, "port") ||
             !strcmp(qe->key, "path") ||
             !strcmp(qe->key, "user") ||
-            !strcmp(qe->key, "host_key_check"))
+            !strcmp(qe->key, "host_key_check") ||
+            strstart(qe->key, "server.", NULL))
         {
             error_setg(errp, "Option '%s' cannot be used with a file name",
                        qe->key);
@@ -555,14 +562,69 @@ static QemuOptsList ssh_runtime_opts = {
     },
 };
 
+static bool ssh_process_legacy_socket_options(QDict *output_opts,
+                                              QemuOpts *legacy_opts,
+                                              Error **errp)
+{
+    const char *host = qemu_opt_get(legacy_opts, "host");
+    const char *port = qemu_opt_get(legacy_opts, "port");
+
+    if (!host && port) {
+        error_setg(errp, "port may not be used without host");
+        return false;
+    }
+
+    if (host) {
+        qdict_put(output_opts, "server.host", qstring_from_str(host));
+        qdict_put(output_opts, "server.port",
+                  qstring_from_str(port ?: stringify(22)));
+    }
+
+    return true;
+}
+
+static InetSocketAddress *ssh_config(BDRVSSHState *s, QDict *options,
+                                     Error **errp)
+{
+    InetSocketAddress *inet = NULL;
+    QDict *addr = NULL;
+    QObject *crumpled_addr = NULL;
+    Visitor *iv = NULL;
+    Error *local_error = NULL;
+
+    qdict_extract_subqdict(options, &addr, "server.");
+    if (!qdict_size(addr)) {
+        error_setg(errp, "SSH server address missing");
+        goto out;
+    }
+
+    crumpled_addr = qdict_crumple(addr, true, errp);
+    if (!crumpled_addr) {
+        goto out;
+    }
+
+    iv = qobject_input_visitor_new(crumpled_addr, true);
+    visit_type_InetSocketAddress(iv, NULL, &inet, &local_error);
+    if (local_error) {
+        error_propagate(errp, local_error);
+        goto out;
+    }
+
+out:
+    QDECREF(addr);
+    qobject_decref(crumpled_addr);
+    visit_free(iv);
+    return inet;
+}
+
 static int connect_to_ssh(BDRVSSHState *s, QDict *options,
                           int ssh_flags, int creat_mode, Error **errp)
 {
     int r, ret;
     QemuOpts *opts = NULL;
     Error *local_err = NULL;
-    const char *host, *user, *path, *host_key_check;
-    int port;
+    const char *user, *path, *host_key_check;
+    long port = 0;
 
     opts = qemu_opts_create(&ssh_runtime_opts, NULL, 0, &error_abort);
     qemu_opts_absorb_qdict(opts, options, &local_err);
@@ -572,15 +634,11 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
         goto err;
     }
 
-    host = qemu_opt_get(opts, "host");
-    if (!host) {
+    if (!ssh_process_legacy_socket_options(options, opts, errp)) {
         ret = -EINVAL;
-        error_setg(errp, "No hostname was specified");
         goto err;
     }
 
-    port = qemu_opt_get_number(opts, "port", 22);
-
     path = qemu_opt_get(opts, "path");
     if (!path) {
         ret = -EINVAL;
@@ -603,12 +661,21 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
         host_key_check = "yes";
     }
 
-    /* Construct the host:port name for inet_connect. */
-    g_free(s->hostport);
-    s->hostport = g_strdup_printf("%s:%d", host, port);
+    /* Pop the config into our state object, Exit if invalid */
+    s->inet = ssh_config(s, options, errp);
+    if (!s->inet) {
+        ret = -EINVAL;
+        goto err;
+    }
+
+    if (qemu_strtol(s->inet->port, NULL, 10, &port) < 0) {
+        error_setg(errp, "Use only numeric port value");
+        ret = -EINVAL;
+        goto err;
+    }
 
     /* Open the socket and connect. */
-    s->sock = inet_connect(s->hostport, errp);
+    s->sock = inet_connect_saddr(s->inet, errp, NULL, NULL);
     if (s->sock < 0) {
         ret = -EIO;
         goto err;
@@ -634,7 +701,8 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
     }
 
     /* Check the remote host's key against known_hosts. */
-    ret = check_host_key(s, host, port, host_key_check, errp);
+    ret = check_host_key(s, s->inet->host, port, host_key_check,
+                         errp);
     if (ret < 0) {
         goto err;
     }
-- 
2.6.2

  parent reply	other threads:[~2016-10-17 17:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-17 17:32 [Qemu-devel] [PATCH v3 0/5] Allow blockdev-add for SSH Ashijeet Acharya
2016-10-17 17:32 ` [Qemu-devel] [PATCH v3 1/5] block/ssh: Add ssh_has_filename_options_conflict() Ashijeet Acharya
2016-10-25 10:03   ` Kevin Wolf
2016-10-17 17:32 ` [Qemu-devel] [PATCH v3 2/5] util/qemu-sockets: Make inet_connect_saddr() public Ashijeet Acharya
2016-10-25 10:03   ` Kevin Wolf
2016-10-17 17:32 ` Ashijeet Acharya [this message]
2016-10-25  9:55   ` [Qemu-devel] [PATCH v3 3/5] block/ssh: Add InetSocketAddress and accept it Kevin Wolf
2016-10-17 17:32 ` [Qemu-devel] [PATCH v3 4/5] block/ssh: Use InetSocketAddress options Ashijeet Acharya
2016-10-25 10:04   ` Kevin Wolf
2016-10-17 17:32 ` [Qemu-devel] [PATCH v3 5/5] qapi: allow blockdev-add for ssh Ashijeet Acharya
2016-10-25 10:04   ` Kevin Wolf
2016-10-25 19:52   ` Eric Blake
2016-10-17 18:51 ` [Qemu-devel] [PATCH v3 0/5] Allow blockdev-add for SSH no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1476725535-3350-4-git-send-email-ashijeetacharya@gmail.com \
    --to=ashijeetacharya@gmail.com \
    --cc=berrange@redhat.com \
    --cc=eblake@redhat.com \
    --cc=jcody@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rjones@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).