diff --git a/guacamole/libguac/Makefile b/guacamole/libguac/Makefile index 76d826524..16368e22b 100644 --- a/guacamole/libguac/Makefile +++ b/guacamole/libguac/Makefile @@ -12,12 +12,18 @@ doc: clean-doc: $(RM) -R doc -libguac.so: client.o guacio.o protocol.o - $(CC) $(CFLAGS) -shared $(LDFLAGS) -Wl,-soname,libguac.so.0 -o libguac.so client.o protocol.o guacio.o +libguac.so: client.o guacio.o protocol.o clientreg.o uuidtree.o + $(CC) $(CFLAGS) -shared $(LDFLAGS) -Wl,-soname,libguac.so.0 -o libguac.so client.o protocol.o guacio.o clientreg.o uuidtree.o client.o: client.c include/client.h include/guacio.h $(CC) $(CFLAGS) -c client.c +clientreg.o: clientreg.c include/uuidtree.h include/client.h + $(CC) $(CFLAGS) -c clientreg.c + +uuidtree.o: uuidtree.c include/uuidtree.h + $(CC) $(CFLAGS) -c uuidtree.c + protocol.o: protocol.c include/protocol.h include/guacio.h $(CC) $(CFLAGS) -c protocol.c diff --git a/guacamole/libguac/client.c b/guacamole/libguac/client.c index 5af516748..4d8c1c3f3 100644 --- a/guacamole/libguac/client.c +++ b/guacamole/libguac/client.c @@ -26,122 +26,6 @@ #include "protocol.h" #include "client.h" -/* TODO: Make registry thread-safe */ - -guac_client_registry_node* guac_create_client_registry() { - - guac_client_registry_node* registry = malloc(sizeof(guac_client_registry_node)); - - registry->used = 0; - memset(registry->next, 0, sizeof(registry->next)); - - return registry; - -} - -void guac_register_client(guac_client_registry_node* registry, guac_client* client) { - - guac_client_registry_node* current = registry; - int i; - unsigned char index; - - for (i=0; iuuid)[i]; - next = ((guac_client_registry_node**) current->next)[index]; - - /* If no node, allocate one */ - if (next == NULL) { - current->used++; - next = guac_create_client_registry(); - ((guac_client_registry_node**) current->next)[index] = next; - } - - current = next; - } - - /* Register client */ - index = ((unsigned char*) client->uuid)[i]; - ((guac_client**) current->next)[index] = client; - -} - -guac_client* guac_find_client(guac_client_registry_node* registry, uuid_t uuid) { - - guac_client_registry_node* current = registry; - int i; - unsigned char index; - - for (i=0; inext)[index]; - - /* If no node, client not registered */ - if (current == NULL) - return NULL; - - } - - /* Return client found (if any) */ - index = ((unsigned char*) uuid)[i]; - return ((guac_client**) current->next)[index]; - -} - -void guac_remove_client(guac_client_registry_node* registry, guac_client* client) { - - guac_client_registry_node* current = registry; - int i; - unsigned char index; - - for (i=0; iuuid)[i]; - current = ((guac_client_registry_node**) current->next)[index]; - - /* If no node, client not registered */ - if (current == NULL) - return; - - } - - /* Remove client, if registered */ - if (((guac_client**) current->next)[index]) { - ((guac_client**) current->next)[index] = NULL; - current->used--; - - /* FIXME: If no more clients at this node, clean up */ - if (current->used == 0) { - /* STUB */ - } - - } - -} - -void guac_cleanup_client_registry(guac_client_registry_node* registry) { - - int i; - for (i=0; inext); i++) { - - if (registry->next[i] != NULL) { - guac_cleanup_client_registry(registry->next[i]); - registry->next[i] = NULL; - } - - } - - free(registry); - -} - - png_byte** guac_alloc_png_buffer(int w, int h, int bpp) { @@ -183,7 +67,7 @@ guac_client* __guac_alloc_client(GUACIO* io) { } -guac_client* guac_get_client(int client_fd, guac_client_registry_node* registry, guac_client_init_handler* client_init, int argc, char** argv) { +guac_client* guac_get_client(int client_fd, guac_client_registry* registry, guac_client_init_handler* client_init, int argc, char** argv) { guac_client* client; GUACIO* io = guac_open(client_fd); @@ -247,14 +131,14 @@ guac_client* guac_get_client(int client_fd, guac_client_registry_node* registry, } -void guac_free_client(guac_client* client, guac_client_registry_node* registry) { +void guac_free_client(guac_client* client, guac_client_registry* registry) { if (client->free_handler) client->free_handler(client); guac_close(client->io); - guac_remove_client(registry, client); + guac_remove_client(registry, client->uuid); free(client); } diff --git a/guacamole/libguac/clientreg.c b/guacamole/libguac/clientreg.c new file mode 100644 index 000000000..fb3e13b20 --- /dev/null +++ b/guacamole/libguac/clientreg.c @@ -0,0 +1,55 @@ + +/* + * 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 . + */ + +#ifndef _CLIENTREG_H +#define _CLIENTREG_H + +#include +#include + +#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(); + + return registry; + +} + +void guac_register_client(guac_client_registry* registry, guac_client* client) { + guac_uuid_tree_put(registry->root, client->uuid, client); +} + +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) { + guac_uuid_tree_remove(registry->root, uuid); +} + +void guac_cleanup_registry(guac_client_registry* registry) { + guac_cleanup_uuid_tree(registry->root); +} + +#endif diff --git a/guacamole/libguac/include/client.h b/guacamole/libguac/include/client.h index 1f0c1e9db..1d8358158 100644 --- a/guacamole/libguac/include/client.h +++ b/guacamole/libguac/include/client.h @@ -22,6 +22,7 @@ #include #include +#include "uuidtree.h" #include "guacio.h" @@ -32,7 +33,7 @@ */ typedef struct guac_client guac_client; -typedef struct guac_client_registry_node guac_client_registry_node; +typedef struct guac_client_registry guac_client_registry; typedef void guac_client_handle_messages(guac_client* client); typedef void guac_client_mouse_handler(guac_client* client, int x, int y, int button_mask); @@ -188,7 +189,7 @@ typedef void guac_client_init_handler(guac_client* client, int argc, char** argv * @param argv The arguments being passed to this client. * @return A pointer to the newly initialized (or found) client. */ -guac_client* guac_get_client(int client_fd, guac_client_registry_node* registry, guac_client_init_handler* client_init, int argc, char** argv); +guac_client* guac_get_client(int client_fd, guac_client_registry* registry, guac_client_init_handler* client_init, int argc, char** argv); /** * Enter the main network message handling loop for the given client. @@ -203,7 +204,7 @@ void guac_start_client(guac_client* client); * @param client The proxy client to free all reasources of. * @param registry The registry to remove this client from when freed. */ -void guac_free_client(guac_client* client, guac_client_registry_node* registry); +void guac_free_client(guac_client* client, guac_client_registry* registry); /** * Allocate a libpng-compatible buffer to hold raw image data. @@ -226,66 +227,22 @@ void guac_free_png_buffer(png_byte** png_buffer, int h); /** - * Represents a single node of the Guacamole client registry. The - * Guacamole client registry contains references to all active clients, - * indexed by client UUID. + * Represent the Guacamole "client registry" in which all + * currently connected clients are stored, indexed by UUID. */ -struct guac_client_registry_node { +struct guac_client_registry { /** - * The number of pointers used inside the next array. + * Root of the uuid tree */ - int used; - - /** - * The next guac_client_registry_node if currently looking at any byte - * of the UUID except the last, or the guac_client if looking at the - * last byte of the UUID. - */ - void* next[256]; + guac_uuid_tree_node* root; }; -/** - * Registers the given client in the client registry by that client's UUID. - * - * @param registry The registry to register the client within. - * @param client The client to register. - */ -void guac_register_client(guac_client_registry_node* registry, guac_client* client); - -/** - * Returns the client from the client registry associated with the given UUID. - * - * @param registry The registry to search. - * @param uuid The uuid of the client to lookup. - * @return The client, if found, or NULL if no such client has been registered. - */ -guac_client* guac_find_client(guac_client_registry_node* registry, uuid_t uuid); - -/** - * Removes the given client from the client registry. - * - * @param registry The registry to remove the client from. - * @param client The client to remove. - */ -void guac_remove_client(guac_client_registry_node* registry, guac_client* client); - -/** - * Creates a new client registry. - * - * @return The newly allocated and initialized registry. - */ -guac_client_registry_node* guac_create_client_registry(); - -/** - * Frees all memory associated with the given client registry. - * - * @param registry The registry to clean up. - */ -void guac_cleanup_client_registry(guac_client_registry_node* registry); - - - +guac_client_registry* guac_create_client_registry(); +void guac_register_client(guac_client_registry* registry, guac_client* client); +guac_client* guac_find_client(guac_client_registry* registry, uuid_t uuid); +void guac_remove_client(guac_client_registry* registry, uuid_t uuid); +void guac_cleanup_registry(guac_client_registry* registry); #endif diff --git a/guacamole/libguac/include/uuidtree.h b/guacamole/libguac/include/uuidtree.h new file mode 100644 index 000000000..683ccf9c2 --- /dev/null +++ b/guacamole/libguac/include/uuidtree.h @@ -0,0 +1,52 @@ + +/* + * 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 . + */ + +#ifndef _UUIDTREE_H +#define _UUIDTREE_H + +#include + +typedef struct guac_uuid_tree_node guac_uuid_tree_node; + +/** + * Represents a single node of a tree storing objects by UUID. + */ +struct guac_uuid_tree_node { + + /** + * The number of pointers used inside the next array. + */ + int used; + + /** + * The next guac_uuid_tree_node if currently looking at any byte + * of the UUID except the last, or the stored object if looking at the + * last byte of the UUID. + */ + void* next[256]; + +}; + +guac_uuid_tree_node* guac_create_uuid_tree(); +void guac_uuid_tree_put(guac_uuid_tree_node* tree, uuid_t uuid, void* obj); +void* guac_uuid_tree_get(guac_uuid_tree_node* tree, uuid_t uuid); +void guac_uuid_tree_remove(guac_uuid_tree_node* tree, uuid_t uuid); +void guac_cleanup_uuid_tree(guac_uuid_tree_node* tree); + +#endif diff --git a/guacamole/libguac/uuidtree.c b/guacamole/libguac/uuidtree.c new file mode 100644 index 000000000..ca0e4cc61 --- /dev/null +++ b/guacamole/libguac/uuidtree.c @@ -0,0 +1,139 @@ + +/* + * 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 . + */ + +#include +#include +#include +#include + +#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; inext)[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; inext)[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; inext)[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; inext); i++) { + + if (tree->next[i] != NULL) { + guac_cleanup_uuid_tree(tree->next[i]); + tree->next[i] = NULL; + } + + } + + free(tree); + +} + diff --git a/guacamole/proxy/daemon.c b/guacamole/proxy/daemon.c index 36820856b..a49c5076b 100644 --- a/guacamole/proxy/daemon.c +++ b/guacamole/proxy/daemon.c @@ -34,7 +34,7 @@ typedef struct client_thread_data { int fd; guac_client_init_handler* client_init; - guac_client_registry_node* registry; + guac_client_registry* registry; int argc; char** argv; @@ -74,7 +74,7 @@ void* start_client_thread(void* data) { int main(int argc, char* argv[]) { /* Client registry */ - guac_client_registry_node* registry; + guac_client_registry* registry; /* Pluggable client */ void* client_plugin_handle;