f67b4409c0a362a4e7f2b29af49d27bf991a4459
[libguac-client-rdp.git] / src / rdp_client.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is libguac-client-rdp.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2011
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <sys/select.h>
42 #include <errno.h>
43
44 #include <freerdp/freerdp.h>
45 #include <freerdp/chanman.h>
46
47 #include <guacamole/log.h>
48 #include <guacamole/guacio.h>
49 #include <guacamole/protocol.h>
50 #include <guacamole/client.h>
51
52 #include "rdp_handlers.h"
53
54 #define RDP_DEFAULT_PORT 3389
55
56 /* Client plugin arguments */
57 const char* GUAC_CLIENT_ARGS[] = {
58     "hostname",
59     "port",
60     NULL
61 };
62
63 typedef struct rdp_guac_client_data {
64
65     rdpInst* rdp_inst;
66     rdpChanMan* chanman;
67         rdpSet* settings;
68
69 } rdp_guac_client_data;
70
71 int rdp_guac_client_free_handler(guac_client* client) {
72
73     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
74
75     /* Disconnect client */
76     guac_client_data->rdp_inst->rdp_disconnect(guac_client_data->rdp_inst);
77
78     /* Free RDP client */
79     freerdp_free(guac_client_data->rdp_inst);
80     freerdp_chanman_free(guac_client_data->chanman);
81     free(guac_client_data->settings);
82
83     /* Free guac client data */
84     free(guac_client_data);
85
86     return 0;
87
88 }
89
90 int rdp_guac_client_handle_messages(guac_client* client) {
91
92     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
93     rdpInst* rdp_inst = guac_client_data->rdp_inst;
94     rdpChanMan* chanman = guac_client_data->chanman;
95
96     int index;
97     int max_fd, fd;
98     void* read_fds[32];
99     void* write_fds[32];
100     int read_count = 0;
101     int write_count = 0;
102
103     fd_set rfds, wfds;
104
105     /* get rdp fds */
106     if (rdp_inst->rdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
107         guac_log_error("Unable to read RDP file descriptors.");
108         return 1;
109     }
110
111     /* get channel fds */
112     if (freerdp_chanman_get_fds(chanman, rdp_inst, read_fds, &read_count, write_fds, &write_count) != 0) {
113         guac_log_error("Unable to read RDP channel file descriptors.");
114         return 1;
115     }
116
117     /* Construct read fd_set */
118     max_fd = 0;
119     FD_ZERO(&rfds);
120     for (index = 0; index < read_count; index++) {
121         fd = (int)(long) (read_fds[index]);
122         if (fd > max_fd)
123             max_fd = fd;
124         FD_SET(fd, &rfds);
125     }
126
127     /* Construct write fd_set */
128     FD_ZERO(&wfds);
129     for (index = 0; index < write_count; index++) {
130         fd = (int)(long) (write_fds[index]);
131         if (fd > max_fd)
132             max_fd = fd;
133         FD_SET(fd, &wfds);
134     }
135
136     /* If no file descriptors, error */
137     if (max_fd == 0) {
138         guac_log_error("No file descriptors");
139         return 1;
140     }
141
142     /* Otherwise, wait for file descriptors given */
143     if (select(max_fd + 1, &rfds, &wfds, NULL, NULL) == -1) {
144         /* these are not really errors */
145         if (!((errno == EAGAIN) ||
146             (errno == EWOULDBLOCK) ||
147             (errno == EINPROGRESS) ||
148             (errno == EINTR))) /* signal occurred */
149         {
150             guac_log_error("Error waiting for file descriptor.");
151             return 1;
152         }
153     }
154
155     /* check the libfreerdp fds */
156     if (rdp_inst->rdp_check_fds(rdp_inst) != 0) {
157         guac_log_error("Error handling RDP file descriptors.");
158         return 1;
159     }
160
161     /* check channel fds */
162     if (freerdp_chanman_check_fds(chanman, rdp_inst) != 0) {
163         guac_log_error("Error handling RDP channel file descriptors.");
164         return 1;
165     }
166
167     /* Success */
168     return 0;
169
170 }
171
172 int guac_client_init(guac_client* client, int argc, char** argv) {
173
174     rdp_guac_client_data* guac_client_data;
175
176     rdpInst* rdp_inst;
177     rdpChanMan* chanman;
178         rdpSet* settings;
179
180     char* hostname;
181     int port = RDP_DEFAULT_PORT;
182
183     if (argc < 2) {
184         guac_send_error(client->io, "Wrong argument count received.");
185         guac_flush(client->io);
186         return 1;
187     }
188
189     /* If port specified, use it */
190     if (argv[1][0] != '\0')
191         port = atoi(argv[1]);
192
193     hostname = argv[0];
194
195     /* Allocate client data */
196     guac_client_data = malloc(sizeof(rdp_guac_client_data));
197
198     /* Get channel manager */
199     chanman = freerdp_chanman_new();
200     guac_client_data->chanman = chanman;
201
202     /* INIT SETTINGS */
203     settings = malloc(sizeof(rdpSet));
204         memset(settings, 0, sizeof(rdpSet));
205     guac_client_data->settings = settings;
206
207     /* Set hostname */
208     strncpy(settings->hostname, hostname, sizeof(settings->hostname) - 1);
209
210     /* Default size */
211         settings->width = 1024;
212         settings->height = 768;
213
214         strncpy(settings->server, hostname, sizeof(settings->server));
215         strcpy(settings->username, "guest");
216
217         settings->tcp_port_rdp = port;
218         settings->encryption = 1;
219         settings->server_depth = 16;
220         settings->bitmap_cache = 1;
221         settings->bitmap_compression = 1;
222         settings->desktop_save = 0;
223         settings->rdp5_performanceflags =
224                 RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS;
225         settings->off_screen_bitmaps = 1;
226         settings->triblt = 0;
227         settings->new_cursors = 1;
228         settings->rdp_version = 5;
229
230     /* Init client */
231     rdp_inst = freerdp_new(settings);
232     if (rdp_inst == NULL) {
233         guac_send_error(client->io, "Error initializing RDP client");
234         guac_flush(client->io);
235         return 1;
236     }
237     guac_client_data->rdp_inst = rdp_inst;
238
239     /* Store client data */
240     rdp_inst->param1 = client;
241     client->data = guac_client_data;
242
243     /* RDP handlers */
244     rdp_inst->ui_error = guac_rdp_ui_error;
245         rdp_inst->ui_warning = guac_rdp_ui_warning;
246         rdp_inst->ui_unimpl = guac_rdp_ui_unimpl;
247         rdp_inst->ui_begin_update = guac_rdp_ui_begin_update;
248         rdp_inst->ui_end_update = guac_rdp_ui_end_update;
249         rdp_inst->ui_desktop_save = guac_rdp_ui_desktop_save;
250         rdp_inst->ui_desktop_restore = guac_rdp_ui_desktop_restore;
251         rdp_inst->ui_create_bitmap = guac_rdp_ui_create_bitmap;
252         rdp_inst->ui_paint_bitmap = guac_rdp_ui_paint_bitmap;
253         rdp_inst->ui_destroy_bitmap = guac_rdp_ui_destroy_bitmap;
254         rdp_inst->ui_line = guac_rdp_ui_line;
255         rdp_inst->ui_rect = guac_rdp_ui_rect;
256         rdp_inst->ui_polygon = guac_rdp_ui_polygon;
257         rdp_inst->ui_polyline = guac_rdp_ui_polyline;
258         rdp_inst->ui_ellipse = guac_rdp_ui_ellipse;
259         rdp_inst->ui_start_draw_glyphs = guac_rdp_ui_start_draw_glyphs;
260         rdp_inst->ui_draw_glyph = guac_rdp_ui_draw_glyph;
261         rdp_inst->ui_end_draw_glyphs = guac_rdp_ui_end_draw_glyphs;
262         rdp_inst->ui_get_toggle_keys_state = guac_rdp_ui_get_toggle_keys_state;
263         rdp_inst->ui_bell = guac_rdp_ui_bell;
264         rdp_inst->ui_destblt = guac_rdp_ui_destblt;
265         rdp_inst->ui_patblt = guac_rdp_ui_patblt;
266         rdp_inst->ui_screenblt = guac_rdp_ui_screenblt;
267         rdp_inst->ui_memblt = guac_rdp_ui_memblt;
268         rdp_inst->ui_triblt = guac_rdp_ui_triblt;
269         rdp_inst->ui_create_glyph = guac_rdp_ui_create_glyph;
270         rdp_inst->ui_destroy_glyph = guac_rdp_ui_destroy_glyph;
271     rdp_inst->ui_select = guac_rdp_ui_select;
272         rdp_inst->ui_set_clip = guac_rdp_ui_set_clip;
273         rdp_inst->ui_reset_clip = guac_rdp_ui_reset_clip;
274         rdp_inst->ui_resize_window = guac_rdp_ui_resize_window;
275         rdp_inst->ui_set_cursor = guac_rdp_ui_set_cursor;
276         rdp_inst->ui_destroy_cursor = guac_rdp_ui_destroy_cursor;
277         rdp_inst->ui_create_cursor = guac_rdp_ui_create_cursor;
278         rdp_inst->ui_set_null_cursor = guac_rdp_ui_set_null_cursor;
279         rdp_inst->ui_set_default_cursor = guac_rdp_ui_set_default_cursor;
280         rdp_inst->ui_create_colourmap = guac_rdp_ui_create_colourmap;
281         rdp_inst->ui_move_pointer = guac_rdp_ui_move_pointer;
282         rdp_inst->ui_set_colourmap = guac_rdp_ui_set_colourmap;
283         rdp_inst->ui_create_surface = guac_rdp_ui_create_surface;
284         rdp_inst->ui_set_surface = guac_rdp_ui_set_surface;
285         rdp_inst->ui_destroy_surface = guac_rdp_ui_destroy_surface;
286         rdp_inst->ui_channel_data = guac_rdp_ui_channel_data;
287
288     /* Init chanman (pre-connect) */
289     if (freerdp_chanman_pre_connect(chanman, rdp_inst)) {
290         guac_send_error(client->io, "Error initializing RDP client channel manager");
291         guac_flush(client->io);
292         return 1;
293     }
294
295     /* Connect to RDP server */
296     if (rdp_inst->rdp_connect(rdp_inst)) {
297         guac_send_error(client->io, "Error connecting to RDP server");
298         guac_flush(client->io);
299         return 1;
300     }
301
302     /* Init chanman (post-connect) */
303     if (freerdp_chanman_post_connect(chanman, rdp_inst)) {
304         guac_send_error(client->io, "Error initializing RDP client channel manager");
305         guac_flush(client->io);
306         return 1;
307     }
308
309     /* Client handlers */
310     client->free_handler = rdp_guac_client_free_handler;
311     client->handle_messages = rdp_guac_client_handle_messages;
312
313     /* Success */
314     return 0;
315
316 }
317