- perform dns lookup on hostname, - randomize local tunnel port - bump rpm spec version updated to include danpb's feedback regarding getaddrinfo --- main.c | 36 ++++++++++++++++++------------------ ovirt-viewer.spec | 9 ++++++++- tunnel.c | 29 ++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/main.c b/main.c index 2962f3f..23af6fa 100644 --- 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 @@ -903,37 +905,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 diff --git a/ovirt-viewer.spec b/ovirt-viewer.spec index a320ecd..a4d826e 100644 --- a/ovirt-viewer.spec +++ b/ovirt-viewer.spec @@ -2,7 +2,7 @@ Name: ovirt-viewer Version: 1.0.0 -Release: 2%{?dist} +Release: 4%{?dist} Summary: ovirt-viewer is a vnc viewer for ovirt managed vms Group: Applications/System @@ -61,6 +61,13 @@ rm -rf $RPM_BUILD_ROOT %{_bindir}/ovirt-viewer %changelog +* Mon Jun 01 2009 <mmorsi at redhat.com> - 1.0.0-4 +- bugfixes, dns lookup and local tunnel port assignment + +* Wed May 20 2009 <mmorsi at redhat.com> - 1.0.0-3 +- changes to viewer to connect to ovirt server + through vnc proxy + * Thu Apr 30 2009 <mmorsi at redhat.com> - 1.0.0-2 - ssl support, see check-certificate cmd line flag - wire up send key menu diff --git a/tunnel.c b/tunnel.c index a03ef0b..c6d091d 100644 --- a/tunnel.c +++ b/tunnel.c @@ -34,6 +34,7 @@ #include <sys/socket.h> #include <stdlib.h> #include <stdio.h> +#include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> @@ -46,9 +47,6 @@ /* constants */ -// port to try to listen on, if we can't, increment until we find one we can -const int PORT_RANGE_START = 5600; - // max length of a vm name const int VM_NAME_MAX_LEN = 250; @@ -137,6 +135,8 @@ stop_tunnel(void) static gpointer tunnel_thread (gpointer _data) { + struct hostent *dns_serv; + //char vm_data[VM_NAME_MAX_LEN]; int local_server_socketfd, ovirt_server_socket, client_socketfd; unsigned int local_server_len, client_len, ovirt_server_len; @@ -145,6 +145,9 @@ tunnel_thread (gpointer _data) struct sockaddr_in ovirt_server_address; struct sockaddr_in client_address; + struct sockaddr_in local_server_address_lookup; + unsigned int local_server_address_lookup_len = sizeof(local_server_address_lookup); + GThread *client_server_gthread = NULL; GThread *server_client_gthread = NULL; @@ -154,8 +157,13 @@ tunnel_thread (gpointer _data) DEBUG ("tunnel thread starting up"); // ovirt server address + dns_serv = gethostbyname(hostname); + if(dns_serv == NULL){ + DEBUG("ovirt server lookup failed"); + return NULL; + } ovirt_server_address.sin_family = PF_INET; - ovirt_server_address.sin_addr.s_addr = inet_addr(hostname); + ovirt_server_address.sin_addr.s_addr = ((struct in_addr*)(dns_serv->h_addr))->s_addr; //inet_addr(hostname); ovirt_server_address.sin_port = htons(OVIRT_SERVER_PORT); ovirt_server_len = sizeof(ovirt_server_address); @@ -165,18 +173,21 @@ tunnel_thread (gpointer _data) sockets = g_slist_prepend(sockets, c_socket); // local server address - tunnel_port = PORT_RANGE_START; local_server_address.sin_family = PF_INET; local_server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); - local_server_address.sin_port = htons(tunnel_port); + local_server_address.sin_port = 0; local_server_len = sizeof(local_server_address); // increment ports until one is available - while(bind(local_server_socketfd, (struct sockaddr*)&local_server_address, local_server_len) < 0){ - tunnel_port += 1; - local_server_address.sin_port += htons(tunnel_port); + if(bind(local_server_socketfd, (struct sockaddr*)&local_server_address, local_server_len) < 0){ + DEBUG("tunnel bind failed"); + return NULL; } + getsockname(local_server_socketfd, + (struct sockaddr*) &local_server_address_lookup, + &local_server_address_lookup_len); + tunnel_port = (int)ntohs(local_server_address_lookup.sin_port); DEBUG ("tunnel bound to local port %i", tunnel_port); // increase client buffer size? -- 1.6.0.6
Ug, sorry once again this is for the viewer. I switched machines and forgot to add the relevant configuration to git. -Mo
Mohammed Morsi wrote:> - perform dns lookup on hostname, > - randomize local tunnel port > - bump rpm spec version > > updated to include danpb's feedback regarding getaddrinfo > --- > main.c | 36 ++++++++++++++++++------------------ > ovirt-viewer.spec | 9 ++++++++- > tunnel.c | 29 ++++++++++++++++++++--------- > 3 files changed, 46 insertions(+), 28 deletions(-) > > diff --git a/main.c b/main.c > index 2962f3f..23af6fa 100644 > --- 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 > @@ -903,37 +905,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 > diff --git a/ovirt-viewer.spec b/ovirt-viewer.spec > index a320ecd..a4d826e 100644 > --- a/ovirt-viewer.spec > +++ b/ovirt-viewer.spec > @@ -2,7 +2,7 @@ > > Name: ovirt-viewer > Version: 1.0.0 > -Release: 2%{?dist} > +Release: 4%{?dist} > Summary: ovirt-viewer is a vnc viewer for ovirt managed vms > > Group: Applications/System > @@ -61,6 +61,13 @@ rm -rf $RPM_BUILD_ROOT > %{_bindir}/ovirt-viewer > > %changelog > +* Mon Jun 01 2009 <mmorsi at redhat.com> - 1.0.0-4 > +- bugfixes, dns lookup and local tunnel port assignment > + > +* Wed May 20 2009 <mmorsi at redhat.com> - 1.0.0-3 > +- changes to viewer to connect to ovirt server > + through vnc proxy > + > * Thu Apr 30 2009 <mmorsi at redhat.com> - 1.0.0-2 > - ssl support, see check-certificate cmd line flag > - wire up send key menu > diff --git a/tunnel.c b/tunnel.c > index a03ef0b..c6d091d 100644 > --- a/tunnel.c > +++ b/tunnel.c > @@ -34,6 +34,7 @@ > #include <sys/socket.h> > #include <stdlib.h> > #include <stdio.h> > +#include <netdb.h> > #include <netinet/in.h> > #include <arpa/inet.h> > #include <unistd.h> > @@ -46,9 +47,6 @@ > > /* constants */ > > -// port to try to listen on, if we can't, increment until we find one we can > -const int PORT_RANGE_START = 5600; > - > // max length of a vm name > const int VM_NAME_MAX_LEN = 250; > > @@ -137,6 +135,8 @@ stop_tunnel(void) > static gpointer > tunnel_thread (gpointer _data) > { > + struct hostent *dns_serv; > + > //char vm_data[VM_NAME_MAX_LEN]; > int local_server_socketfd, ovirt_server_socket, client_socketfd; > unsigned int local_server_len, client_len, ovirt_server_len; > @@ -145,6 +145,9 @@ tunnel_thread (gpointer _data) > struct sockaddr_in ovirt_server_address; > struct sockaddr_in client_address; > > + struct sockaddr_in local_server_address_lookup; > + unsigned int local_server_address_lookup_len = sizeof(local_server_address_lookup); > + > GThread *client_server_gthread = NULL; > GThread *server_client_gthread = NULL; > > @@ -154,8 +157,13 @@ tunnel_thread (gpointer _data) > DEBUG ("tunnel thread starting up"); > > // ovirt server address > + dns_serv = gethostbyname(hostname); > + if(dns_serv == NULL){ > + DEBUG("ovirt server lookup failed"); > + return NULL; > + } > ovirt_server_address.sin_family = PF_INET; > - ovirt_server_address.sin_addr.s_addr = inet_addr(hostname); > + ovirt_server_address.sin_addr.s_addr = ((struct in_addr*)(dns_serv->h_addr))->s_addr; //inet_addr(hostname); > ovirt_server_address.sin_port = htons(OVIRT_SERVER_PORT); > ovirt_server_len = sizeof(ovirt_server_address); > > @@ -165,18 +173,21 @@ tunnel_thread (gpointer _data) > sockets = g_slist_prepend(sockets, c_socket); > > // local server address > - tunnel_port = PORT_RANGE_START; > local_server_address.sin_family = PF_INET; > local_server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); > - local_server_address.sin_port = htons(tunnel_port); > + local_server_address.sin_port = 0; > local_server_len = sizeof(local_server_address); > > // increment ports until one is available > - while(bind(local_server_socketfd, (struct sockaddr*)&local_server_address, local_server_len) < 0){ > - tunnel_port += 1; > - local_server_address.sin_port += htons(tunnel_port); > + if(bind(local_server_socketfd, (struct sockaddr*)&local_server_address, local_server_len) < 0){ > + DEBUG("tunnel bind failed"); > + return NULL; > } > > + getsockname(local_server_socketfd, > + (struct sockaddr*) &local_server_address_lookup, > + &local_server_address_lookup_len); > + tunnel_port = (int)ntohs(local_server_address_lookup.sin_port); > DEBUG ("tunnel bound to local port %i", tunnel_port); > > // increase client buffer size? >ACK