2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is libguac-client-rdp.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2011
20 * the Initial Developer. All Rights Reserved.
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.
36 * ***** END LICENSE BLOCK ***** */
41 #include <sys/select.h>
44 #include <freerdp/freerdp.h>
45 #include <freerdp/chanman.h>
47 #include <guacamole/log.h>
48 #include <guacamole/guacio.h>
49 #include <guacamole/protocol.h>
50 #include <guacamole/client.h>
52 #include "rdp_handlers.h"
54 #define RDP_DEFAULT_PORT 3389
56 /* Client plugin arguments */
57 const char* GUAC_CLIENT_ARGS[] = {
63 typedef struct rdp_guac_client_data {
69 } rdp_guac_client_data;
71 int rdp_guac_client_free_handler(guac_client* client) {
73 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
75 /* Disconnect client */
76 guac_client_data->rdp_inst->rdp_disconnect(guac_client_data->rdp_inst);
79 freerdp_free(guac_client_data->rdp_inst);
80 freerdp_chanman_free(guac_client_data->chanman);
81 free(guac_client_data->settings);
83 /* Free guac client data */
84 free(guac_client_data);
90 int rdp_guac_client_handle_messages(guac_client* client) {
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;
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.");
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.");
117 /* Construct read fd_set */
120 for (index = 0; index < read_count; index++) {
121 fd = (int)(long) (read_fds[index]);
127 /* Construct write fd_set */
129 for (index = 0; index < write_count; index++) {
130 fd = (int)(long) (write_fds[index]);
136 /* If no file descriptors, error */
138 guac_log_error("No file descriptors");
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 */
150 guac_log_error("Error waiting for file descriptor.");
155 /* check the libfreerdp fds */
156 if (rdp_inst->rdp_check_fds(rdp_inst) != 0) {
157 guac_log_error("Error handling RDP file descriptors.");
161 /* check channel fds */
162 if (freerdp_chanman_check_fds(chanman, rdp_inst) != 0) {
163 guac_log_error("Error handling RDP channel file descriptors.");
172 int guac_client_init(guac_client* client, int argc, char** argv) {
174 rdp_guac_client_data* guac_client_data;
181 int port = RDP_DEFAULT_PORT;
184 guac_send_error(client->io, "Wrong argument count received.");
185 guac_flush(client->io);
189 /* If port specified, use it */
190 if (argv[1][0] != '\0')
191 port = atoi(argv[1]);
195 /* Allocate client data */
196 guac_client_data = malloc(sizeof(rdp_guac_client_data));
198 /* Get channel manager */
199 chanman = freerdp_chanman_new();
200 guac_client_data->chanman = chanman;
203 settings = malloc(sizeof(rdpSet));
204 memset(settings, 0, sizeof(rdpSet));
205 guac_client_data->settings = settings;
208 strncpy(settings->hostname, hostname, sizeof(settings->hostname) - 1);
211 settings->width = 1024;
212 settings->height = 768;
214 strncpy(settings->server, hostname, sizeof(settings->server));
215 strcpy(settings->username, "guest");
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;
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);
237 guac_client_data->rdp_inst = rdp_inst;
239 /* Store client data */
240 rdp_inst->param1 = client;
241 client->data = guac_client_data;
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;
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);
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);
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);
309 /* Client handlers */
310 client->free_handler = rdp_guac_client_free_handler;
311 client->handle_messages = rdp_guac_client_handle_messages;