2010-12-08 21:14:04 +00:00
|
|
|
|
2011-02-16 02:04:36 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2010-12-08 21:14:04 +00:00
|
|
|
*
|
2011-02-16 02:04:36 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2010-12-08 21:14:04 +00:00
|
|
|
*
|
2011-02-16 02:04:36 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2010-12-08 21:14:04 +00:00
|
|
|
*
|
2011-02-16 02:04:36 +00:00
|
|
|
* The Original Code is libguac.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Michael Jumper.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dlfcn.h>
|
2011-03-16 08:24:17 +00:00
|
|
|
#include <pthread.h>
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-02-09 09:39:27 +00:00
|
|
|
#include "log.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
#include "guacio.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "client.h"
|
2011-03-17 06:46:02 +00:00
|
|
|
#include "client-handlers.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
png_byte** guac_alloc_png_buffer(int w, int h, int bpp) {
|
|
|
|
|
|
|
|
png_byte** png_buffer;
|
|
|
|
png_byte* row;
|
|
|
|
int y;
|
|
|
|
|
|
|
|
/* Allocate rows for PNG */
|
|
|
|
png_buffer = (png_byte**) malloc(h * sizeof(png_byte*));
|
|
|
|
for (y=0; y<h; y++) {
|
|
|
|
row = (png_byte*) malloc(sizeof(png_byte) * bpp * w);
|
|
|
|
png_buffer[y] = row;
|
|
|
|
}
|
|
|
|
|
|
|
|
return png_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_free_png_buffer(png_byte** png_buffer, int h) {
|
|
|
|
|
|
|
|
int y;
|
|
|
|
|
|
|
|
/* Free PNG data */
|
|
|
|
for (y = 0; y<h; y++)
|
|
|
|
free(png_buffer[y]);
|
|
|
|
free(png_buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_client* __guac_alloc_client(GUACIO* io) {
|
|
|
|
|
|
|
|
/* Allocate new client (not handoff) */
|
|
|
|
guac_client* client = malloc(sizeof(guac_client));
|
|
|
|
memset(client, 0, sizeof(guac_client));
|
|
|
|
|
|
|
|
/* Init new client */
|
|
|
|
client->io = io;
|
2011-03-18 04:16:29 +00:00
|
|
|
client->last_received_timestamp = client->last_sent_timestamp = guac_current_timestamp();
|
2011-03-17 06:46:02 +00:00
|
|
|
client->state = RUNNING;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
guac_client* guac_get_client(int client_fd) {
|
|
|
|
|
|
|
|
guac_client* client;
|
|
|
|
GUACIO* io = guac_open(client_fd);
|
|
|
|
|
|
|
|
/* Pluggable client */
|
2011-02-28 22:51:18 +00:00
|
|
|
char protocol_lib[256] = "libguac-client-";
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
union {
|
|
|
|
guac_client_init_handler* client_init;
|
|
|
|
void* obj;
|
|
|
|
} alias;
|
|
|
|
|
|
|
|
char* error;
|
|
|
|
|
2011-01-01 21:22:17 +00:00
|
|
|
/* Client args description */
|
|
|
|
const char** client_args;
|
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
/* Client arguments */
|
|
|
|
int argc;
|
|
|
|
char** argv;
|
|
|
|
|
2011-01-01 21:22:17 +00:00
|
|
|
/* Instruction */
|
2010-12-08 21:14:04 +00:00
|
|
|
guac_instruction instruction;
|
|
|
|
|
2011-01-01 21:22:17 +00:00
|
|
|
/* Wait for select instruction */
|
2010-12-08 21:14:04 +00:00
|
|
|
for (;;) {
|
|
|
|
|
2011-03-16 06:02:47 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
/* Wait for data until timeout */
|
2011-03-16 08:24:17 +00:00
|
|
|
result = guac_instructions_waiting(io);
|
2011-03-16 06:02:47 +00:00
|
|
|
if (result == 0) {
|
|
|
|
guac_send_error(io, "Select timeout.");
|
|
|
|
guac_close(io);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If error occurs while waiting, exit with failure */
|
|
|
|
if (result < 0) {
|
|
|
|
guac_close(io);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = guac_read_instruction(io, &instruction);
|
2010-12-08 21:14:04 +00:00
|
|
|
if (result < 0) {
|
2010-12-22 10:04:09 +00:00
|
|
|
guac_close(io);
|
2010-12-08 21:14:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-01 21:22:17 +00:00
|
|
|
/* Select instruction read */
|
2010-12-08 21:14:04 +00:00
|
|
|
if (result > 0) {
|
|
|
|
|
2011-01-01 21:22:17 +00:00
|
|
|
if (strcmp(instruction.opcode, "select") == 0) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* Get protocol from message */
|
|
|
|
char* protocol = instruction.argv[0];
|
|
|
|
|
|
|
|
strcat(protocol_lib, protocol);
|
|
|
|
strcat(protocol_lib, ".so");
|
|
|
|
|
|
|
|
/* Create new client */
|
|
|
|
client = __guac_alloc_client(io);
|
|
|
|
|
|
|
|
/* Load client plugin */
|
|
|
|
client->client_plugin_handle = dlopen(protocol_lib, RTLD_LAZY);
|
|
|
|
if (!(client->client_plugin_handle)) {
|
2011-03-20 00:02:30 +00:00
|
|
|
guac_log_error("Could not open client plugin for protocol \"%s\": %s\n", protocol, dlerror());
|
2010-12-08 21:14:04 +00:00
|
|
|
guac_send_error(io, "Could not load server-side client plugin.");
|
|
|
|
guac_flush(io);
|
2010-12-22 10:04:09 +00:00
|
|
|
guac_close(io);
|
2010-12-08 21:14:04 +00:00
|
|
|
guac_free_instruction_data(&instruction);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlerror(); /* Clear errors */
|
|
|
|
|
|
|
|
/* Get init function */
|
|
|
|
alias.obj = dlsym(client->client_plugin_handle, "guac_client_init");
|
|
|
|
|
|
|
|
if ((error = dlerror()) != NULL) {
|
2011-03-20 00:02:30 +00:00
|
|
|
guac_log_error("Could not get guac_client_init in plugin: %s\n", error);
|
2010-12-08 21:14:04 +00:00
|
|
|
guac_send_error(io, "Invalid server-side client plugin.");
|
|
|
|
guac_flush(io);
|
2010-12-22 10:04:09 +00:00
|
|
|
guac_close(io);
|
2010-12-08 21:14:04 +00:00
|
|
|
guac_free_instruction_data(&instruction);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-01 21:22:17 +00:00
|
|
|
/* Get usage strig */
|
|
|
|
client_args = (const char**) dlsym(client->client_plugin_handle, "GUAC_CLIENT_ARGS");
|
|
|
|
|
|
|
|
if ((error = dlerror()) != NULL) {
|
2011-03-20 00:02:30 +00:00
|
|
|
guac_log_error("Could not get GUAC_CLIENT_ in plugin: %s\n", error);
|
2011-01-01 21:22:17 +00:00
|
|
|
guac_send_error(io, "Invalid server-side client plugin.");
|
|
|
|
guac_flush(io);
|
|
|
|
guac_close(io);
|
|
|
|
guac_free_instruction_data(&instruction);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-19 23:39:18 +00:00
|
|
|
if ( /* Send args */
|
|
|
|
guac_send_args(io, client_args)
|
|
|
|
|| guac_flush(io)
|
|
|
|
) {
|
|
|
|
guac_close(io);
|
|
|
|
guac_free_instruction_data(&instruction);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-01 21:22:17 +00:00
|
|
|
|
|
|
|
guac_free_instruction_data(&instruction);
|
|
|
|
break;
|
|
|
|
|
|
|
|
} /* end if select */
|
|
|
|
|
|
|
|
guac_free_instruction_data(&instruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for connect instruction */
|
|
|
|
for (;;) {
|
|
|
|
|
2011-03-16 06:02:47 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
/* Wait for data until timeout */
|
2011-03-16 08:24:17 +00:00
|
|
|
result = guac_instructions_waiting(io);
|
2011-03-16 06:02:47 +00:00
|
|
|
if (result == 0) {
|
|
|
|
guac_send_error(io, "Connect timeout.");
|
|
|
|
guac_close(io);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If error occurs while waiting, exit with failure */
|
|
|
|
if (result < 0) {
|
|
|
|
guac_close(io);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = guac_read_instruction(io, &instruction);
|
2011-01-01 21:22:17 +00:00
|
|
|
if (result < 0) {
|
2011-03-20 00:02:30 +00:00
|
|
|
guac_log_error("Error reading instruction while waiting for connect");
|
2011-01-01 21:22:17 +00:00
|
|
|
guac_close(io);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect instruction read */
|
|
|
|
if (result > 0) {
|
|
|
|
|
|
|
|
if (strcmp(instruction.opcode, "connect") == 0) {
|
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
/* Initialize client arguments */
|
|
|
|
argc = instruction.argc;
|
|
|
|
argv = instruction.argv;
|
|
|
|
|
|
|
|
if (alias.client_init(client, argc, argv) != 0) {
|
|
|
|
/* NOTE: On error, proxy client will send appropriate error message */
|
|
|
|
guac_free_instruction_data(&instruction);
|
2010-12-22 10:04:09 +00:00
|
|
|
guac_close(io);
|
2010-12-08 21:14:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_free_instruction_data(&instruction);
|
|
|
|
return client;
|
|
|
|
|
|
|
|
} /* end if connect */
|
|
|
|
|
|
|
|
guac_free_instruction_data(&instruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
void guac_client_stop(guac_client* client) {
|
|
|
|
client->state = STOPPING;
|
|
|
|
}
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
void guac_free_client(guac_client* client) {
|
|
|
|
|
|
|
|
if (client->free_handler) {
|
|
|
|
if (client->free_handler(client))
|
2011-03-20 00:02:30 +00:00
|
|
|
guac_log_error("Error calling client free handler");
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
guac_close(client->io);
|
|
|
|
|
|
|
|
/* Unload client plugin */
|
|
|
|
if (dlclose(client->client_plugin_handle)) {
|
2011-03-20 00:02:30 +00:00
|
|
|
guac_log_error("Could not close client plugin while unloading client: %s", dlerror());
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-16 08:24:17 +00:00
|
|
|
void* __guac_client_output_thread(void* data) {
|
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
guac_client* client = (guac_client*) data;
|
2011-03-16 08:24:17 +00:00
|
|
|
GUACIO* io = client->io;
|
|
|
|
|
|
|
|
/* Guacamole client output loop */
|
2011-03-17 06:46:02 +00:00
|
|
|
while (client->state == RUNNING) {
|
2011-03-16 08:24:17 +00:00
|
|
|
|
|
|
|
/* Occasionally ping client with sync */
|
2011-03-18 04:16:29 +00:00
|
|
|
long timestamp = guac_current_timestamp();
|
2011-03-17 06:46:02 +00:00
|
|
|
if (timestamp - client->last_sent_timestamp > GUAC_SYNC_FREQUENCY) {
|
|
|
|
client->last_sent_timestamp = timestamp;
|
2011-03-19 23:39:18 +00:00
|
|
|
if (
|
|
|
|
guac_send_sync(io, timestamp)
|
|
|
|
|| guac_flush(io)
|
|
|
|
) {
|
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-16 06:02:47 +00:00
|
|
|
}
|
|
|
|
|
2011-03-07 00:57:13 +00:00
|
|
|
/* Handle server messages */
|
|
|
|
if (client->handle_messages) {
|
|
|
|
|
2011-03-12 03:35:44 +00:00
|
|
|
/* Get previous GUACIO state */
|
|
|
|
int last_total_written = io->total_written;
|
|
|
|
|
2011-03-12 02:57:53 +00:00
|
|
|
/* Only handle messages if synced within threshold */
|
2011-03-17 06:46:02 +00:00
|
|
|
if (client->last_sent_timestamp - client->last_received_timestamp
|
2011-03-13 21:52:19 +00:00
|
|
|
< GUAC_SYNC_THRESHOLD) {
|
2011-03-07 00:57:13 +00:00
|
|
|
|
2011-03-12 02:57:53 +00:00
|
|
|
int retval = client->handle_messages(client);
|
|
|
|
if (retval) {
|
2011-03-20 00:02:30 +00:00
|
|
|
guac_log_error("Error handling server messages");
|
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
2011-03-12 02:57:53 +00:00
|
|
|
}
|
|
|
|
|
2011-03-12 03:35:44 +00:00
|
|
|
/* If data was written during message handling */
|
|
|
|
if (io->total_written != last_total_written) {
|
2011-03-13 21:52:19 +00:00
|
|
|
|
|
|
|
/* Sleep as necessary */
|
2011-03-18 04:16:29 +00:00
|
|
|
guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
|
2011-03-13 21:52:19 +00:00
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
/* Send sync instruction */
|
2011-03-18 04:16:29 +00:00
|
|
|
client->last_sent_timestamp = guac_current_timestamp();
|
2011-03-19 23:39:18 +00:00
|
|
|
if (guac_send_sync(io, client->last_sent_timestamp)) {
|
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-03-13 21:52:19 +00:00
|
|
|
|
2011-03-19 23:39:18 +00:00
|
|
|
if (guac_flush(io)) {
|
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
2011-03-12 03:35:44 +00:00
|
|
|
}
|
2011-03-12 02:57:53 +00:00
|
|
|
|
|
|
|
}
|
2011-03-07 00:57:13 +00:00
|
|
|
|
2011-03-17 21:04:23 +00:00
|
|
|
/* If sync threshold exceeded, don't spin waiting for resync */
|
|
|
|
else
|
2011-03-18 04:16:29 +00:00
|
|
|
guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
|
2011-03-17 21:04:23 +00:00
|
|
|
|
2011-03-07 00:57:13 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 08:24:17 +00:00
|
|
|
/* If no message handler, just sleep until next sync ping */
|
|
|
|
else
|
2011-03-18 04:16:29 +00:00
|
|
|
guac_sleep(GUAC_SYNC_FREQUENCY);
|
2011-03-16 08:24:17 +00:00
|
|
|
|
|
|
|
} /* End of output loop */
|
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
guac_client_stop(client);
|
2011-03-16 08:24:17 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void* __guac_client_input_thread(void* data) {
|
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
guac_client* client = (guac_client*) data;
|
2011-03-16 08:24:17 +00:00
|
|
|
GUACIO* io = client->io;
|
|
|
|
|
|
|
|
guac_instruction instruction;
|
|
|
|
|
|
|
|
/* Guacamole client input loop */
|
2011-03-17 07:25:35 +00:00
|
|
|
while (client->state == RUNNING && guac_read_instruction(io, &instruction) > 0) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-03-17 07:25:35 +00:00
|
|
|
/* Call handler, stop on error */
|
|
|
|
if (guac_client_handle_instruction(client, &instruction) < 0) {
|
2011-03-17 06:46:02 +00:00
|
|
|
guac_free_instruction_data(&instruction);
|
2011-03-16 08:24:17 +00:00
|
|
|
break;
|
2011-03-17 07:25:35 +00:00
|
|
|
}
|
2011-03-17 06:46:02 +00:00
|
|
|
|
2011-03-17 07:25:35 +00:00
|
|
|
/* Free allocate instruction data */
|
|
|
|
guac_free_instruction_data(&instruction);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
guac_client_stop(client);
|
2011-03-16 08:24:17 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-19 23:44:06 +00:00
|
|
|
int guac_start_client(guac_client* client) {
|
2011-03-16 08:24:17 +00:00
|
|
|
|
|
|
|
pthread_t input_thread, output_thread;
|
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
if (pthread_create(&output_thread, NULL, __guac_client_output_thread, (void*) client)) {
|
2011-03-19 23:44:06 +00:00
|
|
|
return -1;
|
2011-03-16 08:24:17 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
if (pthread_create(&input_thread, NULL, __guac_client_input_thread, (void*) client)) {
|
2011-03-19 23:44:06 +00:00
|
|
|
return -1;
|
2011-03-16 08:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for I/O threads */
|
|
|
|
pthread_join(input_thread, NULL);
|
|
|
|
pthread_join(output_thread, NULL);
|
|
|
|
|
|
|
|
/* Done */
|
2011-03-19 23:44:06 +00:00
|
|
|
return 0;
|
2011-03-16 08:24:17 +00:00
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 06:46:02 +00:00
|
|
|
int guac_client_handle_instruction(guac_client* client, guac_instruction* instruction) {
|
|
|
|
|
|
|
|
/* For each defined instruction */
|
|
|
|
__guac_instruction_handler_mapping* current = __guac_instruction_handler_map;
|
|
|
|
while (current->opcode != NULL) {
|
|
|
|
|
|
|
|
/* If recognized, call handler */
|
|
|
|
if (strcmp(instruction->opcode, current->opcode) == 0)
|
|
|
|
return current->handler(client, instruction);
|
|
|
|
|
|
|
|
current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If unrecognized, ignore */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|