Added client registry, added GUACIO transfer operation

This commit is contained in:
Michael Jumper
2010-09-18 00:21:49 -07:00
parent 96b94f60e9
commit 16d335aa19
5 changed files with 217 additions and 7 deletions

View File

@@ -27,6 +27,122 @@
#include "client.h" #include "client.h"
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; i<sizeof(uuid_t)-1; i++) {
guac_client_registry_node* next;
/* Get next registry node */
index = ((unsigned char*) client->uuid)[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; i<sizeof(uuid_t)-1; i++) {
/* Get next registry node */
index = ((unsigned char*) uuid)[i];
current = ((guac_client_registry_node**) current->next)[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; i<sizeof(uuid_t)-1; i++) {
/* Get next registry node */
index = ((unsigned char*) client->uuid)[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; i<sizeof(registry->next); 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) { png_byte** guac_alloc_png_buffer(int w, int h, int bpp) {
png_byte** png_buffer; png_byte** png_buffer;
@@ -67,7 +183,7 @@ guac_client* __guac_alloc_client(GUACIO* io) {
} }
guac_client* guac_get_client(int client_fd, guac_client_init_handler* client_init, const char* hostname, int port) { guac_client* guac_get_client(int client_fd, guac_client_registry_node* registry, guac_client_init_handler* client_init, const char* hostname, int port) {
guac_client* client; guac_client* client;
GUACIO* io = guac_open(client_fd); GUACIO* io = guac_open(client_fd);
@@ -83,7 +199,15 @@ guac_client* guac_get_client(int client_fd, guac_client_init_handler* client_ini
/* connect -> create new client connection */ /* connect -> create new client connection */
if (strcmp(instruction.opcode, "connect") == 0) { if (strcmp(instruction.opcode, "connect") == 0) {
/* Create new client */
client = __guac_alloc_client(io); 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_send_uuid(io, client->uuid);
break; break;
} }
@@ -105,12 +229,15 @@ guac_client* guac_get_client(int client_fd, guac_client_init_handler* client_ini
} }
void guac_free_client(guac_client* client) { void guac_free_client(guac_client* client, guac_client_registry_node* registry) {
if (client->free_handler) if (client->free_handler)
client->free_handler(client); client->free_handler(client);
guac_close(client->io); guac_close(client->io);
guac_remove_client(registry, client);
free(client); free(client);
} }

View File

@@ -49,12 +49,20 @@ GUACIO* guac_open(int fd) {
io->instructionbuf_used_length = 0; io->instructionbuf_used_length = 0;
/* Set limit */ /* Set limit */
io->transfer_limit = 256; io->transfer_limit = 0;
return io; return io;
} }
void guac_transfer(GUACIO* io, int fd) {
guac_flush(io);
close(fd);
io->fd = fd;
}
void guac_close(GUACIO* io) { void guac_close(GUACIO* io) {
guac_flush(io); guac_flush(io);
free(io); free(io);

View File

@@ -32,6 +32,7 @@
*/ */
typedef struct guac_client guac_client; typedef struct guac_client guac_client;
typedef struct guac_client_registry_node guac_client_registry_node;
typedef void guac_client_handle_messages(guac_client* client); 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); typedef void guac_client_mouse_handler(guac_client* client, int x, int y, int button_mask);
@@ -178,6 +179,7 @@ typedef void guac_client_init_handler(guac_client* client, const char* hostname,
* Initialize and return a new guac_client using the specified client init handler (guac_client_init_handler). * Initialize and return a new guac_client using the specified client init handler (guac_client_init_handler).
* This will normally be the guac_client_init function as provided by any of the pluggable proxy clients. * This will normally be the guac_client_init function as provided by any of the pluggable proxy clients.
* *
* @param registry The registry to use to register/find the client we need to return.
* @param client_fd The file descriptor associated with the socket associated with the connection to the * @param client_fd The file descriptor associated with the socket associated with the connection to the
* web-client tunnel. * web-client tunnel.
* @param client_init Function pointer to the client init handler which will initialize the new guac_client * @param client_init Function pointer to the client init handler which will initialize the new guac_client
@@ -186,7 +188,7 @@ typedef void guac_client_init_handler(guac_client* client, const char* hostname,
* @param port The port of the host that the proxy client should connect to. * @param port The port of the host that the proxy client should connect to.
* @return A pointer to the newly initialized client. * @return A pointer to the newly initialized client.
*/ */
guac_client* guac_get_client(int client_fd, guac_client_init_handler* client_init, const char* hostname, int port); guac_client* guac_get_client(int client_fd, guac_client_registry_node* registry, guac_client_init_handler* client_init, const char* hostname, int port);
/** /**
* Enter the main network message handling loop for the given client. * Enter the main network message handling loop for the given client.
@@ -199,8 +201,9 @@ void guac_start_client(guac_client* client);
* Free all resources associated with the given client. * Free all resources associated with the given client.
* *
* @param client The proxy client to free all reasources of. * @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); void guac_free_client(guac_client* client, guac_client_registry_node* registry);
/** /**
* Allocate a libpng-compatible buffer to hold raw image data. * Allocate a libpng-compatible buffer to hold raw image data.
@@ -221,4 +224,68 @@ png_byte** guac_alloc_png_buffer(int w, int h, int bpp);
*/ */
void guac_free_png_buffer(png_byte** png_buffer, int h); 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.
*/
struct guac_client_registry_node {
/**
* The number of pointers used inside the next array.
*/
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];
};
/**
* 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);
#endif #endif

View File

@@ -42,6 +42,7 @@ typedef struct GUACIO {
} GUACIO; } GUACIO;
GUACIO* guac_open(int fd); GUACIO* guac_open(int fd);
void guac_transfer(GUACIO* io, int fd);
ssize_t guac_write_int(GUACIO* io, unsigned int i); ssize_t guac_write_int(GUACIO* io, unsigned int i);
ssize_t guac_write_string(GUACIO* io, const char* str); ssize_t guac_write_string(GUACIO* io, const char* str);
ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count); ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count);

View File

@@ -30,6 +30,8 @@
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
guac_client_registry_node* registry;
/* Server */ /* Server */
int socket_fd; int socket_fd;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
@@ -76,6 +78,9 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "[guacamole] listening on port %i, forwarding to %s:%i\n", listen_port, connect_host, connect_port); fprintf(stderr, "[guacamole] listening on port %i, forwarding to %s:%i\n", listen_port, connect_host, connect_port);
/* Allocate registry */
registry = guac_create_client_registry();
/* Daemon loop */ /* Daemon loop */
for (;;) { for (;;) {
@@ -134,9 +139,9 @@ int main(int argc, char* argv[]) {
} }
/* Load and start client */ /* Load and start client */
client = guac_get_client(connected_socket_fd, alias.client_init, connect_host, connect_port); client = guac_get_client(connected_socket_fd, registry, alias.client_init, connect_host, connect_port);
guac_start_client(client); guac_start_client(client);
guac_free_client(client); guac_free_client(client, registry);
/* Close socket */ /* Close socket */
if (close(connected_socket_fd) < 0) { if (close(connected_socket_fd) < 0) {
@@ -156,6 +161,8 @@ int main(int argc, char* argv[]) {
} }
/* FIXME: Cleanup client registry (and all other objects) on exit */
/* Close socket */ /* Close socket */
if (close(socket_fd) < 0) { if (close(socket_fd) < 0) {
perror("Error closing socket"); perror("Error closing socket");