add cmd line option for server vnc port
[ovirt-viewer.git] / tunnel.c
1 /* ovirt viewer console application
2  * Copyright (C) 2008 Red Hat Inc.
3  * Written by Mohammed Morsi <mmorsi@redhat.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 /* ovirt-viewer starts listening on network port to 
21  *  encapsulate vnc packets including the vm's name
22  *  so as to be able to be proxied. 
23  *
24  * This operation takes place in another thread
25  * which can be started/stopped by calling 
26  * start_tunnel / stop_tunnel. 
27  *
28  * An additional connection thread is created and maintained
29  * internally for each vm / vnc connection open in ovirt-viewer
30  * establishing a connection w/ the ovirt server.
31  */
32
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <netdb.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <unistd.h>
41 #include <assert.h>
42 #include <string.h>
43
44 #include <glib.h>
45
46 #include "internal.h"
47
48 /* constants */
49
50 // max length of a vm name
51 const int VM_NAME_MAX_LEN = 250;
52
53 // max length of vnc data
54 const int VNC_DATA_MAX_LEN = 800000;
55
56 /* Private thread functions */
57 static gpointer tunnel_thread(gpointer data);
58 static gpointer client_server_thread(gpointer data);
59 static gpointer server_client_thread(gpointer data);
60
61 /* Other private functions */
62 static void close_socket(gpointer _socket, gpointer data);
63 static void wait_for_thread(gpointer _thread, gpointer data);
64
65 /* tunnel and main threads */
66 static GThread *tunnel_gthread = NULL;
67 static GThread *main_gthread = NULL;
68
69 /* list of communication threads */
70 static GSList *communication_threads = NULL;
71
72 /* list of sockets */
73 static GSList *sockets = NULL;
74
75 /* thread termination flag */
76 static gboolean run_tunnel = FALSE;
77
78 /*  internal.h shared constructs */
79 int tunnel_port;
80
81 /////////////////
82
83 /** public implementations **/
84
85 /* start tunnel thread */
86 void 
87 start_tunnel(void)
88 {
89   GError *error = NULL;
90
91   DEBUG ("starting the tunnel thread");
92
93   assert (tunnel_gthread == NULL);
94
95   run_tunnel = TRUE;
96
97   main_gthread = g_thread_self ();
98
99   tunnel_gthread = g_thread_create (tunnel_thread, NULL, TRUE, &error);
100   if (error) {
101     g_print ("%s\n", error->message);
102     g_error_free (error);
103     exit (1);
104   }
105 };
106
107 /* stop tunnel thread */
108 void 
109 stop_tunnel(void)
110 {
111   if(!run_tunnel)
112       return;
113
114   DEBUG ("stopping the tunnel thread");
115
116   assert (tunnel_gthread != NULL);
117   ASSERT_IS_MAIN_THREAD ();
118
119   run_tunnel = FALSE;
120
121   g_slist_foreach(sockets, close_socket, NULL);
122
123   (void) g_thread_join (tunnel_gthread);
124   tunnel_gthread = NULL;
125 };
126
127 /////////////////
128
129 /** private implementations **/
130
131 /* the tunnel thread */
132 static gpointer
133 tunnel_thread (gpointer _data)
134 {
135   struct hostent *dns_serv;
136
137   //char vm_data[VM_NAME_MAX_LEN];
138   int local_server_socketfd, ovirt_server_socket, client_socketfd;
139   unsigned int local_server_len, client_len, ovirt_server_len;
140
141   struct sockaddr_in local_server_address;
142   struct sockaddr_in ovirt_server_address;
143   struct sockaddr_in client_address;
144
145   struct sockaddr_in local_server_address_lookup;
146   unsigned int local_server_address_lookup_len = sizeof(local_server_address_lookup);
147
148   GThread *client_server_gthread = NULL;
149   GThread *server_client_gthread = NULL;
150
151   int sockets_param[2];
152   int * c_socket;
153
154   DEBUG ("tunnel thread starting up");
155
156   // ovirt server address
157   dns_serv = gethostbyname(hostname);
158   if(dns_serv == NULL){
159       DEBUG("ovirt server lookup failed");
160       return NULL;
161   }
162   ovirt_server_address.sin_family = PF_INET;
163   ovirt_server_address.sin_addr.s_addr = ((struct in_addr*)(dns_serv->h_addr))->s_addr; //inet_addr(hostname);
164   ovirt_server_address.sin_port = htons(ovirt_server_vnc_port);
165   ovirt_server_len = sizeof(ovirt_server_address);
166
167   // create local net socket
168   local_server_socketfd = socket(PF_INET, SOCK_STREAM, 0);
169   c_socket = malloc(sizeof(int)); *c_socket = local_server_socketfd;
170   sockets = g_slist_prepend(sockets, c_socket);
171
172   // local server address
173   local_server_address.sin_family = PF_INET;
174   local_server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
175   local_server_address.sin_port = 0;
176   local_server_len = sizeof(local_server_address);
177
178   // increment ports until one is available
179   if(bind(local_server_socketfd, (struct sockaddr*)&local_server_address, local_server_len) < 0){
180      DEBUG("tunnel bind failed");
181      return NULL;
182   }
183
184   getsockname(local_server_socketfd,
185               (struct sockaddr*) &local_server_address_lookup,
186               &local_server_address_lookup_len);
187   tunnel_port = (int)ntohs(local_server_address_lookup.sin_port);
188   DEBUG ("tunnel bound to local port %i", tunnel_port);
189
190   // increase client buffer size?
191   listen(local_server_socketfd, 5);
192
193   while(run_tunnel) {
194      // accept a client connection
195      DEBUG("tunnel accepting");
196      client_len = sizeof(client_address);
197      client_socketfd = accept(local_server_socketfd, (struct sockaddr*)&client_address, &client_len);
198      if(client_socketfd < 0){
199          DEBUG("tunnel accept failed");
200          break;
201      }
202      // TODO check accept return value for err
203      c_socket = malloc(sizeof(int)); *c_socket = client_socketfd;
204      sockets = g_slist_prepend(sockets, c_socket);
205
206      DEBUG ("client connected to tunnel");
207
208      // establish connection w/ ovirt server
209      ovirt_server_socket = socket(PF_INET, SOCK_STREAM, 0);
210      c_socket = malloc(sizeof(int)); *c_socket = ovirt_server_socket;
211      sockets = g_slist_prepend(sockets, c_socket);
212      DEBUG ("connecting to ovirt server %s on %i", hostname, ovirt_server_vnc_port);
213      if(connect(ovirt_server_socket, (struct sockaddr*)&ovirt_server_address, ovirt_server_len) < 0){
214        DEBUG ("could not connect to ovirt server");
215        break;
216        //return NULL;
217      }
218      DEBUG ("connected to ovirt server");
219
220      sockets_param[0]  = ovirt_server_socket; 
221      sockets_param[1]  = client_socketfd;
222
223      // launch thread for client -> server traffic
224      client_server_gthread = g_thread_create (client_server_thread, 
225                                               &sockets_param, TRUE, NULL);
226
227      // launch thread for server -> client traffic
228      server_client_gthread = g_thread_create (server_client_thread, 
229                                               &sockets_param, TRUE, NULL);
230
231      communication_threads = g_slist_prepend(communication_threads, client_server_gthread);
232      communication_threads = g_slist_prepend(communication_threads, server_client_gthread);
233
234      // send target vm for this session
235      //strcpy(vm_data, vm_in_focus->description);
236      DEBUG ("sending vm %s", vm_in_focus->description);
237      write(ovirt_server_socket, vm_in_focus->description, strlen(vm_in_focus->description));
238
239   }
240
241   DEBUG("terminating tunnel thread");
242
243   // wait for connection threads to finish
244   g_slist_foreach(communication_threads, wait_for_thread, NULL);
245
246   DEBUG ("tunnel thread completed");
247   return NULL;
248 };
249
250 /* the tunnel thread */
251 static gpointer
252 client_server_thread (gpointer _data){
253   int nbytes;
254   char vnc_data[VNC_DATA_MAX_LEN];
255
256   int ovirt_server_socket = ((int*)_data)[0],
257       client_socket = ((int*)_data)[1];
258
259   DEBUG ("client/server thread starting up");
260   
261   while(run_tunnel){
262     VERBOSE( "accepting client data");
263
264     // grab vnc data
265     nbytes = read(client_socket, vnc_data, VNC_DATA_MAX_LEN);
266     if(nbytes <= 0){
267       DEBUG ( "error reading data from client" );
268       break;
269     }
270     VERBOSE ("read %i bytes from client", nbytes);
271         
272     // send network_data onto server
273     nbytes = write(ovirt_server_socket, vnc_data, nbytes);
274     if(nbytes <= 0){
275       DEBUG ( "error writing data to server" );
276       break;
277     }
278     VERBOSE ("wrote %i bytes to server", nbytes);
279   }
280
281   DEBUG ("client/server thread completed");
282   return NULL;
283 };
284
285 /* the server thread */
286 static gpointer
287 server_client_thread (gpointer _data){
288   char vnc_data[VNC_DATA_MAX_LEN];
289
290   int ovirt_server_socket = ((int*)_data)[0],
291       client_socket = ((int*)_data)[1];
292
293   int nbytes;
294
295   DEBUG ("server/client thread starting up");
296   
297   while(run_tunnel){
298      // grab vnc data
299      nbytes = read(ovirt_server_socket, vnc_data, VNC_DATA_MAX_LEN);
300      if(nbytes <= 0){
301        DEBUG ( "error reading data from server" );
302        break;
303      }
304     VERBOSE ("read %i bytes from server", nbytes);
305
306     // send network_data onto client
307     nbytes = write(client_socket, vnc_data, nbytes);
308     if(nbytes <= 0){
309       DEBUG ( "error writing data to client" );
310       break;
311     }
312     VERBOSE ("wrote %i bytes to client", nbytes);
313   }
314
315   DEBUG ("server/client thread completed");
316   return NULL;
317 };
318
319 static void close_socket(gpointer _socket, gpointer data){
320   shutdown(*(int*) _socket, 2);
321   close(*(int*) _socket);
322   free((int*) _socket);
323 };
324
325 static void wait_for_thread(gpointer _thread, gpointer data){
326   g_thread_join((GThread*)_thread);
327 };