Sun Mar 11 15:22:42 CET 2007 Jochen Eisinger <coffee@silcnet.org>
[silc.git] / lib / silcapputil / silcidcache.c
1 /*
2
3   silcidcache.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2000 - 2007 Pekka Riikonen
8
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.
12
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.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22 #include "silcidcache.h"
23
24 /************************** Types and definitions ***************************/
25
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 */
34 };
35
36
37 /************************ Static utility functions **************************/
38
39 /* Callback that is called by the hash table routine when traversing
40    entries in the hash table. */
41
42 static void silc_idcache_get_all_foreach(void *key, void *context,
43                                          void *user_context)
44 {
45   SilcList *list = user_context;
46   if (!context)
47     return;
48   silc_list_add(*list, context);
49 }
50
51 /* Cache entry destructor */
52
53 static void silc_idcache_destructor(SilcIDCache cache,
54                                     SilcIDCacheEntry entry,
55                                     void *app_context)
56 {
57   if (cache->destructor)
58     cache->destructor(cache, entry, cache->destructor, app_context);
59
60   memset(entry, 'F', sizeof(*entry));
61   silc_free(entry);
62 }
63
64
65 /****************************** Public API **********************************/
66
67 /* Allocates new ID cache object. */
68
69 SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type,
70                                SilcIDCacheDestructor destructor,
71                                void *destructor_context)
72 {
73   SilcIDCache cache;
74
75   SILC_LOG_DEBUG(("Allocating new cache"));
76
77   cache = silc_calloc(1, sizeof(*cache));
78   if (!cache)
79     return NULL;
80
81   cache->id_table = silc_hash_table_alloc(count, silc_hash_id,
82                                           SILC_32_TO_PTR(id_type),
83                                           silc_hash_id_compare,
84                                           SILC_32_TO_PTR(id_type),
85                                           NULL, NULL, TRUE);
86   cache->name_table = silc_hash_table_alloc(count, silc_hash_utf8_string, NULL,
87                                             silc_hash_utf8_compare, NULL,
88                                             NULL, NULL, TRUE);
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;
94
95   if (!cache->id_table || !cache->name_table || !cache->context_table) {
96     if (cache->id_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);
102     silc_free(cache);
103     return NULL;
104   }
105
106   return cache;
107 }
108
109 /* Frees ID cache object and cache entries */
110
111 void silc_idcache_free(SilcIDCache cache)
112 {
113   silc_hash_table_free(cache->id_table);
114   silc_hash_table_free(cache->name_table);
115   silc_hash_table_free(cache->context_table);
116   silc_free(cache);
117 }
118
119 /* Add new entry to cache */
120
121 SilcIDCacheEntry
122 silc_idcache_add(SilcIDCache cache, char *name, void *id, void *context)
123 {
124   SilcIDCacheEntry c;
125
126   if (!cache || !id)
127     return NULL;
128
129   /* Allocate new cache entry */
130   c = silc_calloc(1, sizeof(*c));
131   if (!c)
132     return NULL;
133
134   c->id = id;
135   c->name = name;
136   c->context = context;
137
138   SILC_LOG_DEBUG(("Adding cache entry %p", c));
139
140   /* Add the new entry to the hash tables */
141   if (silc_idcache_find_by_id_one(cache, id, NULL)) {
142     SILC_LOG_ERROR(("Attempted to add same ID twice to ID Cache, id %s",
143                    silc_id_render(id, cache->id_type)));
144     SILC_ASSERT(FALSE);
145     goto err;
146   }
147   if (!silc_hash_table_add(cache->id_table, id, c))
148     goto err;
149   if (name)
150     if (!silc_hash_table_add(cache->name_table, name, c))
151       goto err;
152   if (context)
153     if (!silc_hash_table_add(cache->context_table, context, c))
154       goto err;
155
156   return c;
157
158  err:
159   if (c->name)
160     silc_hash_table_del_by_context(cache->name_table, c->name, c);
161   if (c->context)
162     silc_hash_table_del_by_context(cache->context_table, c->context, c);
163   if (c->id)
164     silc_hash_table_del_by_context(cache->id_table, c->id, c);
165   silc_free(c);
166
167   return NULL;
168 }
169
170 /* Delete cache entry from cache. */
171
172 SilcBool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry entry,
173                           void *app_context)
174 {
175   SilcBool ret = FALSE;
176
177   if (!cache)
178     return FALSE;
179
180   SILC_LOG_DEBUG(("Deleting cache entry %p", entry));
181
182   if (entry->name)
183     ret = silc_hash_table_del_by_context(cache->name_table, entry->name,
184                                          entry);
185   if (entry->context)
186     ret = silc_hash_table_del_by_context(cache->context_table, entry->context,
187                                          entry);
188   if (entry->id)
189     ret = silc_hash_table_del_by_context(cache->id_table, entry->id,
190                                          entry);
191
192   if (ret)
193     silc_idcache_destructor(cache, entry, app_context);
194
195   return ret;
196 }
197
198 /* Deletes ID cache entry by ID. */
199
200 SilcBool silc_idcache_del_by_id(SilcIDCache cache, void *id,
201                                 void *app_context)
202 {
203   SilcIDCacheEntry c;
204
205   if (!cache)
206     return FALSE;
207
208   if (!silc_hash_table_find(cache->id_table, id, NULL, (void *)&c))
209     return FALSE;
210
211   return silc_idcache_del(cache, c, app_context);
212 }
213
214 /* Deletes ID cache entry by context. */
215
216 SilcBool silc_idcache_del_by_context(SilcIDCache cache, void *context,
217                                      void *app_context)
218 {
219   SilcIDCacheEntry c;
220
221   if (!cache)
222     return FALSE;
223
224   if (!silc_hash_table_find(cache->context_table, context, NULL, (void *)&c))
225     return FALSE;
226
227   return silc_idcache_del(cache, c, app_context);
228 }
229
230 /* Update entry */
231
232 SilcBool silc_idcache_update(SilcIDCache cache, SilcIDCacheEntry entry,
233                              void *new_id, char *new_name,
234                              SilcBool free_old_name)
235 {
236   if (!cache)
237     return FALSE;
238
239   if (new_id) {
240     if (!silc_hash_table_del_by_context(cache->id_table, entry->id, entry))
241       return FALSE;
242
243     if (cache->id_type == SILC_ID_CLIENT)
244       *(SilcClientID *)entry->id = *(SilcClientID *)new_id;
245     if (cache->id_type == SILC_ID_SERVER)
246       *(SilcServerID *)entry->id = *(SilcServerID *)new_id;
247     if (cache->id_type == SILC_ID_CHANNEL)
248       *(SilcChannelID *)entry->id = *(SilcChannelID *)new_id;
249
250     if (!silc_hash_table_add(cache->id_table, entry->id, entry))
251       return FALSE;
252   }
253
254   if (new_name) {
255     if (entry->name)
256       if (!silc_hash_table_del_by_context(cache->name_table, entry->name,
257                                           entry))
258         return FALSE;
259
260     if (free_old_name)
261       silc_free(entry->name);
262     entry->name = new_name;
263
264     if (!silc_hash_table_add(cache->name_table, entry->name, entry))
265       return FALSE;
266   }
267
268   return TRUE;
269 }
270
271 /* Update entry by context */
272
273 SilcBool silc_idcache_update_by_context(SilcIDCache cache, void *context,
274                                         void *new_id, char *new_name,
275                                         SilcBool free_old_name)
276 {
277   SilcIDCacheEntry c;
278
279   if (!cache)
280     return FALSE;
281
282   if (!silc_hash_table_find(cache->context_table, context, NULL, (void *)&c))
283     return FALSE;
284
285   return silc_idcache_update(cache, c, new_id, new_name, free_old_name);
286 }
287
288 /* Returns all cache entrys from the ID cache to the `ret' ID Cache List. */
289
290 SilcBool silc_idcache_get_all(SilcIDCache cache, SilcList *ret_list)
291 {
292   if (!cache || !ret_list)
293     return FALSE;
294
295   if (!silc_hash_table_count(cache->id_table))
296     return FALSE;
297
298   silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
299   silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach,
300                           ret_list);
301
302   if (!silc_list_count(*ret_list))
303     return FALSE;
304
305   return TRUE;
306 }
307
308 /* Find ID Cache entry by ID. May return multiple entries. */
309
310 SilcBool silc_idcache_find_by_id(SilcIDCache cache, void *id,
311                                  SilcList *ret_list)
312 {
313   if (!cache || !ret_list)
314     return FALSE;
315
316   if (!silc_hash_table_count(cache->id_table))
317     return FALSE;
318
319   silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
320   silc_hash_table_find_foreach(cache->id_table, id,
321                                silc_idcache_get_all_foreach, ret_list);
322
323   if (!silc_list_count(*ret_list))
324     return FALSE;
325
326   return TRUE;
327 }
328
329 /* Find one specific ID entry.  Compare full IDs */
330
331 SilcBool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
332                                      SilcIDCacheEntry *ret)
333 {
334   if (!cache)
335     return FALSE;
336   return silc_hash_table_find_ext(cache->id_table, id, NULL, (void *)ret,
337                                   NULL, NULL,
338                                   silc_hash_id_compare_full,
339                                   SILC_32_TO_PTR(cache->id_type));
340 }
341
342 /* Finds cache entry by context. */
343
344 SilcBool silc_idcache_find_by_context(SilcIDCache cache, void *context,
345                                       SilcIDCacheEntry *ret)
346 {
347   if (!cache)
348     return FALSE;
349   return silc_hash_table_find(cache->context_table, context, NULL,
350                               (void *)ret);
351 }
352
353 /* Find ID Cache entry by name. Returns list of cache entries. */
354
355 SilcBool silc_idcache_find_by_name(SilcIDCache cache, char *name,
356                                    SilcList *ret_list)
357 {
358   if (!cache || !ret_list)
359     return FALSE;
360
361   if (!silc_hash_table_count(cache->name_table))
362     return FALSE;
363
364   silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
365   silc_hash_table_find_foreach(cache->name_table, name,
366                                silc_idcache_get_all_foreach, ret_list);
367
368   if (!silc_list_count(*ret_list))
369     return FALSE;
370
371   return TRUE;
372 }
373
374 /* Find ID Cache entry by name. Returns one cache entry. */
375
376 SilcBool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
377                                        SilcIDCacheEntry *ret)
378 {
379   if (!cache)
380     return FALSE;
381   return silc_hash_table_find(cache->name_table, name, NULL, (void *)ret);
382 }