c6d091d917d816d34d5493462e27457488c0ac7c
[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 // port which to connect to on ovirt server
57 const int OVIRT_SERVER_PORT = 5900;
58
59 /* Private thread functions */
60 static gpointer tunnel_thread(gpointer data);
61 static gpointer client_server_thread(gpointer data);
62 static gpointer server_client_thread(gpointer data);
63
64 /* Other private functions */
65 static void close_socket(gpointer _socket, gpointer data);
66 static void wait_for_thread(gpointer _thread, gpointer data);
67
68 /* tunnel and main threads */
69 static GThread *tunnel_gthread = NULL;
70 static GThread *main_gthread = NULL;
71
72 /* list of communication threads */
73 static GSList *communication_threads = NULL;
74
75 /* list of sockets */
76 static GSList *sockets = NULL;
77
78 /* thread termination flag */
79 static gboolean run_tunnel = FALSE;
80
81 /*  internal.h shared constructs */
82 int tunnel_port;
83
84 /////////////////
85
86 /** public implementations **/
87
88 /* start tunnel thread */
89 void 
90 start_tunnel(void)
91 {
92   GError *error = NULL;
93
94   DEBUG ("starting the tunnel thread");
95
96   assert (tunnel_gthread == NULL);
97
98   run_tunnel = TRUE;
99
100   main_gthread = g_thread_self ();
101
102   tunnel_gthread = g_thread_create (tunnel_thread, NULL, TRUE, &error);
103   if (error) {
104     g_print ("%s\n", error->message);
105     g_error_free (error);
106     exit (1);
107   }
108 };
109
110 /* stop tunnel thread */
111 void 
112 stop_tunnel(void)
113 {
114   if(!run_tunnel)
115       return;
116
117   DEBUG ("stopping the tunnel thread");
118
119   assert (tunnel_gthread != NULL);
120   ASSERT_IS_MAIN_THREAD ();
121
122   run_tunnel = FALSE;
123
124   g_slist_foreach(sockets, close_socket, NULL);
125
126   (void) g_thread_join (tunnel_gthread);
127   tunnel_gthread = NULL;
128 };
129
130 /////////////////
131
132 /** private implementations **/
133
134 /* the tunnel thread */
135 static gpointer
136 tunnel_thread (gpointer _data)
137 {
138   struct hostent *dns_serv;
139
140   //char vm_data[VM_NAME_MAX_LEN];
141   int local_server_socketfd, ovirt_server_socket, client_socketfd;
142   unsigned int local_server_len, client_len, ovirt_server_len;
143
144   struct sockaddr_in local_server_address;
145   struct sockaddr_in ovirt_server_address;
146   struct sockaddr_in client_address;
147
148   struct sockaddr_in local_server_address_lookup;
149   unsigned int local_server_address_lookup_len = sizeof(local_server_address_lookup);
150
151   GThread *client_server_gthread = NULL;
152   GThread *server_client_gthread = NULL;
153
154   int sockets_param[2];
155   int * c_socket;
156
157   DEBUG ("tunnel thread starting up");
158
159   // ovirt server address
160   dns_serv = gethostbyname(hostname);
161   if(dns_serv == NULL){
162       DEBUG("ovirt server lookup failed");
163       return NULL;
164   }
165   ovirt_server_address.sin_family = PF_INET;
166   ovirt_server_address.sin_addr.s_addr = ((struct in_addr*)(dns_serv->h_addr))->s_addr; //inet_addr(hostname);
167   ovirt_server_address.sin_port = htons(OVIRT_SERVER_PORT);
168   ovirt_server_len = sizeof(ovirt_server_address);
169
170   // create local net socket
171   local_server_socketfd = socket(PF_INET, SOCK_STREAM, 0);
172   c_socket = malloc(sizeof(int)); *c_socket = local_server_socketfd;
173   sockets = g_slist_prepend(sockets, c_socket);
174
175   // local server address
176   local_server_address.sin_family = PF_INET;
177   local_server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
178   local_server_address.sin_port = 0;
179   local_server_len = sizeof(local_server_address);
180
181   // increment ports until one is available
182   if(bind(local_server_socketfd, (struct sockaddr*)&local_server_address, local_server_len) < 0){
183      DEBUG("tunnel bind failed");
184      return NULL;
185   }
186
187   getsockname(local_server_socketfd,
188               (struct sockaddr*) &local_server_address_lookup,
189               &local_server_address_lookup_len);
190   tunnel_port = (int)ntohs(local_server_address_lookup.sin_port);
191   DEBUG ("tunnel bound to local port %i", tunnel_port);
192
193   // increase client buffer size?
194   listen(local_server_socketfd, 5);
195
196   while(run_tunnel) {
197      // accept a client connection
198      DEBUG("tunnel accepting");
199      client_len = sizeof(client_address);
200      client_socketfd = accept(local_server_socketfd, (struct sockaddr*)&client_address, &client_len);
201      if(client_socketfd < 0){
202          DEBUG("tunnel accept failed");
203          break;
204      }
205      // TODO check accept return value for err
206      c_socket = malloc(sizeof(int)); *c_socket = client_socketfd;
207      sockets = g_slist_prepend(sockets, c_socket);
208
209      DEBUG ("client connected to tunnel");
210
211      // establish connection w/ ovirt server
212      ovirt_server_socket = socket(PF_INET, SOCK_STREAM, 0);
213      c_socket = malloc(sizeof(int)); *c_socket = ovirt_server_socket;
214      sockets = g_slist_prepend(sockets, c_socket);
215      DEBUG ("connecting to ovirt server %s on %i", hostname, OVIRT_SERVER_PORT);
216      if(connect(ovirt_server_socket, (struct sockaddr*)&ovirt_server_address, ovirt_server_len) < 0){
217        DEBUG ("could not connect to ovirt server");
218        break;
219        //return NULL;
220      }
221      DEBUG ("connected to ovirt server");
222
223      sockets_param[0]  = ovirt_server_socket; 
224      sockets_param[1]  = client_socketfd;
225
226      // launch thread for client -> server traffic
227      client_server_gthread = g_thread_create (client_server_thread, 
228                                               &sockets_param, TRUE, NULL);
229
230      // launch thread for server -> client traffic
231      server_client_gthread = g_thread_create (server_client_thread, 
232                                               &sockets_param, TRUE, NULL);
233
234      communication_threads = g_slist_prepend(communication_threads, client_server_gthread);
235      communication_threads = g_slist_prepend(communication_threads, server_client_gthread);
236
237      // send target vm for this session
238      //strcpy(vm_data, vm_in_focus->description);
239      DEBUG ("sending vm %s", vm_in_focus->description);
240      write(ovirt_server_socket, vm_in_focus->description, strlen(vm_in_focus->description));
241
242   }
243
244   DEBUG("terminating tunnel thread");
245
246   // wait for connection threads to finish
247   g_slist_foreach(communication_threads, wait_for_thread, NULL);
248
249   DEBUG ("tunnel thread completed");
250   return NULL;
251 };
252
253 /* the tunnel thread */
254 static gpointer
255 client_server_thread (gpointer _data){
256   int nbytes;
257   char vnc_data[VNC_DATA_MAX_LEN];
258
259   int ovirt_server_socket = ((int*)_data)[0],
260       client_socket = ((int*)_data)[1];
261
262   DEBUG ("client/server thread starting up");
263   
264   while(run_tunnel){
265     VERBOSE( "accepting client data");
266
267     // grab vnc data
268     nbytes = read(client_socket, vnc_data, VNC_DATA_MAX_LEN);
269     if(nbytes <= 0){
270       DEBUG ( "error reading data from client" );
271       break;
272     }
273     VERBOSE ("read %i bytes from client", nbytes);
274         
275     // send network_data onto server
276     nbytes = write(ovirt_server_socket, vnc_data, nbytes);
277     if(nbytes <= 0){
278       DEBUG ( "error writing data to server" );
279       break;
280     }
281     VERBOSE ("wrote %i bytes to server", nbytes);
282   }
283
284   DEBUG ("client/server thread completed");
285   return NULL;
286 };
287
288 /* the server thread */
289 static gpointer
290 server_client_thread (gpointer _data){
291   char vnc_data[VNC_DATA_MAX_LEN];
292
293   int ovirt_server_socket = ((int*)_data)[0],
294       client_socket = ((int*)_data)[1];
295
296   int nbytes;
297
298   DEBUG ("server/client thread starting up");
299   
300   while(run_tunnel){
301      // grab vnc data
302      nbytes = read(ovirt_server_socket, vnc_data, VNC_DATA_MAX_LEN);
303      if(nbytes <= 0){
304        DEBUG ( "error reading data from server" );
305        break;
306      }
307     VERBOSE ("read %i bytes from server", nbytes);
308
309     // send network_data onto client
310     nbytes = write(client_socket, vnc_data, nbytes);
311     if(nbytes <= 0){
312       DEBUG ( "error writing data to client" );
313       break;
314     }
315     VERBOSE ("wrote %i bytes to client", nbytes);
316   }
317
318   DEBUG ("server/client thread completed");
319   return NULL;
320 };
321
322 static void close_socket(gpointer _socket, gpointer data){
323   shutdown(*(int*) _socket, 2);
324   close(*(int*) _socket);
325   free((int*) _socket);
326 };
327
328 static void wait_for_thread(gpointer _thread, gpointer data){
329   g_thread_join((GThread*)_thread);
330 };