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

Re: [virt-tools-list] [PATCH 02/12] Add virt_viewer_session_open_uri



On Thu, Jan 26, 2012 at 4:55 PM, Daniel P. Berrange <berrange redhat com> wrote:
>
> I'm more in favour of the former, so that we have direct access
> to the various parts of the URI internally whenever we need them.

Spice uri have various parts that don't need to be parsed but only by spice-gtk.

For example, the ports are specified using "port="  and "tls-port="
arguments. We want to add further connexion parameters there,
specifying which channel should be secured, what compression should be
used etc..

I think the URI representation should be kept instead, and use a GUri
kind of API that would make it easy to extract its several parts.
Unfortunately, GUri isn't yet available.

So for now, it looks like the best option is to have both. Either
specify the URI or the various components. I made a patch that uses
URI only, but it's not convenient as gtk-vnc and libvirt don't use URI
notation, and we need to convert back and forth in various places (see
attach patch)

-- 
Marc-André Lureau
From 1a50ccdf1a62042677b5613468060123731bbd8a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre lureau redhat com>
Date: Wed, 13 Jul 2011 23:52:42 +0200
Subject: [PATCH] Add virt_viewer_session_open_uri

---
 src/virt-viewer-app.c           |  112 ++++++++++++++++++++++++---------------
 src/virt-viewer-app.h           |    9 ++--
 src/virt-viewer-session-spice.c |   14 ++---
 src/virt-viewer-session-vnc.c   |   23 ++++++--
 src/virt-viewer-session.c       |   10 ++--
 src/virt-viewer-session.h       |    2 +
 src/virt-viewer.c               |    6 ++-
 7 files changed, 107 insertions(+), 69 deletions(-)

diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
index 3d02a0a..8344f51 100644
--- a/src/virt-viewer-app.c
+++ b/src/virt-viewer-app.c
@@ -114,8 +114,7 @@ struct _VirtViewerAppPrivate {
 	gboolean connected;
 	guint reconnect_poll; /* source id */
 	char *unixsock;
-	char *ghost;
-	char *gport;
+	char *guri;
 	char *host; /* ssh */
         int port;/* ssh */
 	char *user; /* ssh */
@@ -631,14 +630,21 @@ virt_viewer_app_channel_open(VirtViewerSession *session,
 {
 	VirtViewerAppPrivate *priv;
 	int fd = -1;
+	gchar *ghost = NULL, *gport = NULL;
+	int port;
 
 	g_return_if_fail(self != NULL);
 
 	priv = self->priv;
 	if (priv->transport && g_ascii_strcasecmp(priv->transport, "ssh") == 0 &&
 	    !priv->direct) {
-		if ((fd = virt_viewer_app_open_tunnel_ssh(priv->host, priv->port, priv->user,
-							  priv->ghost, priv->gport, NULL)) < 0)
+		if (virt_viewer_util_extract_host(priv->guri, NULL, &ghost, NULL, NULL, &port) < 0) {
+			virt_viewer_app_simple_message_dialog(self, _("Cannot determine the host for the uri %s."), priv->guri);
+			goto cleanup;
+		}
+		if (port != 0)
+			gport = g_strdup_printf("%d", port);
+		if ((fd = virt_viewer_app_open_tunnel_ssh(priv->host, priv->port, priv->user, ghost, gport, NULL)) < 0)
 			virt_viewer_app_simple_message_dialog(self, _("Connect to ssh failed."));
 	} else {
 		virt_viewer_app_simple_message_dialog(self, _("Can't connect to channel, SSH only supported."));
@@ -646,6 +652,9 @@ virt_viewer_app_channel_open(VirtViewerSession *session,
 
 	if (fd >= 0)
 		virt_viewer_session_channel_open_fd(session, channel, fd);
+cleanup:
+	g_free(ghost);
+	g_free(gport);
 }
 #else
 static void
@@ -657,6 +666,46 @@ virt_viewer_app_channel_open(VirtViewerSession *session G_GNUC_UNUSED,
 }
 #endif
 
+static gint
+virt_viewer_app_open_indirect(VirtViewerApp *self, int *fd)
+{
+	VirtViewerAppPrivate *priv = self->priv;
+	gchar *p = NULL, *ghost = NULL, *gport = NULL;
+	gint ret = 0, port = 0;
+
+	if (priv->guri) {
+		virt_viewer_app_trace(self, "Opening indirect TCP connection to display at %s\n",
+				      priv->guri);
+		if (virt_viewer_util_extract_host(priv->guri, NULL, &ghost, NULL, NULL, &port) < 0) {
+			virt_viewer_app_simple_message_dialog(self, _("Cannot determine the host for the uri %s."), priv->guri);
+			ret = -1;
+			goto cleanup;
+		}
+		if (port != 0)
+			gport = g_strdup_printf("%d", port);
+	} else {
+		virt_viewer_app_trace(self, "Opening indirect UNIX connection to display at %s\n",
+				      priv->unixsock);
+	}
+
+	if (priv->port)
+		p = g_strdup_printf(":%d", priv->port);
+	virt_viewer_app_trace(self, "Setting up SSH tunnel via %s%s%s%s\n",
+			      priv->user ? priv->user : "",
+			      priv->user ? "@" : "",
+			      priv->host, p ? p : "");
+	g_free(p);
+
+	if ((*fd = virt_viewer_app_open_tunnel_ssh(priv->host, priv->port,
+						   priv->user, ghost, gport, priv->unixsock)) < 0)
+		ret = -1;
+
+cleanup:
+	g_free(ghost);
+	g_free(gport);
+	return ret;
+}
+
 int
 virt_viewer_app_activate(VirtViewerApp *self)
 {
@@ -672,27 +721,7 @@ virt_viewer_app_activate(VirtViewerApp *self)
 	if (priv->transport &&
 	    g_ascii_strcasecmp(priv->transport, "ssh") == 0 &&
 	    !priv->direct) {
-		gchar *p = NULL;
-
-		if (priv->gport) {
-			virt_viewer_app_trace(self, "Opening indirect TCP connection to display at %s:%s\n",
-					      priv->ghost, priv->gport);
-		} else {
-			virt_viewer_app_trace(self, "Opening indirect UNIX connection to display at %s\n",
-					      priv->unixsock);
-		}
-		if (priv->port)
-			p = g_strdup_printf(":%d", priv->port);
-
-		virt_viewer_app_trace(self, "Setting up SSH tunnel via %s%s%s%s\n",
-				      priv->user ? priv->user : "",
-				      priv->user ? "@" : "",
-				      priv->host, p ? p : "");
-		g_free(p);
-
-		if ((fd = virt_viewer_app_open_tunnel_ssh(priv->host, priv->port,
-							  priv->user, priv->ghost,
-							  priv->gport, priv->unixsock)) < 0)
+		if (virt_viewer_app_open_indirect(self, &fd) < 0)
 			return -1;
 	} else if (priv->unixsock) {
 		virt_viewer_app_trace(self, "Opening direct UNIX connection to display at %s",
@@ -705,10 +734,10 @@ virt_viewer_app_activate(VirtViewerApp *self)
 	if (fd >= 0) {
 		ret = virt_viewer_session_open_fd(VIRT_VIEWER_SESSION(priv->session), fd);
 	} else {
-		virt_viewer_app_trace(self, "Opening direct TCP connection to display at %s:%s\n",
-				      priv->ghost, priv->gport);
-		ret = virt_viewer_session_open_host(VIRT_VIEWER_SESSION(priv->session),
-						    priv->ghost, priv->gport);
+		virt_viewer_app_trace(self, "Opening direct TCP connection to display at %s\n",
+				      priv->guri);
+		ret = virt_viewer_session_open_uri(VIRT_VIEWER_SESSION(priv->session),
+						   priv->guri);
 	}
 
 	virt_viewer_app_show_status(self, _("Connecting to graphic server"));
@@ -1225,8 +1254,8 @@ virt_viewer_app_update_pretty_address(VirtViewerApp *self)
 
 	priv = self->priv;
 	g_free(priv->pretty_address);
-	if (priv->gport)
-		priv->pretty_address = g_strdup_printf("%s:%s", priv->ghost, priv->gport);
+	if (priv->guri)
+		priv->pretty_address = g_strdup(priv->guri);
 	else
 		priv->pretty_address = g_strdup_printf("%s:%s", priv->host, priv->unixsock);
 }
@@ -1350,30 +1379,27 @@ virt_viewer_app_update_menu_displays(VirtViewerApp *self)
 
 void
 virt_viewer_app_set_connect_info(VirtViewerApp *self,
-				 const gchar *host,
-				 const gchar *ghost,
-				 const gchar *gport,
-				 const gchar *transport,
 				 const gchar *unixsock,
+				 const gchar *guri,
+				 const gchar *host,
+				 const gint port,
 				 const gchar *user,
-				 gint port)
+				 const gchar *transport)
 {
 	g_return_if_fail(VIRT_VIEWER_IS_APP(self));
 	VirtViewerAppPrivate *priv = self->priv;
 
-	DEBUG_LOG("Set connect info: %s,%s,%s,%s,%s,%s,%d",
-		  host, ghost, gport, transport, unixsock, user, port);
+	DEBUG_LOG("Set connect info: %s,%s,%s,%s,%s,%d",
+		  host, guri, transport, unixsock, user, port);
 
 	g_free(priv->host);
-	g_free(priv->ghost);
-	g_free(priv->gport);
+	g_free(priv->guri);
 	g_free(priv->transport);
 	g_free(priv->unixsock);
 	g_free(priv->user);
 
 	priv->host = g_strdup(host);
-	priv->ghost = g_strdup(ghost);
-	priv->gport = g_strdup(gport);
+	priv->guri = g_strdup(guri);
 	priv->transport = g_strdup(transport);
 	priv->unixsock = g_strdup(unixsock);
 	priv->user = g_strdup(user);
@@ -1387,7 +1413,7 @@ virt_viewer_app_free_connect_info(VirtViewerApp *self)
 {
 	g_return_if_fail(VIRT_VIEWER_IS_APP(self));
 
-	virt_viewer_app_set_connect_info(self, NULL, NULL, NULL, NULL, NULL, NULL, 0);
+	virt_viewer_app_set_connect_info(self, NULL, NULL, NULL, 0, NULL, NULL);
 }
 
 VirtViewerWindow*
diff --git a/src/virt-viewer-app.h b/src/virt-viewer-app.h
index f3fbadd..7760890 100644
--- a/src/virt-viewer-app.h
+++ b/src/virt-viewer-app.h
@@ -69,13 +69,12 @@ void virt_viewer_app_set_zoom_level(VirtViewerApp *self, gint zoom_level);
 void virt_viewer_app_set_direct(VirtViewerApp *self, gboolean direct);
 gboolean virt_viewer_app_has_session(VirtViewerApp *self);
 void virt_viewer_app_set_connect_info(VirtViewerApp *self,
-                                      const gchar *host,
-                                      const gchar *ghost,
-                                      const gchar *gport,
-                                      const gchar *transport,
                                       const gchar *unixsock,
+                                      const gchar *guri,
+                                      const gchar *host,
+                                      const gint port,
                                       const gchar *user,
-                                      gint port);
+                                      const gchar *transport);
 gboolean virt_viewer_app_window_set_visible(VirtViewerApp *self, VirtViewerWindow *window, gboolean visible);
 void virt_viewer_app_show_status(VirtViewerApp *self, const gchar *fmt, ...);
 void virt_viewer_app_show_display(VirtViewerApp *self);
diff --git a/src/virt-viewer-session-spice.c b/src/virt-viewer-session-spice.c
index d0d6e29..13c1ebb 100644
--- a/src/virt-viewer-session-spice.c
+++ b/src/virt-viewer-session-spice.c
@@ -43,7 +43,7 @@ struct _VirtViewerSessionSpicePrivate {
 
 static void virt_viewer_session_spice_close(VirtViewerSession *session);
 static gboolean virt_viewer_session_spice_open_fd(VirtViewerSession *session, int fd);
-static gboolean virt_viewer_session_spice_open_host(VirtViewerSession *session, char *host, char *port);
+static gboolean virt_viewer_session_spice_open_uri(VirtViewerSession *session, char *uri);
 static gboolean virt_viewer_session_spice_channel_open_fd(VirtViewerSession *session, VirtViewerSessionChannel *channel, int fd);
 static void virt_viewer_session_spice_channel_new(SpiceSession *s,
 						  SpiceChannel *channel,
@@ -79,7 +79,7 @@ virt_viewer_session_spice_class_init(VirtViewerSessionSpiceClass *klass)
 
 	dclass->close = virt_viewer_session_spice_close;
 	dclass->open_fd = virt_viewer_session_spice_open_fd;
-	dclass->open_host = virt_viewer_session_spice_open_host;
+	dclass->open_uri = virt_viewer_session_spice_open_uri;
 	dclass->channel_open_fd = virt_viewer_session_spice_channel_open_fd;
 
 	g_type_class_add_private(oclass, sizeof(VirtViewerSessionSpicePrivate));
@@ -118,19 +118,15 @@ virt_viewer_session_spice_close(VirtViewerSession *session)
 }
 
 static gboolean
-virt_viewer_session_spice_open_host(VirtViewerSession *session,
-				    char *host,
-				    char *port)
+virt_viewer_session_spice_open_uri(VirtViewerSession *session,
+				   char *uri)
 {
 	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
 
 	g_return_val_if_fail(self != NULL, FALSE);
 	g_return_val_if_fail(self->priv->session != NULL, FALSE);
 
-	g_object_set(self->priv->session,
-		     "host", host,
-		     "port", port,
-		     NULL);
+	g_object_set(self->priv->session, "uri", uri, NULL);
 
 	return spice_session_connect(self->priv->session);
 }
diff --git a/src/virt-viewer-session-vnc.c b/src/virt-viewer-session-vnc.c
index ad3002a..82d9fb5 100644
--- a/src/virt-viewer-session-vnc.c
+++ b/src/virt-viewer-session-vnc.c
@@ -39,7 +39,7 @@ struct _VirtViewerSessionVncPrivate {
 
 static void virt_viewer_session_vnc_close(VirtViewerSession* session);
 static gboolean virt_viewer_session_vnc_open_fd(VirtViewerSession* session, int fd);
-static gboolean virt_viewer_session_vnc_open_host(VirtViewerSession* session, char *host, char *port);
+static gboolean virt_viewer_session_vnc_open_uri(VirtViewerSession* session, gchar *uri);
 static gboolean virt_viewer_session_vnc_channel_open_fd(VirtViewerSession* session,
 							VirtViewerSessionChannel* channel, int fd);
 
@@ -68,7 +68,7 @@ virt_viewer_session_vnc_class_init(VirtViewerSessionVncClass *klass)
 
 	dclass->close = virt_viewer_session_vnc_close;
 	dclass->open_fd = virt_viewer_session_vnc_open_fd;
-	dclass->open_host = virt_viewer_session_vnc_open_host;
+	dclass->open_uri = virt_viewer_session_vnc_open_uri;
 	dclass->channel_open_fd = virt_viewer_session_vnc_channel_open_fd;
 
 	g_type_class_add_private(oclass, sizeof(VirtViewerSessionVncPrivate));
@@ -168,16 +168,27 @@ virt_viewer_session_vnc_channel_open_fd(VirtViewerSession* session G_GNUC_UNUSED
 }
 
 static gboolean
-virt_viewer_session_vnc_open_host(VirtViewerSession* session,
-				  char *host,
-				  char *port)
+virt_viewer_session_vnc_open_uri(VirtViewerSession* session, gchar *uri)
 {
 	VirtViewerSessionVnc *self = VIRT_VIEWER_SESSION_VNC(session);
+        gboolean ret = FALSE;
+        gchar *ghost = NULL, *gport = NULL;
+        int port;
 
 	g_return_val_if_fail(self != NULL, FALSE);
 	g_return_val_if_fail(self->priv->vnc != NULL, FALSE);
 
-	return vnc_display_open_host(self->priv->vnc, host, port);
+        g_return_val_if_fail(virt_viewer_util_extract_host(uri, NULL, &ghost, NULL, NULL, &port) >= 0, FALSE);
+
+        if (port != 0)
+                gport = g_strdup_printf("%d", port);
+
+	ret = vnc_display_open_host(self->priv->vnc, ghost, gport);
+
+        g_free(ghost);
+        g_free(gport);
+
+        return ret;
 }
 
 static void
diff --git a/src/virt-viewer-session.c b/src/virt-viewer-session.c
index d151238..6ded756 100644
--- a/src/virt-viewer-session.c
+++ b/src/virt-viewer-session.c
@@ -229,14 +229,16 @@ gboolean virt_viewer_session_open_fd(VirtViewerSession *session, int fd)
 	return VIRT_VIEWER_SESSION_GET_CLASS(session)->open_fd(session, fd);
 }
 
-gboolean virt_viewer_session_open_host(VirtViewerSession *session, char *host, char *port)
+gboolean virt_viewer_session_open_uri(VirtViewerSession *session, gchar *uri)
 {
         VirtViewerSessionClass *klass;
 
-	g_return_val_if_fail(VIRT_VIEWER_IS_SESSION(session), FALSE);
+        g_return_val_if_fail(VIRT_VIEWER_IS_SESSION(session), FALSE);
+
+        klass = VIRT_VIEWER_SESSION_GET_CLASS(session);
+        g_return_val_if_fail(klass->open_uri != NULL, FALSE);
 
-	klass = VIRT_VIEWER_SESSION_GET_CLASS(session);
-        return klass->open_host(session, host, port);
+        return klass->open_uri(session, uri);
 }
 
 gboolean virt_viewer_session_channel_open_fd(VirtViewerSession *session,
diff --git a/src/virt-viewer-session.h b/src/virt-viewer-session.h
index 48c8de3..28c2c26 100644
--- a/src/virt-viewer-session.h
+++ b/src/virt-viewer-session.h
@@ -68,6 +68,7 @@ struct _VirtViewerSessionClass {
 	void (* close) (VirtViewerSession* session);
 	gboolean (* open_fd) (VirtViewerSession* session, int fd);
 	gboolean (* open_host) (VirtViewerSession* session, char *host, char *port);
+	gboolean (* open_uri) (VirtViewerSession* session, char *uri);
 	gboolean (* channel_open_fd) (VirtViewerSession* session, VirtViewerSessionChannel *channel, int fd);
 
 	/* signals */
@@ -104,6 +105,7 @@ gboolean virt_viewer_session_open_host(VirtViewerSession* session, char *host, c
 GObject* virt_viewer_session_get(VirtViewerSession* session);
 gboolean virt_viewer_session_channel_open_fd(VirtViewerSession* session,
 					     VirtViewerSessionChannel* channel, int fd);
+gboolean virt_viewer_session_open_uri(VirtViewerSession *session, gchar *uri);
 
 G_END_DECLS
 
diff --git a/src/virt-viewer.c b/src/virt-viewer.c
index 7a7c488..db505b2 100644
--- a/src/virt-viewer.c
+++ b/src/virt-viewer.c
@@ -285,7 +285,7 @@ virt_viewer_extract_connect_info(VirtViewer *self,
 	gchar *transport = NULL;
 	gchar *user = NULL;
 	gint port = 0;
-	gchar *uri = NULL;
+	gchar *uri = NULL, *guri = NULL;
 
 	virt_viewer_app_free_connect_info(app);
 
@@ -341,7 +341,8 @@ virt_viewer_extract_connect_info(VirtViewer *self,
 		ghost = g_strdup(host);
 	}
 
-	virt_viewer_app_set_connect_info(app, host, ghost, gport, transport, unixsock, user, port);
+	guri = g_strdup_printf("%s://%s:%s", type, ghost, gport);
+	virt_viewer_app_set_connect_info(app, unixsock, guri, host, port, user, transport);
 
 	retval = TRUE;
 
@@ -356,6 +357,7 @@ virt_viewer_extract_connect_info(VirtViewer *self,
 	g_free(xpath);
 	g_free(xmldesc);
 	g_free(uri);
+	g_free(guri);
 	return retval;
 }
 
-- 
1.7.7.6


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