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 <stdio.h>
|
2012-09-05 07:47:21 +00:00
|
|
|
#include <stdlib.h>
|
2010-12-08 21:14:04 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "client.h"
|
2011-03-17 06:46:02 +00:00
|
|
|
#include "client-handlers.h"
|
2011-11-25 21:04:59 +00:00
|
|
|
#include "error.h"
|
2012-09-05 07:47:21 +00:00
|
|
|
#include "layer.h"
|
|
|
|
#include "plugin.h"
|
2012-09-07 00:55:24 +00:00
|
|
|
#include "pool.h"
|
2012-09-05 07:47:21 +00:00
|
|
|
#include "protocol.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "time.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-26 05:01:53 +00:00
|
|
|
guac_layer __GUAC_DEFAULT_LAYER = {
|
|
|
|
.index = 0,
|
2012-08-25 04:37:37 +00:00
|
|
|
.uri = "layer://0",
|
2011-10-26 05:01:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const guac_layer* GUAC_DEFAULT_LAYER = &__GUAC_DEFAULT_LAYER;
|
|
|
|
|
2012-09-07 03:08:15 +00:00
|
|
|
guac_resource* guac_client_alloc_resource(guac_client* client) {
|
|
|
|
|
|
|
|
/* Init new layer */
|
|
|
|
guac_resource* resource = malloc(sizeof(guac_resource));
|
|
|
|
resource->index = guac_pool_next_int(client->__resource_pool);
|
|
|
|
resource->accept_handler = NULL;
|
|
|
|
resource->reject_handler = NULL;
|
|
|
|
resource->data = NULL;
|
|
|
|
|
|
|
|
/* Resize resource map if necessary */
|
|
|
|
if (resource->index >= client->__available_resource_slots) {
|
|
|
|
client->__available_resource_slots = resource->index * 2;
|
|
|
|
client->__resource_map = realloc(client->__resource_map,
|
|
|
|
sizeof(guac_resource*) * client->__available_resource_slots);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store resource in map */
|
|
|
|
client->__resource_map[resource->index] = resource;
|
|
|
|
|
|
|
|
return resource;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-28 06:56:38 +00:00
|
|
|
guac_layer* guac_client_alloc_layer(guac_client* client) {
|
2011-07-20 19:36:02 +00:00
|
|
|
|
2012-09-07 00:55:24 +00:00
|
|
|
/* Init new layer */
|
|
|
|
guac_layer* allocd_layer = malloc(sizeof(guac_layer));
|
|
|
|
allocd_layer->index = guac_pool_next_int(client->__layer_pool)+1;
|
|
|
|
allocd_layer->uri = malloc(64);
|
|
|
|
snprintf(allocd_layer->uri, 64, "layer://%i", allocd_layer->index);
|
2011-07-20 19:36:02 +00:00
|
|
|
|
|
|
|
return allocd_layer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_layer* guac_client_alloc_buffer(guac_client* client) {
|
|
|
|
|
2012-09-07 00:55:24 +00:00
|
|
|
/* Init new layer */
|
|
|
|
guac_layer* allocd_layer = malloc(sizeof(guac_layer));
|
|
|
|
allocd_layer->index = -guac_pool_next_int(client->__buffer_pool) - 1;
|
|
|
|
allocd_layer->uri = malloc(64);
|
|
|
|
snprintf(allocd_layer->uri, 64, "layer://%i", allocd_layer->index);
|
2011-07-20 19:36:02 +00:00
|
|
|
|
|
|
|
return allocd_layer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-07 03:08:15 +00:00
|
|
|
void guac_client_free_resource(guac_client* client, guac_resource* resource) {
|
|
|
|
|
|
|
|
/* Release index to pool */
|
|
|
|
guac_pool_free_int(client->__resource_pool, resource->index);
|
|
|
|
|
|
|
|
/* Free resource */
|
|
|
|
free(resource);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-20 19:36:02 +00:00
|
|
|
void guac_client_free_buffer(guac_client* client, guac_layer* layer) {
|
2011-10-23 22:35:23 +00:00
|
|
|
|
2012-09-07 00:55:24 +00:00
|
|
|
/* Release index to pool */
|
|
|
|
guac_pool_free_int(client->__buffer_pool, -layer->index - 1);
|
2012-04-10 18:08:52 +00:00
|
|
|
|
2012-09-07 00:55:24 +00:00
|
|
|
/* Free layer */
|
|
|
|
free(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
|
|
|
|
2012-02-28 06:56:38 +00:00
|
|
|
void guac_client_free_layer(guac_client* client, guac_layer* layer) {
|
|
|
|
|
2012-09-07 00:55:24 +00:00
|
|
|
/* Release index to pool */
|
|
|
|
guac_pool_free_int(client->__layer_pool, layer->index - 1);
|
2012-02-28 06:56:38 +00:00
|
|
|
|
2012-09-07 00:55:24 +00:00
|
|
|
/* Free layer */
|
|
|
|
free(layer);
|
2012-02-28 06:56:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:28:56 +00:00
|
|
|
guac_client* guac_client_alloc() {
|
2011-11-25 21:04:59 +00:00
|
|
|
|
|
|
|
/* Allocate new client */
|
|
|
|
guac_client* client = malloc(sizeof(guac_client));
|
|
|
|
if (client == NULL) {
|
|
|
|
guac_error = GUAC_STATUS_NO_MEMORY;
|
2011-11-27 23:57:43 +00:00
|
|
|
guac_error_message = "Could not allocate memory for client";
|
2011-11-25 21:04:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-01 21:22:17 +00:00
|
|
|
|
2011-11-25 21:04:59 +00:00
|
|
|
/* Init new client */
|
|
|
|
memset(client, 0, sizeof(guac_client));
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-11-25 21:04:59 +00:00
|
|
|
client->last_received_timestamp =
|
2012-09-05 07:47:21 +00:00
|
|
|
client->last_sent_timestamp = guac_timestamp_current();
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-12-25 06:49:27 +00:00
|
|
|
client->state = GUAC_CLIENT_RUNNING;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2012-09-07 02:57:19 +00:00
|
|
|
/* Allocate buffer and layer pools */
|
2012-09-07 00:55:24 +00:00
|
|
|
client->__buffer_pool = guac_pool_alloc(GUAC_BUFFER_POOL_INITIAL_SIZE);
|
|
|
|
client->__layer_pool = guac_pool_alloc(GUAC_BUFFER_POOL_INITIAL_SIZE);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2012-09-07 02:57:19 +00:00
|
|
|
/* Allocate resource pool */
|
|
|
|
client->__resource_pool = guac_pool_alloc(0);
|
|
|
|
client->__available_resource_slots = GUAC_RESOURCE_MAP_INITIAL_SIZE;
|
|
|
|
client->__resource_map =
|
|
|
|
malloc(sizeof(guac_resource*) * client->__available_resource_slots);
|
|
|
|
|
2011-11-25 21:04:59 +00:00
|
|
|
return client;
|
|
|
|
|
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) {
|
|
|
|
|
2011-11-25 21:04:59 +00:00
|
|
|
/* FIXME: Errors currently ignored... */
|
|
|
|
client->free_handler(client);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-07 03:23:03 +00:00
|
|
|
/* Free layer pools */
|
|
|
|
guac_pool_free(client->__buffer_pool);
|
|
|
|
guac_pool_free(client->__layer_pool);
|
|
|
|
|
|
|
|
/* Free resource pool */
|
|
|
|
guac_pool_free(client->__resource_pool);
|
|
|
|
free(client->__resource_map);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-25 20:22:12 +00:00
|
|
|
void vguac_client_log_info(guac_client* client, const char* format,
|
|
|
|
va_list ap) {
|
|
|
|
|
|
|
|
/* Call handler if defined */
|
|
|
|
if (client->log_info_handler != NULL)
|
|
|
|
client->log_info_handler(client, format, ap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void vguac_client_log_error(guac_client* client, const char* format,
|
|
|
|
va_list ap) {
|
|
|
|
|
|
|
|
/* Call handler if defined */
|
|
|
|
if (client->log_error_handler != NULL)
|
|
|
|
client->log_error_handler(client, format, ap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_client_log_info(guac_client* client, const char* format, ...) {
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
vguac_client_log_info(client, format, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_client_log_error(guac_client* client, const char* format, ...) {
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
vguac_client_log_error(client, format, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-02 06:57:34 +00:00
|
|
|
void guac_client_stop(guac_client* client) {
|
2011-12-25 06:49:27 +00:00
|
|
|
client->state = GUAC_CLIENT_STOPPING;
|
2011-12-02 06:57:34 +00:00
|
|
|
}
|
|
|
|
|