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"
|
2015-08-12 03:43:46 +00:00
|
|
|
#include "encode-jpeg.h"
|
2015-08-11 23:26:24 +00:00
|
|
|
#include "encode-png.h"
|
2016-03-01 05:35:32 +00:00
|
|
|
#include "encode-webp.h"
|
2011-11-25 21:04:59 +00:00
|
|
|
#include "error.h"
|
2016-03-01 05:35:32 +00:00
|
|
|
#include "id.h"
|
2012-09-05 07:47:21 +00:00
|
|
|
#include "layer.h"
|
2012-09-07 00:55:24 +00:00
|
|
|
#include "pool.h"
|
2016-03-01 05:35:32 +00:00
|
|
|
#include "plugin.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"
|
2016-03-01 05:35:32 +00:00
|
|
|
#include "user.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <pthread.h>
|
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;
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/**
|
|
|
|
* Single chunk of data, to be broadcast to all users.
|
|
|
|
*/
|
|
|
|
typedef struct __write_chunk {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The buffer to write.
|
|
|
|
*/
|
|
|
|
const void* buffer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of bytes in the buffer.
|
|
|
|
*/
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
} __write_chunk;
|
|
|
|
|
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);
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Initialize stream with odd index (even indices are user-level) */
|
2013-10-28 16:11:45 +00:00
|
|
|
allocd_stream = &(client->__output_streams[stream_index]);
|
2016-03-01 05:35:32 +00:00
|
|
|
allocd_stream->index = (stream_index * 2) + 1;
|
2013-10-28 16:11:45 +00:00
|
|
|
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 */
|
2016-03-01 05:35:32 +00:00
|
|
|
guac_pool_free_int(client->__stream_pool, (stream->index - 1) / 2);
|
2013-10-28 16:11:45 +00:00
|
|
|
|
|
|
|
/* Mark stream as closed */
|
|
|
|
stream->index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
|
2012-10-19 03:57:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/**
|
2016-03-01 19:04:55 +00:00
|
|
|
* Callback which handles read requests on the broadcast socket. This callback
|
|
|
|
* always fails, as the broadcast socket is write-only; it cannot be read.
|
|
|
|
*
|
|
|
|
* @param socket
|
|
|
|
* The broadcast socket to read from.
|
|
|
|
*
|
|
|
|
* @param buf
|
|
|
|
* The buffer into which data should be read.
|
|
|
|
*
|
|
|
|
* @param count
|
|
|
|
* The number of bytes to attempt to read.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The number of bytes read, or -1 if an error occurs. This implementation
|
|
|
|
* always returns -1, as the broadcast socket is write-only and cannot be
|
|
|
|
* read.
|
2016-03-01 05:35:32 +00:00
|
|
|
*/
|
|
|
|
static ssize_t __guac_socket_broadcast_read_handler(guac_socket* socket,
|
|
|
|
void* buf, size_t count) {
|
2015-06-19 21:41:25 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Broadcast socket reads are not allowed */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:04:55 +00:00
|
|
|
* Callback invoked by guac_client_foreach_user() which write a given chunk of
|
|
|
|
* data to that user's socket. If the write attempt fails, the user is
|
|
|
|
* signalled to stop with guac_user_stop().
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The user that the chunk of data should be written to.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* A pointer to a __write_chunk which describes the data to be written.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Always NULL.
|
2016-03-01 05:35:32 +00:00
|
|
|
*/
|
|
|
|
static void* __write_chunk_callback(guac_user* user, void* data) {
|
2015-06-19 21:41:25 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
__write_chunk* chunk = (__write_chunk*) data;
|
2015-06-19 21:41:25 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Attempt write, disconnect on failure */
|
|
|
|
if (guac_socket_write(user->socket, chunk->buffer, chunk->length))
|
|
|
|
guac_user_stop(user);
|
2015-06-19 21:41:25 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
return NULL;
|
2015-06-19 21:41:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/**
|
|
|
|
* Socket write handler which operates on each of the sockets of all connected
|
2016-03-01 19:04:55 +00:00
|
|
|
* users. This write handler will always succeed, but any failing user-specific
|
|
|
|
* writes will invoke guac_user_stop() on the failing user.
|
|
|
|
*
|
|
|
|
* @param socket
|
|
|
|
* The socket to which the given data must be written.
|
|
|
|
*
|
|
|
|
* @param buf
|
|
|
|
* The buffer containing the data to write.
|
|
|
|
*
|
|
|
|
* @param count
|
|
|
|
* The number of bytes to attempt to write from the given buffer.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The number of bytes written, or -1 if an error occurs. This handler will
|
|
|
|
* always succeed, and thus will always return the exact number of bytes
|
|
|
|
* specified by count.
|
2016-03-01 05:35:32 +00:00
|
|
|
*/
|
|
|
|
static ssize_t __guac_socket_broadcast_write_handler(guac_socket* socket,
|
|
|
|
const void* buf, size_t count) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) socket->data;
|
2015-06-19 21:41:25 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Build chunk */
|
|
|
|
__write_chunk chunk;
|
|
|
|
chunk.buffer = buf;
|
|
|
|
chunk.length = count;
|
|
|
|
|
|
|
|
/* Broadcast chunk to all users */
|
|
|
|
guac_client_foreach_user(client, __write_chunk_callback, &chunk);
|
2015-06-19 21:41:25 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
return count;
|
2015-06-19 21:41:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/**
|
|
|
|
* Callback which is invoked by guac_client_foreach_user() to flush all
|
|
|
|
* pending data on the given user's socket. If an error occurs while flushing
|
|
|
|
* a user's socket, that user is signalled to stop with guac_user_stop().
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The user whose socket should be flushed.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* Arbitrary data passed to guac_client_foreach_user(). This is not needed
|
|
|
|
* by this callback, and should be left as NULL.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Always NULL.
|
|
|
|
*/
|
2016-03-01 05:35:32 +00:00
|
|
|
static void* __flush_callback(guac_user* user, void* data) {
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Attempt flush, disconnect on failure */
|
|
|
|
if (guac_socket_flush(user->socket))
|
|
|
|
guac_user_stop(user);
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
return NULL;
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
}
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/**
|
|
|
|
* Socket flush handler which operates on each of the sockets of all connected
|
|
|
|
* users. This flush handler will always succeed, but any failing user-specific
|
|
|
|
* flush will invoke guac_user_stop() on the failing user.
|
|
|
|
*
|
|
|
|
* @param socket
|
|
|
|
* The broadcast socket to flush.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Zero if the flush operation succeeds, non-zero if the operation fails.
|
|
|
|
* This handler will always succeed, and thus will always return zero.
|
|
|
|
*/
|
2016-03-01 05:35:32 +00:00
|
|
|
static ssize_t __guac_socket_broadcast_flush_handler(guac_socket* socket) {
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
guac_client* client = (guac_client*) socket->data;
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Flush all users */
|
|
|
|
guac_client_foreach_user(client, __flush_callback, NULL);
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
return 0;
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/**
|
|
|
|
* Callback which is invoked by guac_client_foreach_user() to lock the given
|
|
|
|
* user's socket in preparation for the beginning of a Guacamole protocol
|
|
|
|
* instruction.
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The user whose socket should be locked.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* Arbitrary data passed to guac_client_foreach_user(). This is not needed
|
|
|
|
* by this callback, and should be left as NULL.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Always NULL.
|
|
|
|
*/
|
2016-03-01 05:35:32 +00:00
|
|
|
static void* __lock_callback(guac_user* user, void* data) {
|
|
|
|
|
|
|
|
/* Lock socket */
|
|
|
|
guac_socket_instruction_begin(user->socket);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/**
|
|
|
|
* Socket lock handler which acquires the socket locks of all connected users.
|
|
|
|
* Socket-level locks are acquired in preparation for the beginning of a new
|
|
|
|
* Guacamole instruction to ensure that parallel writes are only interleaved at
|
|
|
|
* instruction boundaries.
|
|
|
|
*
|
|
|
|
* @param socket
|
|
|
|
* The broadcast socket to lock.
|
|
|
|
*/
|
2016-03-01 05:35:32 +00:00
|
|
|
static void __guac_socket_broadcast_lock_handler(guac_socket* socket) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) socket->data;
|
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/* Lock sockets of all users */
|
2016-03-01 05:35:32 +00:00
|
|
|
guac_client_foreach_user(client, __lock_callback, NULL);
|
|
|
|
|
|
|
|
}
|
2014-06-26 22:09:44 +00:00
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/**
|
|
|
|
* Callback which is invoked by guac_client_foreach_user() to unlock the given
|
|
|
|
* user's socket at the end of a Guacamole protocol instruction.
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The user whose socket should be unlocked.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* Arbitrary data passed to guac_client_foreach_user(). This is not needed
|
|
|
|
* by this callback, and should be left as NULL.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Always NULL.
|
|
|
|
*/
|
2016-03-01 05:35:32 +00:00
|
|
|
static void* __unlock_callback(guac_user* user, void* data) {
|
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/* Unlock socket */
|
2016-03-01 05:35:32 +00:00
|
|
|
guac_socket_instruction_end(user->socket);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/**
|
|
|
|
* Socket unlock handler which releases the socket locks of all connected users.
|
|
|
|
* Socket-level locks are released after a Guacamole instruction has finished
|
|
|
|
* being written.
|
|
|
|
*
|
|
|
|
* @param socket
|
|
|
|
* The broadcast socket to unlock.
|
|
|
|
*/
|
2016-03-01 05:35:32 +00:00
|
|
|
static void __guac_socket_broadcast_unlock_handler(guac_socket* socket) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) socket->data;
|
|
|
|
|
2016-03-01 19:04:55 +00:00
|
|
|
/* Unlock sockets of all users */
|
2016-03-01 05:35:32 +00:00
|
|
|
guac_client_foreach_user(client, __unlock_callback, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:04:55 +00:00
|
|
|
* Callback which handles select operations on the broadcast socket, waiting
|
|
|
|
* for data to become available such that the next read operation will not
|
|
|
|
* block. This callback always fails, as the broadcast socket is write-only; it
|
|
|
|
* cannot be read.
|
|
|
|
*
|
|
|
|
* @param socket
|
|
|
|
* The broadcast socket to wait for.
|
|
|
|
*
|
|
|
|
* @param usec_timeout
|
|
|
|
* The maximum amount of time to wait for data, in microseconds, or -1 to
|
|
|
|
* potentially wait forever.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* A positive value on success, zero if the timeout elapsed and no data is
|
|
|
|
* available, or a negative value if an error occurs. This implementation
|
|
|
|
* always returns -1, as the broadcast socket is write-only and cannot be
|
|
|
|
* read.
|
2016-03-01 05:35:32 +00:00
|
|
|
*/
|
2016-03-01 19:04:55 +00:00
|
|
|
static int __guac_socket_broadcast_select_handler(guac_socket* socket,
|
|
|
|
int usec_timeout) {
|
2016-03-01 05:35:32 +00:00
|
|
|
|
|
|
|
/* Selecting the broadcast socket is not possible */
|
|
|
|
return -1;
|
2014-06-26 22:09:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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;
|
2016-03-01 05:35:32 +00:00
|
|
|
pthread_rwlockattr_t lock_attributes;
|
2013-09-28 03:02:06 +00:00
|
|
|
|
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-12-25 06:49:27 +00:00
|
|
|
client->state = GUAC_CLIENT_RUNNING;
|
2016-03-01 05:35:32 +00:00
|
|
|
client->last_sent_timestamp = guac_timestamp_current();
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2014-06-26 22:09:44 +00:00
|
|
|
/* Generate ID */
|
2016-03-01 05:35:32 +00:00
|
|
|
client->connection_id = guac_generate_id(GUAC_CLIENT_ID_PREFIX);
|
2014-06-26 22:09:44 +00:00
|
|
|
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);
|
|
|
|
|
2015-06-19 21:12:27 +00:00
|
|
|
/* Initialize streams */
|
2014-04-09 22:43:09 +00:00
|
|
|
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->__output_streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
|
|
|
|
}
|
2013-09-28 03:02:06 +00:00
|
|
|
|
2015-06-19 21:41:25 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Init locks */
|
|
|
|
pthread_rwlockattr_init(&lock_attributes);
|
|
|
|
pthread_rwlockattr_setpshared(&lock_attributes, PTHREAD_PROCESS_SHARED);
|
|
|
|
|
|
|
|
pthread_rwlock_init(&(client->__users_lock), &lock_attributes);
|
|
|
|
|
|
|
|
/* Set up socket to broadcast to all users */
|
|
|
|
guac_socket* socket = guac_socket_alloc();
|
|
|
|
client->socket = socket;
|
|
|
|
socket->data = client;
|
|
|
|
|
|
|
|
socket->read_handler = __guac_socket_broadcast_read_handler;
|
|
|
|
socket->write_handler = __guac_socket_broadcast_write_handler;
|
|
|
|
socket->select_handler = __guac_socket_broadcast_select_handler;
|
|
|
|
socket->flush_handler = __guac_socket_broadcast_flush_handler;
|
|
|
|
socket->lock_handler = __guac_socket_broadcast_lock_handler;
|
|
|
|
socket->unlock_handler = __guac_socket_broadcast_unlock_handler;
|
2015-06-19 21:12:27 +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
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Remove all users */
|
|
|
|
while (client->__users != NULL)
|
|
|
|
guac_client_remove_user(client, client->__users);
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-29 04:23:52 +00:00
|
|
|
/* Free socket */
|
|
|
|
guac_socket_free(client->socket);
|
|
|
|
|
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->__output_streams);
|
|
|
|
|
2015-06-19 21:41:25 +00:00
|
|
|
/* Free stream pool */
|
|
|
|
guac_pool_free(client->__stream_pool);
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Close associated plugin */
|
|
|
|
if (client->__plugin_handle != NULL) {
|
|
|
|
if (dlclose(client->__plugin_handle))
|
|
|
|
guac_client_log(client, GUAC_LOG_ERROR, "Unable to close plugin: %s", dlerror());
|
2011-03-17 06:46:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
pthread_rwlock_destroy(&(client->__users_lock));
|
|
|
|
free(client);
|
2011-03-17 06:46:02 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int guac_client_add_user(guac_client* client, guac_user* user, int argc, char** argv) {
|
|
|
|
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
pthread_rwlock_wrlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
/* Call handler, if defined */
|
|
|
|
if (client->join_handler)
|
|
|
|
retval = client->join_handler(user, argc, argv);
|
|
|
|
|
|
|
|
/* Add to list if join was successful */
|
|
|
|
if (retval == 0) {
|
|
|
|
|
|
|
|
user->__prev = NULL;
|
|
|
|
user->__next = client->__users;
|
|
|
|
|
|
|
|
if (client->__users != NULL)
|
|
|
|
client->__users->__prev = user;
|
|
|
|
|
|
|
|
client->__users = user;
|
|
|
|
client->connected_users++;
|
|
|
|
|
|
|
|
/* Update owner pointer if user is owner */
|
|
|
|
if (user->owner)
|
|
|
|
client->__owner = user;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_unlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_client_remove_user(guac_client* client, guac_user* user) {
|
|
|
|
|
|
|
|
pthread_rwlock_wrlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
/* Call handler, if defined */
|
|
|
|
if (user->leave_handler)
|
|
|
|
user->leave_handler(user);
|
|
|
|
else if (client->leave_handler)
|
|
|
|
client->leave_handler(user);
|
|
|
|
|
|
|
|
/* Update prev / head */
|
|
|
|
if (user->__prev != NULL)
|
|
|
|
user->__prev->__next = user->__next;
|
|
|
|
else
|
|
|
|
client->__users = user->__next;
|
|
|
|
|
|
|
|
/* Update next */
|
|
|
|
if (user->__next != NULL)
|
|
|
|
user->__next->__prev = user->__prev;
|
|
|
|
|
|
|
|
client->connected_users--;
|
|
|
|
|
|
|
|
/* Update owner pointer if user was owner */
|
|
|
|
if (user->owner)
|
|
|
|
client->__owner = NULL;
|
|
|
|
|
|
|
|
pthread_rwlock_unlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_client_foreach_user(guac_client* client, guac_user_callback* callback, void* data) {
|
|
|
|
|
|
|
|
guac_user* current;
|
|
|
|
|
|
|
|
pthread_rwlock_rdlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
/* Call function on each user */
|
|
|
|
current = client->__users;
|
|
|
|
while (current != NULL) {
|
|
|
|
callback(current, data);
|
|
|
|
current = current->__next;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_unlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void* guac_client_for_owner(guac_client* client, guac_user_callback* callback,
|
|
|
|
void* data) {
|
|
|
|
|
|
|
|
void* retval;
|
|
|
|
|
|
|
|
pthread_rwlock_rdlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
/* Invoke callback with current owner */
|
|
|
|
retval = callback(client->__owner, data);
|
|
|
|
|
|
|
|
pthread_rwlock_unlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
/* Return value from callback */
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-05 00:44:30 +00:00
|
|
|
void* guac_client_for_user(guac_client* client, guac_user* user,
|
|
|
|
guac_user_callback* callback, void* data) {
|
|
|
|
|
|
|
|
guac_user* current;
|
|
|
|
|
|
|
|
int user_valid = 0;
|
|
|
|
void* retval;
|
|
|
|
|
|
|
|
pthread_rwlock_rdlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
/* Loop through all users, searching for a pointer to the given user */
|
|
|
|
current = client->__users;
|
|
|
|
while (current != NULL) {
|
|
|
|
|
|
|
|
/* If the user's pointer exists in the list, they are indeed valid */
|
|
|
|
if (current == user) {
|
|
|
|
user_valid = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = current->__next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use NULL if user does not actually exist */
|
|
|
|
if (!user_valid)
|
|
|
|
user = NULL;
|
|
|
|
|
|
|
|
/* Invoke callback with requested user (if they exist) */
|
|
|
|
retval = callback(user, data);
|
|
|
|
|
|
|
|
pthread_rwlock_unlock(&(client->__users_lock));
|
|
|
|
|
|
|
|
/* Return value from callback */
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int guac_client_end_frame(guac_client* client) {
|
|
|
|
|
|
|
|
/* Update and send timestamp */
|
|
|
|
client->last_sent_timestamp = guac_timestamp_current();
|
|
|
|
return guac_protocol_send_sync(client->socket, client->last_sent_timestamp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty NULL-terminated array of argument names.
|
|
|
|
*/
|
|
|
|
const char* __GUAC_CLIENT_NO_ARGS[] = { NULL };
|
|
|
|
|
|
|
|
int guac_client_load_plugin(guac_client* client, const char* protocol) {
|
|
|
|
|
|
|
|
/* Reference to dlopen()'d plugin */
|
|
|
|
void* client_plugin_handle;
|
|
|
|
|
|
|
|
/* Pluggable client */
|
|
|
|
char protocol_lib[GUAC_PROTOCOL_LIBRARY_LIMIT] =
|
|
|
|
GUAC_PROTOCOL_LIBRARY_PREFIX;
|
2016-03-01 19:04:55 +00:00
|
|
|
|
2016-03-01 18:31:33 +00:00
|
|
|
/* Type-pun for the sake of dlsym() - cannot typecast a void* to a function
|
|
|
|
* pointer otherwise */
|
2016-03-01 05:35:32 +00:00
|
|
|
union {
|
|
|
|
guac_client_init_handler* client_init;
|
|
|
|
void* obj;
|
|
|
|
} alias;
|
|
|
|
|
|
|
|
/* Add protocol and .so suffix to protocol_lib */
|
|
|
|
strncat(protocol_lib, protocol, GUAC_PROTOCOL_NAME_LIMIT-1);
|
|
|
|
strcat(protocol_lib, GUAC_PROTOCOL_LIBRARY_SUFFIX);
|
|
|
|
|
|
|
|
/* Load client plugin */
|
|
|
|
client_plugin_handle = dlopen(protocol_lib, RTLD_LAZY);
|
|
|
|
if (!client_plugin_handle) {
|
|
|
|
guac_error = GUAC_STATUS_NOT_FOUND;
|
|
|
|
guac_error_message = dlerror();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlerror(); /* Clear errors */
|
|
|
|
|
|
|
|
/* Get init function */
|
|
|
|
alias.obj = dlsym(client_plugin_handle, "guac_client_init");
|
|
|
|
|
|
|
|
/* Fail if cannot find guac_client_init */
|
|
|
|
if (dlerror() != NULL) {
|
|
|
|
guac_error = GUAC_STATUS_INTERNAL_ERROR;
|
|
|
|
guac_error_message = dlerror();
|
2016-03-02 18:54:58 +00:00
|
|
|
dlclose(client_plugin_handle);
|
2016-03-01 05:35:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Init client */
|
|
|
|
client->args = __GUAC_CLIENT_NO_ARGS;
|
|
|
|
client->__plugin_handle = client_plugin_handle;
|
|
|
|
|
|
|
|
return alias.client_init(client);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the provided approximate processing lag, taking into account the
|
|
|
|
* processing lag of the given user.
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The guac_user to use to update the approximate processing lag.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* Pointer to an int containing the current approximate processing lag.
|
|
|
|
* The int will be updated according to the processing lag of the given
|
|
|
|
* user.
|
2016-03-01 19:04:55 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Always NULL.
|
2016-03-01 05:35:32 +00:00
|
|
|
*/
|
|
|
|
static void* __calculate_lag(guac_user* user, void* data) {
|
|
|
|
|
|
|
|
int* processing_lag = (int*) data;
|
|
|
|
|
|
|
|
/* Simply find maximum */
|
|
|
|
if (user->processing_lag > *processing_lag)
|
|
|
|
*processing_lag = user->processing_lag;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int guac_client_get_processing_lag(guac_client* client) {
|
|
|
|
|
|
|
|
int processing_lag = 0;
|
|
|
|
|
|
|
|
/* Approximate the processing lag of all users */
|
|
|
|
guac_client_foreach_user(client, __calculate_lag, &processing_lag);
|
|
|
|
|
|
|
|
return processing_lag;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-11 23:26:24 +00:00
|
|
|
void guac_client_stream_png(guac_client* client, guac_socket* socket,
|
|
|
|
guac_composite_mode mode, const guac_layer* layer, int x, int y,
|
|
|
|
cairo_surface_t* surface) {
|
|
|
|
|
|
|
|
/* Allocate new stream for image */
|
|
|
|
guac_stream* stream = guac_client_alloc_stream(client);
|
|
|
|
|
|
|
|
/* Declare stream as containing image data */
|
|
|
|
guac_protocol_send_img(socket, stream, mode, layer, "image/png", x, y);
|
|
|
|
|
|
|
|
/* Write PNG data */
|
|
|
|
guac_png_write(socket, stream, surface);
|
|
|
|
|
|
|
|
/* Terminate stream */
|
|
|
|
guac_protocol_send_end(socket, stream);
|
|
|
|
|
|
|
|
/* Free allocated stream */
|
|
|
|
guac_client_free_stream(client, stream);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-12 03:43:46 +00:00
|
|
|
void guac_client_stream_jpeg(guac_client* client, guac_socket* socket,
|
|
|
|
guac_composite_mode mode, const guac_layer* layer, int x, int y,
|
|
|
|
cairo_surface_t* surface, int quality) {
|
|
|
|
|
|
|
|
/* Allocate new stream for image */
|
|
|
|
guac_stream* stream = guac_client_alloc_stream(client);
|
|
|
|
|
|
|
|
/* Declare stream as containing image data */
|
|
|
|
guac_protocol_send_img(socket, stream, mode, layer, "image/jpeg", x, y);
|
|
|
|
|
|
|
|
/* Write JPEG data */
|
|
|
|
guac_jpeg_write(socket, stream, surface, quality);
|
|
|
|
|
|
|
|
/* Terminate stream */
|
|
|
|
guac_protocol_send_end(socket, stream);
|
|
|
|
|
|
|
|
/* Free allocated stream */
|
|
|
|
guac_client_free_stream(client, stream);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-21 01:34:44 +00:00
|
|
|
void guac_client_stream_webp(guac_client* client, guac_socket* socket,
|
|
|
|
guac_composite_mode mode, const guac_layer* layer, int x, int y,
|
2015-09-22 18:52:56 +00:00
|
|
|
cairo_surface_t* surface, int quality, int lossless) {
|
2015-09-21 01:34:44 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_WEBP
|
|
|
|
/* Allocate new stream for image */
|
|
|
|
guac_stream* stream = guac_client_alloc_stream(client);
|
|
|
|
|
|
|
|
/* Declare stream as containing image data */
|
|
|
|
guac_protocol_send_img(socket, stream, mode, layer, "image/webp", x, y);
|
|
|
|
|
|
|
|
/* Write WebP data */
|
2015-09-22 18:52:56 +00:00
|
|
|
guac_webp_write(socket, stream, surface, quality, lossless);
|
2015-09-21 01:34:44 +00:00
|
|
|
|
|
|
|
/* Terminate stream */
|
|
|
|
guac_protocol_send_end(socket, stream);
|
|
|
|
|
|
|
|
/* Free allocated stream */
|
|
|
|
guac_client_free_stream(client, stream);
|
|
|
|
#else
|
|
|
|
/* Do nothing if WebP support is not built in */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_WEBP
|
2016-03-01 05:35:32 +00:00
|
|
|
/**
|
|
|
|
* Callback which is invoked by guac_client_supports_webp() for each user
|
|
|
|
* associated with the given client, thus updating an overall support flag
|
|
|
|
* describing the WebP support state for the client as a whole.
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The user to check for WebP support.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* Pointer to an int containing the current WebP support status for the
|
|
|
|
* client associated with the given user. This flag will be 0 if any user
|
|
|
|
* already checked has lacked WebP support, or 1 otherwise.
|
2016-03-01 19:04:55 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Always NULL.
|
2016-03-01 05:35:32 +00:00
|
|
|
*/
|
|
|
|
static void* __webp_support_callback(guac_user* user, void* data) {
|
2015-09-21 01:34:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int* webp_supported = (int*) data;
|
2015-09-21 01:34:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Check whether current user supports WebP */
|
|
|
|
if (*webp_supported)
|
|
|
|
*webp_supported = guac_user_supports_webp(user);
|
2015-09-21 01:34:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
return NULL;
|
2015-09-21 01:34:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-09-21 01:34:44 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int guac_client_supports_webp(guac_client* client) {
|
|
|
|
|
|
|
|
#ifdef ENABLE_WEBP
|
|
|
|
int webp_supported = 1;
|
|
|
|
|
|
|
|
/* WebP is supported for entire client only if each user supports it */
|
|
|
|
guac_client_foreach_user(client, __webp_support_callback, &webp_supported);
|
|
|
|
|
|
|
|
return webp_supported;
|
2015-09-21 01:34:44 +00:00
|
|
|
#else
|
|
|
|
/* Support for WebP is completely absent */
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|