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-11-25 20:17:20 +00:00
|
|
|
#include "socket.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
#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
|
|
|
|
2011-10-26 05:01:53 +00:00
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
|
|
|
|
guac_layer __GUAC_DEFAULT_LAYER = {
|
|
|
|
.index = 0,
|
2011-11-24 00:08:33 +00:00
|
|
|
.__next = NULL,
|
|
|
|
.__next_available = NULL
|
2011-10-26 05:01:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const guac_layer* GUAC_DEFAULT_LAYER = &__GUAC_DEFAULT_LAYER;
|
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_client* __guac_alloc_client(guac_socket* io) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* 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-11-25 20:17:20 +00:00
|
|
|
client->last_received_timestamp = client->last_sent_timestamp = guac_protocol_get_timestamp();
|
2011-03-17 06:46:02 +00:00
|
|
|
client->state = RUNNING;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-11-24 00:08:33 +00:00
|
|
|
client->__all_layers = NULL;
|
|
|
|
client->__available_buffers = NULL;
|
2011-07-20 19:36:02 +00:00
|
|
|
|
2011-11-24 00:08:33 +00:00
|
|
|
client->__next_buffer_index = -1;
|
2011-10-24 06:11:29 +00:00
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2011-07-20 19:36:02 +00:00
|
|
|
guac_layer* guac_client_alloc_layer(guac_client* client, int index) {
|
|
|
|
|
|
|
|
guac_layer* allocd_layer;
|
|
|
|
|
2011-10-23 22:35:23 +00:00
|
|
|
/* Init new layer */
|
|
|
|
allocd_layer = malloc(sizeof(guac_layer));
|
2011-07-20 19:36:02 +00:00
|
|
|
|
2011-11-24 00:08:33 +00:00
|
|
|
/* Add to __all_layers list */
|
|
|
|
allocd_layer->__next = client->__all_layers;
|
|
|
|
client->__all_layers = allocd_layer;
|
2011-07-20 19:36:02 +00:00
|
|
|
|
|
|
|
allocd_layer->index = index;
|
|
|
|
return allocd_layer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_layer* guac_client_alloc_buffer(guac_client* client) {
|
|
|
|
|
|
|
|
guac_layer* allocd_layer;
|
|
|
|
|
|
|
|
/* If available layers, pop off first available buffer */
|
2011-11-24 00:08:33 +00:00
|
|
|
if (client->__available_buffers != NULL) {
|
|
|
|
allocd_layer = client->__available_buffers;
|
|
|
|
client->__available_buffers = client->__available_buffers->__next_available;
|
|
|
|
allocd_layer->__next_available = NULL;
|
2011-07-20 19:36:02 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 00:08:33 +00:00
|
|
|
/* If no available buffer, allocate new buffer, add to __all_layers list */
|
2011-07-20 19:36:02 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
/* Init new layer */
|
|
|
|
allocd_layer = malloc(sizeof(guac_layer));
|
2011-11-24 00:08:33 +00:00
|
|
|
allocd_layer->index = client->__next_buffer_index--;
|
2011-07-20 19:36:02 +00:00
|
|
|
|
2011-11-24 00:08:33 +00:00
|
|
|
/* Add to __all_layers list */
|
|
|
|
allocd_layer->__next = client->__all_layers;
|
|
|
|
client->__all_layers = allocd_layer;
|
2011-07-20 19:36:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocd_layer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_client_free_buffer(guac_client* client, guac_layer* layer) {
|
2011-10-23 22:35:23 +00:00
|
|
|
|
|
|
|
/* Add layer to pool of available buffers */
|
2011-11-24 00:08:33 +00:00
|
|
|
layer->__next_available = client->__available_buffers;
|
|
|
|
client->__available_buffers = layer;
|
2011-10-23 22:35:23 +00:00
|
|
|
|
2011-07-20 19:36:02 +00:00
|
|
|
}
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-11-23 08:43:30 +00:00
|
|
|
guac_client* guac_get_client(int client_fd, int usec_timeout) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
guac_client* client;
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_socket* io = guac_socket_open(client_fd);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* 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 */
|
2011-11-25 02:18:03 +00:00
|
|
|
guac_instruction* instruction;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
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-11-25 20:17:20 +00:00
|
|
|
result = guac_protocol_instructions_waiting(io, usec_timeout);
|
2011-11-21 07:09:02 +00:00
|
|
|
if (result == 0) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_protocol_send_error(io, "Select timeout.");
|
|
|
|
guac_socket_close(io);
|
2011-03-16 06:02:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If error occurs while waiting, exit with failure */
|
2011-11-21 07:09:02 +00:00
|
|
|
if (result < 0) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_socket_close(io);
|
2011-03-16 06:02:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
instruction = guac_protocol_read_instruction(io, usec_timeout);
|
2011-11-25 02:18:03 +00:00
|
|
|
if (instruction == NULL) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_socket_close(io);
|
2011-11-21 07:09:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-01-01 21:22:17 +00:00
|
|
|
/* Select instruction read */
|
2011-11-25 02:18:03 +00:00
|
|
|
else {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-11-25 02:18:03 +00:00
|
|
|
if (strcmp(instruction->opcode, "select") == 0) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* Get protocol from message */
|
2011-11-25 02:18:03 +00:00
|
|
|
char* protocol = instruction->argv[0];
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
strcat(protocol_lib, protocol);
|
|
|
|
strcat(protocol_lib, ".so");
|
|
|
|
|
|
|
|
/* Create new client */
|
|
|
|
client = __guac_alloc_client(io);
|
|
|
|
|
|
|
|
/* Load client plugin */
|
2011-11-24 00:08:33 +00:00
|
|
|
client->__client_plugin_handle = dlopen(protocol_lib, RTLD_LAZY);
|
|
|
|
if (!(client->__client_plugin_handle)) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_client_log_error(client, "Could not open client plugin for protocol \"%s\": %s\n", protocol, dlerror());
|
|
|
|
guac_protocol_send_error(io, "Could not load server-side client plugin.");
|
|
|
|
guac_socket_flush(io);
|
|
|
|
guac_socket_close(io);
|
|
|
|
guac_instruction_free(instruction);
|
2010-12-08 21:14:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlerror(); /* Clear errors */
|
|
|
|
|
|
|
|
/* Get init function */
|
2011-11-24 00:08:33 +00:00
|
|
|
alias.obj = dlsym(client->__client_plugin_handle, "guac_client_init");
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
if ((error = dlerror()) != NULL) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_client_log_error(client, "Could not get guac_client_init in plugin: %s\n", error);
|
|
|
|
guac_protocol_send_error(io, "Invalid server-side client plugin.");
|
|
|
|
guac_socket_flush(io);
|
|
|
|
guac_socket_close(io);
|
|
|
|
guac_instruction_free(instruction);
|
2010-12-08 21:14:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-01 21:22:17 +00:00
|
|
|
/* Get usage strig */
|
2011-11-24 00:08:33 +00:00
|
|
|
client_args = (const char**) dlsym(client->__client_plugin_handle, "GUAC_CLIENT_ARGS");
|
2011-01-01 21:22:17 +00:00
|
|
|
|
|
|
|
if ((error = dlerror()) != NULL) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_client_log_error(client, "Could not get GUAC_CLIENT_ARGS in plugin: %s\n", error);
|
|
|
|
guac_protocol_send_error(io, "Invalid server-side client plugin.");
|
|
|
|
guac_socket_flush(io);
|
|
|
|
guac_socket_close(io);
|
|
|
|
guac_instruction_free(instruction);
|
2011-01-01 21:22:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-19 23:39:18 +00:00
|
|
|
if ( /* Send args */
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_protocol_send_args(io, client_args)
|
|
|
|
|| guac_socket_flush(io)
|
2011-03-19 23:39:18 +00:00
|
|
|
) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_socket_close(io);
|
|
|
|
guac_instruction_free(instruction);
|
2011-03-19 23:39:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-01 21:22:17 +00:00
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_instruction_free(instruction);
|
2011-01-01 21:22:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
} /* end if select */
|
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_instruction_free(instruction);
|
2011-01-01 21:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for connect instruction */
|
|
|
|
for (;;) {
|
|
|
|
|
2011-03-16 06:02:47 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
/* Wait for data until timeout */
|
2011-11-25 20:17:20 +00:00
|
|
|
result = guac_protocol_instructions_waiting(io, usec_timeout);
|
2011-03-16 06:02:47 +00:00
|
|
|
if (result == 0) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_protocol_send_error(io, "Connect timeout.");
|
|
|
|
guac_socket_close(io);
|
2011-03-16 06:02:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If error occurs while waiting, exit with failure */
|
|
|
|
if (result < 0) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_socket_close(io);
|
2011-03-16 06:02:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
instruction = guac_protocol_read_instruction(io, usec_timeout);
|
2011-11-25 02:18:03 +00:00
|
|
|
if (instruction == NULL) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_client_log_error(client, "Error reading instruction while waiting for connect");
|
|
|
|
guac_socket_close(io);
|
2011-01-01 21:22:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect instruction read */
|
2011-11-25 02:18:03 +00:00
|
|
|
else {
|
2011-01-01 21:22:17 +00:00
|
|
|
|
2011-11-25 02:18:03 +00:00
|
|
|
if (strcmp(instruction->opcode, "connect") == 0) {
|
2011-01-01 21:22:17 +00:00
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
/* Initialize client arguments */
|
2011-11-25 02:18:03 +00:00
|
|
|
argc = instruction->argc;
|
|
|
|
argv = instruction->argv;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
if (alias.client_init(client, argc, argv) != 0) {
|
|
|
|
/* NOTE: On error, proxy client will send appropriate error message */
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_instruction_free(instruction);
|
|
|
|
guac_socket_close(io);
|
2010-12-08 21:14:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_instruction_free(instruction);
|
2010-12-08 21:14:04 +00:00
|
|
|
return client;
|
|
|
|
|
|
|
|
} /* end if connect */
|
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_instruction_free(instruction);
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
void guac_client_free(guac_client* client) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
if (client->free_handler) {
|
|
|
|
if (client->free_handler(client))
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_client_log_error(client, "Error calling client free handler");
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_socket_close(client->io);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* Unload client plugin */
|
2011-11-24 00:08:33 +00:00
|
|
|
if (dlclose(client->__client_plugin_handle)) {
|
2011-11-25 20:17:20 +00:00
|
|
|
guac_client_log_error(client, "Could not close client plugin while unloading client: %s", dlerror());
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-07-20 19:36:02 +00:00
|
|
|
/* Free all layers */
|
2011-11-24 00:08:33 +00:00
|
|
|
while (client->__all_layers != NULL) {
|
2011-10-23 22:35:23 +00:00
|
|
|
|
|
|
|
/* Get layer, update layer pool head */
|
2011-11-24 00:08:33 +00:00
|
|
|
guac_layer* layer = client->__all_layers;
|
|
|
|
client->__all_layers = layer->__next;
|
2011-10-23 22:35:23 +00:00
|
|
|
|
2011-10-26 05:01:53 +00:00
|
|
|
/* Free layer */
|
|
|
|
free(layer);
|
2011-10-23 22:35:23 +00:00
|
|
|
|
2011-10-26 05:01:53 +00:00
|
|
|
}
|
2011-10-23 22:35:23 +00:00
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
free(client);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|