Removed client registry, use of UUID, and semaphore. Removed handshaking phase. (simplify proxy)

This commit is contained in:
Michael Jumper
2010-09-24 02:59:15 -07:00
parent a1440075d0
commit b3c1017b8e
8 changed files with 11 additions and 396 deletions

View File

@@ -20,7 +20,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <uuid/uuid.h>
#include <syslog.h>
@@ -56,15 +55,6 @@ void guac_free_png_buffer(png_byte** png_buffer, int h) {
}
void __guac_set_client_io(guac_client* client, GUACIO* io) {
sem_wait(&(client->io_lock)); /* Acquire I/O */
client->io = io;
}
void __guac_release_client_io(guac_client* client) {
sem_post(&(client->io_lock));
}
guac_client* __guac_alloc_client(GUACIO* io) {
/* Allocate new client (not handoff) */
@@ -73,18 +63,15 @@ guac_client* __guac_alloc_client(GUACIO* io) {
/* Init new client */
client->io = io;
uuid_generate(client->uuid);
sem_init(&(client->io_lock), 0, 0); /* I/O starts locked */
return client;
}
guac_client* guac_get_client(int client_fd, guac_client_registry* registry, guac_client_init_handler* client_init, int argc, char** argv) {
guac_client* guac_get_client(int client_fd, guac_client_init_handler* client_init, int argc, char** argv) {
guac_client* client;
GUACIO* io = guac_open(client_fd);
guac_instruction instruction;
/* Make copies of arguments */
char** safe_argv = malloc(argc * sizeof(char*));
@@ -94,65 +81,11 @@ guac_client* guac_get_client(int client_fd, guac_client_registry* registry, guac
for (i=0; i<argc; i++)
scratch_argv[i] = safe_argv[i] = strdup(argv[i]);
/* Wait for handshaking messages */
for (;;) {
/* Create new client */
client = __guac_alloc_client(io);
int retval;
retval = guac_read_instruction(io, &instruction); /* 0 if no instructions finished yet, <0 if error or EOF */
if (retval > 0) {
/* connect -> create new client connection */
if (strcmp(instruction.opcode, "connect") == 0) {
/* Create new client */
client = __guac_alloc_client(io);
/* Register client */
if (registry) {
guac_register_client(registry, client);
/* Send UUID to web-client */
guac_send_uuid(io, client->uuid);
guac_flush(client->io);
}
if (client_init(client, argc, scratch_argv) != 0)
return NULL;
break;
}
/* resume -> resume existing connection (when that connection pauses) */
if (strcmp(instruction.opcode, "resume") == 0) {
if (registry) {
client = guac_find_client(
registry,
(unsigned char*) guac_decode_base64_inplace(instruction.argv[0])
);
if (client) {
__guac_set_client_io(client, io);
return NULL; /* Returning NULL, so old client loop is used */
/* FIXME: Fix semantics of returning NULL vs ptr. This function needs redocumentation, and callers
* need to lose their "error" handling. */
}
}
return NULL;
}
}
if (retval < 0)
return NULL; /* EOF or error */
/* Otherwise, retval == 0 implies unfinished instruction */
}
if (client_init(client, argc, scratch_argv) != 0)
return NULL;
/* Free memory used for arg copy */
for (i=0; i<argc; i++)
@@ -166,7 +99,7 @@ guac_client* guac_get_client(int client_fd, guac_client_registry* registry, guac
}
void guac_free_client(guac_client* client, guac_client_registry* registry) {
void guac_free_client(guac_client* client) {
if (client->free_handler) {
if (client->free_handler(client))
@@ -175,8 +108,6 @@ void guac_free_client(guac_client* client, guac_client_registry* registry) {
guac_close(client->io);
guac_remove_client(registry, client->uuid);
free(client);
}
@@ -271,13 +202,6 @@ void guac_start_client(guac_client* client) {
}
}
else if (strcmp(instruction.opcode, "pause") == 0) {
/* Allow other connection to take over I/O */
__guac_release_client_io(client);
}
else if (strcmp(instruction.opcode, "disconnect") == 0) {
syslog(LOG_INFO, "Client requested disconnect");
return;

View File

@@ -1,63 +0,0 @@
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _CLIENTREG_H
#define _CLIENTREG_H
#include <stdlib.h>
#include <uuid/uuid.h>
#include <semaphore.h>
#include "uuidtree.h"
#include "client.h"
guac_client_registry* guac_create_client_registry() {
guac_client_registry* registry = malloc(sizeof(guac_client_registry));
registry->root = guac_create_uuid_tree();
sem_init(&(registry->tree_lock), 0, 1);
return registry;
}
void guac_register_client(guac_client_registry* registry, guac_client* client) {
sem_wait(&(registry->tree_lock));
guac_uuid_tree_put(registry->root, client->uuid, client);
sem_post(&(registry->tree_lock));
}
guac_client* guac_find_client(guac_client_registry* registry, uuid_t uuid) {
return (guac_client*) guac_uuid_tree_get(registry->root, uuid);
}
void guac_remove_client(guac_client_registry* registry, uuid_t uuid) {
sem_wait(&(registry->tree_lock));
guac_uuid_tree_remove(registry->root, uuid);
sem_post(&(registry->tree_lock));
}
void guac_cleanup_registry(guac_client_registry* registry) {
sem_wait(&(registry->tree_lock));
guac_cleanup_uuid_tree(registry->root);
sem_destroy(&(registry->tree_lock));
}
#endif

View File

@@ -26,8 +26,6 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <uuid/uuid.h>
#include "guacio.h"
#include "protocol.h"
@@ -208,15 +206,6 @@ void guac_send_size(GUACIO* io, int w, int h) {
guac_write_string(io, ";");
}
void guac_send_uuid(GUACIO* io, uuid_t uuid) {
guac_write_string(io, "uuid:");
guac_write_base64(io, uuid, 16);
guac_flush_base64(io);
guac_write_string(io, ";");
}
void guac_send_clipboard(GUACIO* io, const char* data) {
char* escaped = guac_escape_string(data);

View File

@@ -1,139 +0,0 @@
/*
* Guacamole - Clientless Remote Desktop
* Copyright (C) 2010 Michael Jumper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <uuid/uuid.h>
#include "uuidtree.h"
guac_uuid_tree_node* guac_create_uuid_tree() {
guac_uuid_tree_node* tree = malloc(sizeof(guac_uuid_tree_node));
tree->used = 0;
memset(tree->next, 0, sizeof(tree->next));
return tree;
}
void guac_uuid_tree_put(guac_uuid_tree_node* tree, uuid_t uuid, void* obj) {
guac_uuid_tree_node* current = tree;
int i;
unsigned char index;
for (i=0; i<sizeof(uuid_t)-1; i++) {
guac_uuid_tree_node* next;
/* Get next tree node */
index = ((unsigned char*) uuid)[i];
next = ((guac_uuid_tree_node**) current->next)[index];
/* If no node, allocate one */
if (next == NULL) {
current->used++;
next = guac_create_uuid_tree();
((guac_uuid_tree_node**) current->next)[index] = next;
}
current = next;
}
/* Store object */
index = ((unsigned char*) uuid)[i];
current->next[index] = obj;
}
void* guac_uuid_tree_get(guac_uuid_tree_node* tree, uuid_t uuid) {
guac_uuid_tree_node* current = tree;
int i;
unsigned char index;
for (i=0; i<sizeof(uuid_t)-1; i++) {
/* Get next tree node */
index = ((unsigned char*) uuid)[i];
current = ((guac_uuid_tree_node**) current->next)[index];
/* If no node, not present */
if (current == NULL)
return NULL;
}
/* Return if found */
index = ((unsigned char*) uuid)[i];
return current->next[index];
}
void guac_uuid_tree_remove(guac_uuid_tree_node* tree, uuid_t uuid) {
guac_uuid_tree_node* current = tree;
int i;
unsigned char index;
for (i=0; i<sizeof(uuid_t)-1; i++) {
/* Get next tree node */
index = ((unsigned char*) uuid)[i];
current = ((guac_uuid_tree_node**) current->next)[index];
/* If no node, nothing to remove */
if (current == NULL)
return;
}
/* Remove, if present*/
if (current->next[index]) {
current->next[index] = NULL;
current->used--;
/* FIXME: If no more objects at this node, clean up */
if (current->used == 0) {
/* STUB */
}
}
}
void guac_cleanup_uuid_tree(guac_uuid_tree_node* tree) {
int i;
for (i=0; i<sizeof(tree->next); i++) {
if (tree->next[i] != NULL) {
guac_cleanup_uuid_tree(tree->next[i]);
tree->next[i] = NULL;
}
}
free(tree);
}