2013-12-29 04:53:12 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
2010-12-08 21:14:04 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-02-16 02:04:36 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-12-29 04:53:12 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
|
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"
|
2018-10-19 16:30:20 +00:00
|
|
|
#include "guacamole/client.h"
|
|
|
|
#include "guacamole/error.h"
|
|
|
|
#include "guacamole/layer.h"
|
|
|
|
#include "guacamole/plugin.h"
|
|
|
|
#include "guacamole/pool.h"
|
|
|
|
#include "guacamole/protocol.h"
|
|
|
|
#include "guacamole/socket.h"
|
|
|
|
#include "guacamole/stream.h"
|
2018-11-12 23:38:02 +00:00
|
|
|
#include "guacamole/string.h"
|
2018-10-19 16:30:20 +00:00
|
|
|
#include "guacamole/timestamp.h"
|
|
|
|
#include "guacamole/user.h"
|
2016-03-01 05:35:32 +00:00
|
|
|
#include "id.h"
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
#include <dlfcn.h>
|
2017-03-30 05:32:26 +00:00
|
|
|
#include <inttypes.h>
|
2016-03-01 05:35:32 +00:00
|
|
|
#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>
|
|
|
|
|
2016-09-11 20:56:26 +00:00
|
|
|
/**
|
|
|
|
* Empty NULL-terminated array of argument names.
|
|
|
|
*/
|
|
|
|
const char* __GUAC_CLIENT_NO_ARGS[] = { NULL };
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-09-11 20:56:26 +00:00
|
|
|
client->args = __GUAC_CLIENT_NO_ARGS;
|
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 */
|
2016-12-21 07:58:53 +00:00
|
|
|
client->socket = guac_socket_broadcast(client);
|
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));
|
2016-03-17 23:03:58 +00:00
|
|
|
free(client->connection_id);
|
2016-03-01 05:35:32 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
/* Call handler, if defined */
|
|
|
|
if (client->join_handler)
|
|
|
|
retval = client->join_handler(user, argc, argv);
|
|
|
|
|
2016-10-07 20:09:59 +00:00
|
|
|
pthread_rwlock_wrlock(&(client->__users_lock));
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* 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));
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
2016-10-07 20:09:59 +00:00
|
|
|
/* Call handler, if defined */
|
|
|
|
if (user->leave_handler)
|
|
|
|
user->leave_handler(user);
|
|
|
|
else if (client->leave_handler)
|
|
|
|
client->leave_handler(user);
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2017-03-30 05:32:26 +00:00
|
|
|
|
|
|
|
/* Log received timestamp and calculated lag (at TRACE level only) */
|
|
|
|
guac_client_log(client, GUAC_LOG_TRACE, "Server completed "
|
|
|
|
"frame %" PRIu64 "ms.", client->last_sent_timestamp);
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
return guac_protocol_send_sync(client->socket, client->last_sent_timestamp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2018-11-12 23:38:02 +00:00
|
|
|
guac_strlcat(protocol_lib, protocol, sizeof(protocol_lib));
|
|
|
|
if (guac_strlcat(protocol_lib, GUAC_PROTOCOL_LIBRARY_SUFFIX,
|
|
|
|
sizeof(protocol_lib)) >= sizeof(protocol_lib)) {
|
|
|
|
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
|
|
guac_error_message = "Protocol name is too long";
|
|
|
|
return -1;
|
|
|
|
}
|
2016-03-01 05:35:32 +00:00
|
|
|
|
|
|
|
/* 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->__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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-30 20:51:10 +00:00
|
|
|
void guac_client_stream_argv(guac_client* client, guac_socket* socket,
|
|
|
|
const char* mimetype, const char* name, const char* value) {
|
|
|
|
|
|
|
|
/* Allocate new stream for argument value */
|
|
|
|
guac_stream* stream = guac_client_alloc_stream(client);
|
|
|
|
|
|
|
|
/* Declare stream as containing connection parameter data */
|
|
|
|
guac_protocol_send_argv(socket, stream, mimetype, name);
|
|
|
|
|
|
|
|
/* Write parameter data */
|
|
|
|
guac_protocol_send_blobs(socket, stream, value, strlen(value));
|
|
|
|
|
|
|
|
/* Terminate stream */
|
|
|
|
guac_protocol_send_end(socket, stream);
|
|
|
|
|
|
|
|
/* Free allocated stream */
|
|
|
|
guac_client_free_stream(client, stream);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|