2014-07-05 20:14:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Glyptodon LLC
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "guac_list.h"
|
2016-03-01 05:40:39 +00:00
|
|
|
#include "proc.h"
|
|
|
|
#include "proc-map.h"
|
|
|
|
#include "user.h"
|
2014-07-05 20:14:48 +00:00
|
|
|
|
|
|
|
#include <guacamole/client.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a hash code based on the connection ID of the given client.
|
|
|
|
*/
|
|
|
|
static unsigned int __guacd_client_hash(const char* str) {
|
|
|
|
|
|
|
|
unsigned int hash_value = 0;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
/* Apply each character in string to the hash code */
|
|
|
|
while ((c = *(str++)))
|
|
|
|
hash_value = hash_value * 65599 + c;
|
|
|
|
|
|
|
|
return hash_value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-06 06:01:29 +00:00
|
|
|
/**
|
|
|
|
* Locates the bucket corresponding to the hash code indicated by the give id.
|
|
|
|
* Each bucket is an instance of guac_common_list.
|
|
|
|
*/
|
2016-03-01 05:40:39 +00:00
|
|
|
static guac_common_list* __guacd_proc_find_bucket(guacd_proc_map* map, const char* id) {
|
2014-07-05 20:14:48 +00:00
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
const int index = __guacd_client_hash(id) % GUACD_PROC_MAP_BUCKETS;
|
2014-07-05 20:14:48 +00:00
|
|
|
return map->__buckets[index];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-06 06:01:29 +00:00
|
|
|
/**
|
2016-03-01 05:40:39 +00:00
|
|
|
* Given a list of guacd_proc instances, returns the
|
|
|
|
* guacd_proc having the guac_client with the given ID, or NULL if no
|
|
|
|
* such client is stored.
|
2014-07-06 06:01:29 +00:00
|
|
|
*/
|
2016-03-01 05:40:39 +00:00
|
|
|
static guac_common_list_element* __guacd_proc_find(guac_common_list* bucket, const char* id) {
|
2014-07-05 20:14:48 +00:00
|
|
|
|
|
|
|
guac_common_list_element* current = bucket->head;
|
|
|
|
|
|
|
|
/* Search for matching element within bucket */
|
|
|
|
while (current != NULL) {
|
|
|
|
|
|
|
|
/* Check connection ID */
|
2016-03-01 05:40:39 +00:00
|
|
|
guacd_proc* proc = (guacd_proc*) current->data;
|
|
|
|
if (strcmp(proc->client->connection_id, id) == 0)
|
2014-07-05 20:14:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
guacd_proc_map* guacd_proc_map_alloc() {
|
2014-07-05 20:14:48 +00:00
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
guacd_proc_map* map = malloc(sizeof(guacd_proc_map));
|
2014-07-05 20:14:48 +00:00
|
|
|
guac_common_list** current;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Init all buckets */
|
|
|
|
current = map->__buckets;
|
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
for (i=0; i<GUACD_PROC_MAP_BUCKETS; i++) {
|
2014-07-05 20:14:48 +00:00
|
|
|
*current = guac_common_list_alloc();
|
|
|
|
current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
int guacd_proc_map_add(guacd_proc_map* map, guacd_proc* proc) {
|
2014-07-05 20:14:48 +00:00
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
const char* identifier = proc->client->connection_id;
|
|
|
|
guac_common_list* bucket = __guacd_proc_find_bucket(map, identifier);
|
2014-07-05 20:14:48 +00:00
|
|
|
guac_common_list_element* found;
|
|
|
|
|
|
|
|
/* Retrieve corresponding element, if any */
|
|
|
|
guac_common_list_lock(bucket);
|
2016-03-01 05:40:39 +00:00
|
|
|
found = __guacd_proc_find(bucket, identifier);
|
2014-07-05 20:14:48 +00:00
|
|
|
|
|
|
|
/* If no such element, we can add the new client successfully */
|
|
|
|
if (found == NULL) {
|
2016-03-01 05:40:39 +00:00
|
|
|
guac_common_list_add(bucket, proc);
|
2014-07-05 20:14:48 +00:00
|
|
|
guac_common_list_unlock(bucket);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, fail - already exists */
|
|
|
|
guac_common_list_unlock(bucket);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
guacd_proc* guacd_proc_map_retrieve(guacd_proc_map* map, const char* id) {
|
2014-07-05 20:14:48 +00:00
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
guacd_proc* proc;
|
2014-07-05 20:14:48 +00:00
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
guac_common_list* bucket = __guacd_proc_find_bucket(map, id);
|
2014-07-05 20:14:48 +00:00
|
|
|
guac_common_list_element* found;
|
|
|
|
|
|
|
|
/* Retrieve corresponding element, if any */
|
|
|
|
guac_common_list_lock(bucket);
|
2016-03-01 05:40:39 +00:00
|
|
|
found = __guacd_proc_find(bucket, id);
|
2014-07-05 20:14:48 +00:00
|
|
|
|
|
|
|
/* If no such element, fail */
|
|
|
|
if (found == NULL) {
|
|
|
|
guac_common_list_unlock(bucket);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
proc = (guacd_proc*) found->data;
|
2014-07-05 20:14:48 +00:00
|
|
|
|
|
|
|
guac_common_list_unlock(bucket);
|
2016-03-01 05:40:39 +00:00
|
|
|
return proc;
|
2014-07-05 20:14:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
guacd_proc* guacd_proc_map_remove(guacd_proc_map* map, const char* id) {
|
2014-07-05 20:14:48 +00:00
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
guacd_proc* proc;
|
2014-07-05 20:14:48 +00:00
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
guac_common_list* bucket = __guacd_proc_find_bucket(map, id);
|
2014-07-05 20:14:48 +00:00
|
|
|
guac_common_list_element* found;
|
|
|
|
|
|
|
|
/* Retrieve corresponding element, if any */
|
|
|
|
guac_common_list_lock(bucket);
|
2016-03-01 05:40:39 +00:00
|
|
|
found = __guacd_proc_find(bucket, id);
|
2014-07-05 20:14:48 +00:00
|
|
|
|
|
|
|
/* If no such element, fail */
|
|
|
|
if (found == NULL) {
|
|
|
|
guac_common_list_unlock(bucket);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
proc = (guacd_proc*) found->data;
|
2014-07-05 20:14:48 +00:00
|
|
|
guac_common_list_remove(bucket, found);
|
|
|
|
|
|
|
|
guac_common_list_unlock(bucket);
|
2016-03-01 05:40:39 +00:00
|
|
|
return proc;
|
2014-07-05 20:14:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|