changes to establish connection to ovirt server on vm's forward_vnc_port
authorMohammed Morsi <mmorsi@redhat.com>
Tue, 27 Jan 2009 22:23:11 +0000 (17:23 -0500)
committerMohammed Morsi <mmorsi@redhat.com>
Tue, 27 Jan 2009 22:23:11 +0000 (17:23 -0500)
configure.ac
internal.h
main.c
wui_thread.c

index e597939..13276ea 100644 (file)
@@ -33,10 +33,10 @@ PKG_CHECK_MODULES([OVIRT_VIEWER],
        [gtk+-2.0 gtk-vnc-1.0 glib-2.0 libxml-2.0 gnutls gthread-2.0 libcurl])
 
 dnl Header files.
-AC_CHECK_HEADERS([sys/socket.h sys/un.h windows.h])
+AC_CHECK_HEADERS([netdb.h netinet/in.h sys/socket.h sys/un.h windows.h])
 
 dnl Optional functions.
-AC_CHECK_FUNCS([socketpair fork])
+AC_CHECK_FUNCS([socket gethostbyname htons connect])
 
 dnl Default location for CA certificate bundle.
 AC_ARG_ENABLE([cainfo],
index 9c39bef..950fd05 100644 (file)
@@ -104,6 +104,7 @@ struct vm {
   int hostid;
   int id;
   int vnc_port;
+  int forward_vnc_port;
   char *uuid;                  /* Printable UUID. */
 
   /* Only the fields above this point are required.  The remainder may
diff --git a/main.c b/main.c
index f6e5e08..390046a 100644 (file)
--- a/main.c
+++ b/main.c
 #include <gtk/gtk.h>
 #include <vncdisplay.h>
 
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
+
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
@@ -69,7 +77,7 @@ static void viewer_connected (GtkWidget *vnc);
 static void viewer_initialized (GtkWidget *vnc, GtkWidget *data);
 static void viewer_disconnected (GtkWidget *vnc);
 static void viewer_credential (GtkWidget *vnc, GValueArray *credList);
-static int viewer_open_tunnel_ssh (const char *sshhost, int sshport, const char *sshuser, int vncport);
+static int viewer_open_vnc_socket (const char *vnchost, int vncport);
 
 /* For any widgets accessed from multiple functions. */
 static GtkWidget *window;
@@ -501,6 +509,7 @@ connect_to_vm (GtkWidget *widget, gpointer _vm)
   int i, uuidlen, len, fd;
   GtkWidget *child;
   const char *label;
+  const char* hostname;
   char *label2;
 
   DEBUG ("searching tabs for uuid %s", vm->uuid);
@@ -523,10 +532,8 @@ connect_to_vm (GtkWidget *widget, gpointer _vm)
   DEBUG ("not found, creating new tab");
 
   /* This VM isn't in the notebook already, so create a new console. */
-  fd = viewer_open_tunnel_ssh (/*vm->host XXX*/ "192.168.50.6",
-                              0, /* Default SSH port. */
-                              "root", /* Root account. */
-                              vm->vnc_port);
+  hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
+  fd = viewer_open_vnc_socket(hostname, vm->forward_vnc_port);
   if (fd == -1) return;                /* We've already given an error. */
 
   child = vnc_display_new ();
@@ -740,70 +747,37 @@ viewer_credential (GtkWidget *vnc, GValueArray *credList)
                 gtk_widget_destroy(GTK_WIDGET(dialog));
 }
 
-#if defined(HAVE_SOCKETPAIR) && defined(HAVE_FORK)
+#if defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) && defined(HAVE_GETHOSTBYNAME)
 
-static int viewer_open_tunnel(const char **cmd)
+static int 
+viewer_open_vnc_socket(const char* vnchost, int vncport)
 {
-        int fd[2];
-        pid_t pid;
+  int socketfd;
+  struct hostent *serv;
+  struct sockaddr_in serv_addr;
 
-        if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd) < 0)
-                return -1;
+  socketfd = socket(PF_INET, SOCK_STREAM, 0);
+  if(socketfd < 0){
+      return -1;
+  }
 
