2013-12-29 04:53:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
2010-12-08 21:14:04 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2011-02-16 02:04:36 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2011-02-16 02:04:36 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
#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"
|
2014-06-10 23:54:08 +00:00
|
|
|
#include "instruction.h"
|
2012-09-05 07:47:21 +00:00
|
|
|
#include "layer.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"
|
2014-04-10 02:09:41 +00:00
|
|
|
#include "stream.h"
|
2014-04-09 22:43:09 +00:00
|
|
|
#include "timestamp.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2014-07-02 20:33:47 +00:00
|
|
|
#ifdef HAVE_OSSP_UUID_H
|
2014-06-26 22:09:44 +00:00
|
|
|
#include <ossp/uuid.h>
|
2014-07-02 20:33:47 +00:00
|
|
|
#else
|
|
|
|
#include <uuid.h>
|
|
|
|
#endif
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2014-06-10 23:54:08 +00:00
|
|
|
#include <stdarg.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-10-26 05:01:53 +00:00
|
|
|
guac_layer __GUAC_DEFAULT_LAYER = {
|
2012-10-18 08:34:25 +00:00
|
|
|
.index = 0
|
2011-10-26 05:01:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const guac_layer* GUAC_DEFAULT_LAYER = &__GUAC_DEFAULT_LAYER;
|
|
|
|
|
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;
|
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;
|
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
|
|
|
|
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 */
|
2012-10-19 03:57:11 +00:00
|
|
|
guac_pool_free_int(client->__layer_pool, layer->index);
|
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-10-19 03:57:11 +00:00
|
|
|
guac_stream* guac_client_alloc_stream(guac_client* client) {
|
|
|
|
|
2013-10-28 16:11:45 +00:00
|
|
|
guac_stream* allocd_stream;
|
|
|
|
int stream_index;
|
|
|
|
|
|
|
|
/* Refuse to allocate beyond maximum */
|
|
|
|
if (client->__stream_pool->active == GUAC_CLIENT_MAX_STREAMS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Allocate stream */
|
|
|
|
stream_index = guac_pool_next_int(client->__stream_pool);
|
|
|
|
|
|
|
|
/* Initialize stream */
|
|
|
|
allocd_stream = &(client->__output_streams[stream_index]);
|
|
|
|
allocd_stream->index = stream_index;
|
|
|
|
allocd_stream->data = NULL;
|
2014-07-08 17:18:13 +00:00
|
|
|
allocd_stream->ack_handler = NULL;
|
|
|
|
allocd_stream->blob_handler = NULL;
|
|
|
|
allocd_stream->end_handler = NULL;
|
2012-10-19 03:57:11 +00:00
|
|
|
|
|
|
|
return allocd_stream;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_client_free_stream(guac_client* client, guac_stream* stream) {
|
|
|
|
|
|
|
|
/* Release index to pool */
|
2013-10-28 16:11:45 +00:00
|
|
|
guac_pool_free_int(client->__stream_pool, stream->index);
|
|
|
|
|
|
|
|
/* Mark stream as closed */
|
|
|
|
stream->index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
|
2012-10-19 03:57:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-26 22:09:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a newly allocated string containing a guaranteed-unique connection
|
|
|
|
* identifier string which is 37 characters long and begins with a '$'
|
|
|
|
* character. If an error occurs, NULL is returned, and no memory is
|
|
|
|
* allocated.
|
|
|
|
*/
|
|
|
|
static char* __guac_generate_connection_id() {
|
|
|
|
|
|
|
|
char* buffer;
|
|
|
|
char* identifier;
|
|
|
|
size_t identifier_length;
|
|
|
|
|
|
|
|
uuid_t* uuid;
|
|
|
|
|
|
|
|
/* Attempt to create UUID object */
|
|
|
|
if (uuid_create(&uuid) != UUID_RC_OK) {
|
|
|
|
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
|
|
guac_error_message = "Could not allocate memory for UUID";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate random UUID */
|
|
|
|
if (uuid_make(uuid, UUID_MAKE_V4) != UUID_RC_OK) {
|
|
|
|
uuid_destroy(uuid);
|
|
|
|
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
|
|
guac_error_message = "UUID generation failed";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate buffer for future formatted ID */
|
|
|
|
buffer = malloc(UUID_LEN_STR + 2);
|
|
|
|
if (buffer == NULL) {
|
|
|
|
uuid_destroy(uuid);
|
|
|
|
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
|
|
guac_error_message = "Could not allocate memory for connection ID";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
identifier = &(buffer[1]);
|
|
|
|
identifier_length = UUID_LEN_STR + 1;
|
|
|
|
|
|
|
|
/* Build connection ID from UUID */
|
|
|
|
if (uuid_export(uuid, UUID_FMT_STR, &identifier, &identifier_length) != UUID_RC_OK) {
|
|
|
|
free(buffer);
|
|
|
|
uuid_destroy(uuid);
|
|
|
|
guac_error = GUAC_STATUS_BAD_STATE;
|
|
|
|
guac_error_message = "Conversion of UUID to connection ID failed";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid_destroy(uuid);
|
|
|
|
|
|
|
|
buffer[0] = '$';
|
|
|
|
buffer[UUID_LEN_STR + 1] = '\0';
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:28:56 +00:00
|
|
|
guac_client* guac_client_alloc() {
|
2011-11-25 21:04:59 +00:00
|
|
|
|
2013-09-28 03:02:06 +00:00
|
|
|
int i;
|
|
|
|
|
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
|
|
|
|
2014-06-26 22:09:44 +00:00
|
|
|
/* Generate ID */
|
|
|
|
client->connection_id = __guac_generate_connection_id();
|
|
|
|
if (client->connection_id == NULL) {
|
|
|
|
free(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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-10-19 03:57:11 +00:00
|
|
|
/* Allocate stream pool */
|
|
|
|
client->__stream_pool = guac_pool_alloc(0);
|
|
|
|
|
2013-09-28 03:02:06 +00:00
|
|
|
/* Initialze streams */
|
2014-04-09 22:43:09 +00:00
|
|
|
client->__input_streams = malloc(sizeof(guac_stream) * GUAC_CLIENT_MAX_STREAMS);
|
|
|
|
client->__output_streams = malloc(sizeof(guac_stream) * GUAC_CLIENT_MAX_STREAMS);
|
|
|
|
|
2013-10-28 16:11:45 +00:00
|
|
|
for (i=0; i<GUAC_CLIENT_MAX_STREAMS; i++) {
|
|
|
|
client->__input_streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
|
|
|
|
client->__output_streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
|
|
|
|
}
|
2013-09-28 03:02:06 +00:00
|
|
|
|
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);
|
|
|
|
|
2014-04-09 22:43:09 +00:00
|
|
|
/* Free streams */
|
|
|
|
free(client->__input_streams);
|
|
|
|
free(client->__output_streams);
|
|
|
|
|
2012-11-30 03:35:17 +00:00
|
|
|
/* Free stream pool */
|
|
|
|
guac_pool_free(client->__stream_pool);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
void vguac_client_log(guac_client* client, guac_client_log_level level,
|
|
|
|
const char* format, va_list ap) {
|
2011-11-25 20:22:12 +00:00
|
|
|
|
|
|
|
/* Call handler if defined */
|
2014-11-08 00:32:19 +00:00
|
|
|
if (client->log_handler != NULL)
|
|
|
|
client->log_handler(client, level, format, ap);
|
2011-11-25 20:22:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
void guac_client_log(guac_client* client, guac_client_log_level level,
|
|
|
|
const char* format, ...) {
|
2011-11-25 20:22:12 +00:00
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
vguac_client_log(client, level, format, args);
|
2011-11-25 20:22:12 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-20 21:00:26 +00:00
|
|
|
void vguac_client_abort(guac_client* client, guac_protocol_status status,
|
|
|
|
const char* format, va_list ap) {
|
|
|
|
|
|
|
|
/* Only relevant if client is running */
|
|
|
|
if (client->state == GUAC_CLIENT_RUNNING) {
|
|
|
|
|
|
|
|
/* Log detail of error */
|
2014-11-08 00:32:19 +00:00
|
|
|
vguac_client_log(client, GUAC_LOG_ERROR, format, ap);
|
2014-03-20 21:00:26 +00:00
|
|
|
|
|
|
|
/* Send error immediately, limit information given */
|
|
|
|
guac_protocol_send_error(client->socket, "Aborted. See logs.", status);
|
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
|
|
|
|
/* Stop client */
|
|
|
|
guac_client_stop(client);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_client_abort(guac_client* client, guac_protocol_status status,
|
|
|
|
const char* format, ...) {
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
vguac_client_abort(client, status, format, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
}
|
|
|
|
|