5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2000 - 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
22 #include "silcidcache.h"
24 /************************** Types and definitions ***************************/
26 /* ID Cache context */
27 struct SilcIDCacheStruct {
28 SilcHashTable id_table; /* ID hash table */
29 SilcHashTable name_table; /* Name hash table */
30 SilcHashTable context_table; /* Context hash table */
31 SilcIDCacheDestructor destructor; /* Entry destructor */
32 void *context; /* Destructor context */
33 SilcIdType id_type; /* Type of ID cache */
37 /************************ Static utility functions **************************/
39 /* Callback that is called by the hash table routine when traversing
40 entries in the hash table. */
42 static void silc_idcache_get_all_foreach(void *key, void *context,
45 SilcList *list = user_context;
48 silc_list_add(*list, context);
51 /* Cache entry destructor */
53 static void silc_idcache_destructor(SilcIDCache cache,
54 SilcIDCacheEntry entry,
57 if (cache->destructor)
58 cache->destructor(cache, entry, cache->destructor, app_context);
60 memset(entry, 'F', sizeof(*entry));
65 /****************************** Public API **********************************/
67 /* Allocates new ID cache object. */
69 SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type,
70 SilcIDCacheDestructor destructor,
71 void *destructor_context)
75 SILC_LOG_DEBUG(("Allocating new cache"));
77 cache = silc_calloc(1, sizeof(*cache));
81 cache->id_table = silc_hash_table_alloc(count, silc_hash_id,
82 SILC_32_TO_PTR(id_type),
84 SILC_32_TO_PTR(id_type),
86 cache->name_table = silc_hash_table_alloc(count, silc_hash_utf8_string, NULL,
87 silc_hash_utf8_compare, NULL,
89 cache->context_table = silc_hash_table_alloc(count, silc_hash_ptr, NULL,
90 NULL, NULL, NULL, NULL, TRUE);
91 cache->destructor = destructor;
92 cache->context = destructor_context;
93 cache->id_type = id_type;
95 if (!cache->id_table || !cache->name_table || !cache->context_table) {
97 silc_hash_table_free(cache->id_table);
98 if (cache->name_table)
99 silc_hash_table_free(cache->name_table);
100 if (cache->context_table)
101 silc_hash_table_free(cache->context_table);
109 /* Frees ID cache object and cache entries */
111 void silc_idcache_free(SilcIDCache cache)
113 silc_hash_table_free(cache->id_table);
114 silc_hash_table_free(cache->name_table);
115 silc_hash_table_free(cache->context_table);
119 /* Add new entry to cache */
122 silc_idcache_add(SilcIDCache cache, char *name, void *id, void *context)
129 /* Allocate new cache entry */
130 c = silc_calloc(1, sizeof(*c));
136 c->context = context;
138 SILC_LOG_DEBUG(("Adding cache entry %p", c));
140 /* Add the new entry to the hash tables */
143 if (silc_idcache_find_by_id_one(cache, id, NULL)) {
144 SILC_LOG_ERROR(("Attempted to add same ID twice to ID Cache, id %s",
145 silc_id_render(id, cache->id_type)));
149 if (!silc_hash_table_add(cache->id_table, id, c))
153 if (!silc_hash_table_add(cache->name_table, name, c))
156 if (!silc_hash_table_add(cache->context_table, context, c))
163 silc_hash_table_del_by_context(cache->name_table, c->name, c);
165 silc_hash_table_del_by_context(cache->context_table, c->context, c);
167 silc_hash_table_del_by_context(cache->id_table, c->id, c);
173 /* Delete cache entry from cache. */
175 SilcBool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry entry,
178 SilcBool ret = FALSE;
180 SILC_LOG_DEBUG(("Deleting cache entry %p", entry));
183 ret = silc_hash_table_del_by_context(cache->name_table, entry->name,
186 ret = silc_hash_table_del_by_context(cache->context_table, entry->context,
189 ret = silc_hash_table_del_by_context(cache->id_table, entry->id,
193 silc_idcache_destructor(cache, entry, app_context);
198 /* Deletes ID cache entry by ID. */
200 SilcBool silc_idcache_del_by_id(SilcIDCache cache, void *id,
205 if (!silc_hash_table_find(cache->id_table, id, NULL, (void **)&c))
208 return silc_idcache_del(cache, c, app_context);
211 /* Deletes ID cache entry by context. */
213 SilcBool silc_idcache_del_by_context(SilcIDCache cache, void *context,
218 if (!silc_hash_table_find(cache->context_table, context, NULL, (void **)&c))
221 return silc_idcache_del(cache, c, app_context);
226 SilcBool silc_idcache_update(SilcIDCache cache, SilcIDCacheEntry entry,
227 void *new_id, char *new_name,
228 SilcBool free_old_name)
231 if (!silc_hash_table_del_by_context(cache->id_table, entry->id, entry))
234 if (cache->id_type == SILC_ID_CLIENT)
235 *(SilcClientID *)entry->id = *(SilcClientID *)new_id;
236 if (cache->id_type == SILC_ID_SERVER)
237 *(SilcServerID *)entry->id = *(SilcServerID *)new_id;
238 if (cache->id_type == SILC_ID_CHANNEL)
239 *(SilcChannelID *)entry->id = *(SilcChannelID *)new_id;
241 if (!silc_hash_table_add(cache->id_table, entry->id, entry))
246 if (!silc_hash_table_del_by_context(cache->name_table, entry->name, entry))
250 silc_free(entry->name);
251 entry->name = new_name;
253 if (!silc_hash_table_add(cache->name_table, entry->name, entry))
260 /* Update entry by context */
262 SilcBool silc_idcache_update_by_context(SilcIDCache cache, void *context,
263 void *new_id, char *new_name,
264 SilcBool free_old_name)
268 if (!silc_hash_table_find(cache->context_table, context, NULL, (void **)&c))
271 return silc_idcache_update(cache, c, new_id, new_name, free_old_name);
274 /* Returns all cache entrys from the ID cache to the `ret' ID Cache List. */
276 SilcBool silc_idcache_get_all(SilcIDCache cache, SilcList *ret_list)
281 if (!silc_hash_table_count(cache->id_table))
284 silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
285 silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach,
288 if (!silc_list_count(*ret_list))
294 /* Find ID Cache entry by ID. May return multiple entries. */
296 SilcBool silc_idcache_find_by_id(SilcIDCache cache, void *id,
302 if (!silc_hash_table_count(cache->id_table))
305 silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
306 silc_hash_table_find_foreach(cache->id_table, id,
307 silc_idcache_get_all_foreach, ret_list);
309 if (!silc_list_count(*ret_list))
315 /* Find one specific ID entry. Compare full IDs */
317 SilcBool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
318 SilcIDCacheEntry *ret)
320 return silc_hash_table_find_ext(cache->id_table, id, NULL, (void *)ret,
322 silc_hash_id_compare_full,
323 SILC_32_TO_PTR(cache->id_type));
326 /* Finds cache entry by context. */
328 SilcBool silc_idcache_find_by_context(SilcIDCache cache, void *context,
329 SilcIDCacheEntry *ret)
331 return silc_hash_table_find(cache->context_table, context, NULL,
335 /* Find ID Cache entry by name. Returns list of cache entries. */
337 SilcBool silc_idcache_find_by_name(SilcIDCache cache, char *name,
343 if (!silc_hash_table_count(cache->name_table))
346 silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
347 silc_hash_table_find_foreach(cache->name_table, name,
348 silc_idcache_get_all_foreach, ret_list);
350 if (!silc_list_count(*ret_list))
356 /* Find ID Cache entry by name. Returns one cache entry. */
358 SilcBool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
359 SilcIDCacheEntry *ret)
361 return silc_hash_table_find(cache->name_table, name, NULL, (void *)ret);