-        pid = fork();
-        if (pid == -1) {
-               close(fd[0]);
-               close(fd[1]);
-               return -1;
-       }
+  serv = gethostbyname(vnchost);
+  if(serv == NULL){
+      return -1;
+  }
 
-        if (pid == 0) { /* child */
-                close(fd[0]);
-                close(0);
-                close(1);
-                if (dup(fd[1]) < 0)
-                        _exit(1);
-                if (dup(fd[1]) < 0)
-                        _exit(1);
-                close(fd[1]);
-                execvp("ssh", (char *const*)cmd);
-                _exit(1);
-        }
-        close(fd[1]);
-       return fd[0];
-}
+  serv_addr.sin_family = PF_INET;
+  serv_addr.sin_port = htons(vncport);
+  serv_addr.sin_addr.s_addr = ((struct in_addr *)(serv->h_addr))->s_addr; 
 
-static int
-viewer_open_tunnel_ssh (const char *sshhost, int sshport, const char *sshuser,
-                       int vncport)
-{
-  const char *cmd[10];
-  char portstr[50], portstr2[50];
-  int n = 0;
-
-  if (!sshport)
-    sshport = 22;
-
-  snprintf (portstr, sizeof portstr, "%d", sshport);
-  snprintf (portstr2, sizeof portstr2, "%d", vncport);
-
-  cmd[n++] = "ssh";
-  cmd[n++] = "-p";
-  cmd[n++] = portstr;
-  if (sshuser) {
-    cmd[n++] = "-l";
-    cmd[n++] = sshuser;
+  if (connect(socketfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
+      return -1;
   }
-  cmd[n++] = sshhost;
-  cmd[n++] = "nc";
-  cmd[n++] = "localhost";
-  cmd[n++] = portstr2;
-  cmd[n++] = NULL;
 
-  return viewer_open_tunnel(cmd);
+  return socketfd;
 }
 
-#endif /* defined(HAVE_SOCKETPAIR) && defined(HAVE_FORK) */
+#endif /* defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) && defined(HAVE_GETHOSTBYNAME) */
 
 /* Remove all menu items from the Connect menu. */
 static void
index 0585c89..9dab95c 100644 (file)
@@ -977,6 +977,7 @@ parse_vm_from_xml (xmlNodePtr node)
   vm.hostid = -1;
   vm.id = -1;
   vm.vnc_port = -1;
+  vm.forward_vnc_port = -1;
   vm.mem_allocated = -1;
   vm.mem_used = -1;
   vm.vcpus_allocated = -1;
@@ -1053,6 +1054,13 @@ parse_vm_from_xml (xmlNodePtr node)
        xmlFree (str);
       }
     }
+    else if (xmlStrcmp (p->name, (const xmlChar *) "forward-vnc-port") == 0) {
+      str = xmlNodeGetContent (p);
+      if (str != NULL) {
+       vm.forward_vnc_port = strtol ((char *) str, NULL, 10);
+       xmlFree (str);
+      }
+    }
     else if (xmlStrcmp (p->name, (const xmlChar *) "vnic-mac-addr") == 0) {
       str = xmlNodeGetContent (p);
       if (str != NULL) {
@@ -1072,6 +1080,8 @@ parse_vm_from_xml (xmlNodePtr node)
     DEBUG ("required field \"description\" missing from <vm> structure");
   else if (vm.vnc_port == -1)
     DEBUG ("required field \"vnc-port\" missing from <vm> structure");
+  else if (vm.forward_vnc_port == -1)
+    DEBUG ("required field \"forward-vnc-port\" missing from <vm> structure");
   else if (vm.uuid == NULL)
     DEBUG ("required field \"uuid\" missing from <vm> structure");
   else