X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=main.c;h=bb456efbab89d9df0bd7427527de770ea35a8a8c;hb=2c24cea28b2f4702105fcf4f54fc4e923697174a;hp=f6e5e08ed7d14a2d27f0aa6cb9a8baba31b82ccb;hpb=f4f6a4d699c857e97a6fca07df3b942de7649f67;p=ovirt-viewer.git diff --git a/main.c b/main.c index f6e5e08..bb456ef 100644 --- a/main.c +++ b/main.c @@ -27,6 +27,14 @@ #include #include +#ifdef HAVE_NETDB_H +#include +#endif + +#ifdef HAVE_NETINET_IN_H +#include +#endif + #ifdef HAVE_SYS_SOCKET_H #include #endif @@ -50,12 +58,17 @@ gboolean debug = 0; * configure or on the command line. */ const char *cainfo = CAINFO; -gboolean check_cert = TRUE; +gboolean check_cert = FALSE; // do we want this enabled by default ? + // would require a CA by default (self-signed wont work) + // (don't set to true, change var/flag to no_check_cert) /* Private functions. */ static void start_ui (void); static GtkWidget *menu_item_new (int which_menu); +static void refresh_menu_vm_list (GtkWidget *, gpointer); +static void connect_to_wui_on_enter (GtkWidget *, gpointer); static void connect_to_wui (GtkWidget *, gpointer); +static void login_to_wui_on_enter (GtkWidget *, gpointer); static void login_to_wui (GtkWidget *, gpointer); static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data); static void destroy (GtkWidget *widget, gpointer data); @@ -69,7 +82,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; @@ -86,6 +99,7 @@ static GtkWidget *login_area; static GtkWidget *la_username; static GtkWidget *la_password; static GtkWidget *la_button; +static GtkWidget *la_error; static GtkWidget *notebook; static GtkWidget *statusbar; static guint statusbar_ctx; @@ -118,6 +132,11 @@ static struct menuItem menuItems[] = { /* Window title. */ static const char *title = "oVirt Viewer"; +// when running vm +// 47 chars +static const char *title_vm = + "oVirt Viewer: (ctrl+alt to grab/release mouse) "; + /* Gtk widget styles. Avoid installation hassles by keeping this * inside the binary. It can still be overridden by the user (who * will do that?) @@ -140,7 +159,7 @@ static const GOptionEntry options[] = { { "cainfo", 0, 0, G_OPTION_ARG_STRING, &cainfo, "set the path of the CA certificate bundle", NULL }, { "check-certificate", 0, 0, G_OPTION_ARG_NONE, &check_cert, - "if --no-check-certificate is passed we don't check the SSL certificate of the server", NULL }, + "check the SSL certificate of the server", NULL }, { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "turn on debugging messages", NULL }, { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version, @@ -234,7 +253,9 @@ start_ui (void) GtkWidget *ca_vbox; GtkWidget *ca_hbox; GtkWidget *ca_label; + GtkWidget *la_vbox; GtkWidget *la_hbox; + GtkWidget *la_label; DEBUG ("creating viewer windows and menus"); @@ -279,6 +300,9 @@ start_ui (void) refresh_vmlist_separator = gtk_separator_menu_item_new (); g_object_ref (refresh_vmlist_separator); + g_signal_connect (G_OBJECT (refresh_vmlist), "activate", + G_CALLBACK (refresh_menu_vm_list), NULL); + #if 0 screenshot = gtk_menu_item_new_with_mnemonic ("_Screenshot"); gtk_menu_append (GTK_MENU (filemenu), screenshot); @@ -330,24 +354,36 @@ start_ui (void) gtk_widget_set_name (connection_area, "ovirt-viewer-connection-area"); + g_signal_connect (G_OBJECT (ca_hostname), "key-release-event", + G_CALLBACK (connect_to_wui_on_enter), NULL); g_signal_connect (G_OBJECT (ca_button), "clicked", G_CALLBACK (connect_to_wui), NULL); login_area = gtk_event_box_new (); + la_vbox = gtk_vbox_new (FALSE, 0); la_hbox = gtk_hbox_new (FALSE, 0); + la_label = gtk_label_new ("Username / password:"); la_username = gtk_entry_new (); gtk_entry_set_width_chars (GTK_ENTRY (la_username), 12); la_password = gtk_entry_new (); gtk_entry_set_width_chars (GTK_ENTRY (la_password), 12); gtk_entry_set_visibility (GTK_ENTRY (la_password), FALSE); la_button = gtk_button_new_with_label ("Login"); + la_error = gtk_label_new (NULL); gtk_container_add (GTK_CONTAINER (la_hbox), la_username); gtk_container_add (GTK_CONTAINER (la_hbox), la_password); gtk_container_add (GTK_CONTAINER (la_hbox), la_button); - gtk_container_add (GTK_CONTAINER (login_area), la_hbox); + gtk_box_pack_start (GTK_BOX (la_vbox), la_label, TRUE, FALSE, 4); + gtk_box_pack_start (GTK_BOX (la_vbox), la_hbox, TRUE, FALSE, 4); + gtk_box_pack_start (GTK_BOX (la_vbox), la_error, TRUE, FALSE, 4); + gtk_container_add (GTK_CONTAINER (login_area), la_vbox); gtk_widget_set_name (login_area, "ovirt-viewer-login-area"); + g_signal_connect (G_OBJECT (la_username), "key-release-event", + G_CALLBACK (login_to_wui_on_enter), NULL); + g_signal_connect (G_OBJECT (la_password), "key-release-event", + G_CALLBACK (login_to_wui_on_enter), NULL); g_signal_connect (G_OBJECT (la_button), "clicked", G_CALLBACK (login_to_wui), NULL); @@ -459,6 +495,21 @@ help_about (GtkWidget *menu) } static void +refresh_menu_vm_list(GtkWidget *widget, gpointer data) +{ + wui_thread_send_refresh_vm_list(); +} + +static void +connect_to_wui_on_enter (GtkWidget *widget, gpointer data) +{ + // if key released was not 'enter' key + if(((GdkEventKey *)data)->type == GDK_KEY_RELEASE && (((GdkEventKey *)data)->keyval & 0xFF) != 13 ) return; + + connect_to_wui(widget, data); +} + +static void connect_to_wui (GtkWidget *widget, gpointer data) { const char *hostname; @@ -477,6 +528,15 @@ connect_to_wui (GtkWidget *widget, gpointer data) } static void +login_to_wui_on_enter (GtkWidget *widget, gpointer data) +{ + // if key released was not 'enter' key + if(((GdkEventKey *)data)->type == GDK_KEY_RELEASE && (((GdkEventKey *)data)->keyval & 0xFF) != 13 ) return; + + login_to_wui(widget, data); +} + +static void login_to_wui (GtkWidget *widget, gpointer data) { const char *username, *password; @@ -501,7 +561,9 @@ connect_to_vm (GtkWidget *widget, gpointer _vm) int i, uuidlen, len, fd; GtkWidget *child; const char *label; + const char* hostname; char *label2; + char new_title[97]; // 47 chars for title + 50 for vm name DEBUG ("searching tabs for uuid %s", vm->uuid); @@ -522,11 +584,17 @@ connect_to_vm (GtkWidget *widget, gpointer _vm) DEBUG ("not found, creating new tab"); + // before we open vnc connection, make sure vm is running + gboolean vm_running = main_vmlist_has_running_vm(vm); + + if(!vm_running){ + main_status_error (g_strdup ("VM not running")); + return; + } + /* 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 (); @@ -535,6 +603,16 @@ connect_to_vm (GtkWidget *widget, gpointer _vm) return; } + main_status_error(g_strdup("")); + + for(i = 0; i < 47; ++i) new_title[i] = title_vm[i]; + for(i = 0; i < 50; ++i){ + if(vm->description[i] == '\0') break; + new_title[47 + i] = vm->description[i]; + } + new_title[i < 50 ? i+47 : 96] = '\0'; + gtk_window_set_title (GTK_WINDOW (window), new_title); + /* gtk_signal_connect(GTK_OBJECT(child), "vnc-pointer-grab", GTK_SIGNAL_FUNC(viewer_grab), window); @@ -740,70 +818,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; - - if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd) < 0) - return -1; - - pid = fork(); - if (pid == -1) { - close(fd[0]); - close(fd[1]); - return -1; - } + int socketfd; + struct hostent *serv; + struct sockaddr_in serv_addr; - 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]; -} - -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; + socketfd = socket(PF_INET, SOCK_STREAM, 0); + if(socketfd < 0){ + return -1; + } - if (!sshport) - sshport = 22; + serv = gethostbyname(vnchost); + if(serv == NULL){ + return -1; + } - snprintf (portstr, sizeof portstr, "%d", sshport); - snprintf (portstr2, sizeof portstr2, "%d", vncport); + 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; - 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 @@ -938,9 +983,7 @@ main_login_error (gpointer _str) DEBUG ("login error: %s", str); ASSERT_IS_MAIN_THREAD (); - /* gtk_label_set_text (GTK_LABEL (la_error), str); - */ g_free (str); return FALSE; @@ -1011,6 +1054,9 @@ add_vm_to_connectmenu (gpointer _vm, gpointer data) struct vm *vm = (struct vm *) _vm; GtkWidget *item; + // TODO only present running vms ? + // if(vm->state == "running") + DEBUG ("adding %s to Connect menu", vm->description); item = gtk_menu_item_new_with_label (vm->description);