add cmd line option for server vnc port
[ovirt-viewer.git] / main.c
diff --git a/main.c b/main.c
index 2962f3f..dd26c93 100644 (file)
--- a/main.c
+++ b/main.c
@@ -36,6 +36,8 @@
 #include <netinet/in.h>
 #endif
 
+#include <arpa/inet.h>
+
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
@@ -73,6 +75,7 @@ static GSList *vmlist = NULL;
 /*  internal.h shared constructs */
 const char* hostname;
 struct vm* vm_in_focus;
+int ovirt_server_vnc_port = 5900;
 
 /* Private functions. */
 static void start_ui (void);
@@ -198,6 +201,8 @@ static const char *help_msg =
   "Use '" PACKAGE " --help' to see a list of available command line options";
 
 static const GOptionEntry options[] = {
+  { "port", 'p', 0, G_OPTION_ARG_INT, &ovirt_server_vnc_port,
+    "set port which to connect to server via vnc", NULL },
   { "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,
@@ -903,37 +908,35 @@ viewer_credential (GtkWidget *vnc, GValueArray *credList)
                 gtk_widget_destroy(GTK_WIDGET(dialog));
 }
 
-#if defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) && defined(HAVE_GETHOSTBYNAME)
+#if defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS)
 
 static int 
 viewer_open_vnc_socket(const char* vnchost, int vncport)
 {
-  int socketfd;
-  struct hostent *serv;
-  struct sockaddr_in serv_addr;
+  int result, socketfd;
+  char port[10];
+  struct addrinfo* vnc_addr;
 
-  socketfd = socket(PF_INET, SOCK_STREAM, 0);
-  if(socketfd < 0){
-      return -1;
-  }
+  sprintf(port, "%d", vncport);
 
-  serv = gethostbyname(vnchost);
-  if(serv == NULL){
+  result = getaddrinfo(vnchost, port, NULL, &vnc_addr);
+  if(result != 0 || vnc_addr == NULL)
       return -1;
-  }
 
-  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; 
+  // just use first found, ignoring rest
+  socketfd = socket(vnc_addr->ai_family,
+                    vnc_addr->ai_socktype,
+                    vnc_addr->ai_protocol);
 
-  if (connect(socketfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
-      return -1;
-  }
+  if(connect(socketfd, vnc_addr->ai_addr, vnc_addr->ai_addrlen) <0)
+      socketfd = -1;
+
+  freeaddrinfo(vnc_addr);
 
   return socketfd;
 }
 
-#endif /* defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) && defined(HAVE_GETHOSTBYNAME) */
+#endif /* defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) */
 
 /* Remove all menu items from the Connect menu. */
 static void