Merge commit 'origin/silc.1.1.branch'
[silc.git] / lib / silcapputil / silcidcache.c
1 /*
2
3   silcidcache.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2000 - 2008 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(NULL, 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(NULL, count, silc_hash_utf8_string,
87                                             NULL, silc_hash_utf8_compare, NULL,
88                                             NULL, NULL, TRUE);
89   cache->context_table = silc_hash_table_alloc(NULL, 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)
127     return NULL;
128   if (!name && !id && !context)
129     return NULL;
130
131   /* Allocate new cache entry */
132   c = silc_calloc(1, sizeof(*c));
133   if (!c)
134     return NULL;
135
136   c->id = id;
137   c->name = name;
138   c->context = context;
139
140   SILC_LOG_DEBUG(("Adding cache entry %p", c));
141
142   if (id) {
143     /* See if this entry is added already to cache */
144     if (silc_idcache_find_by_id_one(cache, id, NULL)) {
145       SILC_LOG_DEBUG(("Attempted to add same ID twice to ID Cache, id %s",
146                       silc_id_render(id, cache->id_type)));
147       goto err;
148     }
149   }
150
151   /* Add the new entry to the hash tables */
152   if (id)
153     if (!silc_hash_table_add(cache->id_table, id, c))
154       goto err;
155   if (name)
156     if (!silc_hash_table_add(cache->name_table, name, c))
157       goto err;
158   if (context)
159     if (!silc_hash_table_add(cache->context_table, context, c))
160       goto err;
161
162   return c;
163
164  err:
165   if (c->name)
166     silc_hash_table_del_by_context(cache->name_table, c->name, c);
167   if (c->context)
168     silc_hash_table_del_by_context(cache->context_table, c->context, c);
169   if (c->id)
170     silc_hash_table_del_by_context(cache->id_table, c->id, c);
171   silc_free(c);
172
173   return NULL;
174 }
175
176 /* Delete cache entry from cache. */
177
178 SilcBool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry entry,
179                           void *app_context)
180 {
181   SilcBool ret = FALSE;
182
183   if (!cache)
184     return FALSE;
185
186   SILC_LOG_DEBUG(("Deleting cache entry %p", entry));
187
188   if (entry->name)
189     ret = silc_hash_table_del_by_context(cache->name_table, entry->name,
190                                          entry);
191   if (entry->context)
192     ret = silc_hash_table_del_by_context(cache->context_table, entry->context,
193                                          entry);
194   if (entry->id)
195     ret = silc_hash_table_del_by_context(cache->id_table, entry->id,
196                                          entry);
197
198   if (ret)
199     silc_idcache_destructor(cache, entry, app_context);
200
201   return ret;
202 }
203
204 /* Deletes ID cache entry by ID. */
205
206 SilcBool silc_idcache_del_by_id(SilcIDCache cache, void *id,
207                                 void *app_context)
208 {
209   SilcIDCacheEntry c;
210
211   if (!cache)
212     return FALSE;
213
214   if (!silc_hash_table_find(cache->id_table, id, NULL, (void *)&c))
215     return FALSE;
216
217   return silc_idcache_del(cache, c, app_context);
218 }
219
220 /* Deletes ID cache entry by context. */
221
222 SilcBool silc_idcache_del_by_context(SilcIDCache cache, void *context,
223                                      void *app_context)
224 {
225   SilcIDCacheEntry c;
226
227   if (!cache)
228     return FALSE;
229
230   if (!silc_hash_table_find(cache->context_table, context, NULL, (void *)&c))
231     return FALSE;
232
233   return silc_idcache_del(cache, c, app_context);
234 }
235
236 /* Update entry */
237
238 SilcBool silc_idcache_update(SilcIDCache cache, SilcIDCacheEntry entry,
239                              void *new_id, char *new_name,
240                              SilcBool free_old_name)
241 {
242   if (!cache)
243     return FALSE;
244
245   if (new_id) {
246     if (entry->id) {
247       if (!silc_hash_table_del_by_context(cache->id_table, entry->id, entry))
248         return FALSE;
249
250       if (cache->id_type == SILC_ID_CLIENT)
251         *(SilcClientID *)entry->id = *(SilcClientID *)new_id;
252       if (cache->id_type == SILC_ID_SERVER)
253         *(SilcServerID *)entry->id = *(SilcServerID *)new_id;
254       if (cache->id_type == SILC_ID_CHANNEL)
255         *(SilcChannelID *)entry->id = *(SilcChannelID *)new_id;
256     } else {
257       entry->id = new_id;
258     }
259
260     if (!silc_hash_table_add(cache->id_table, entry->id, entry))
261       return FALSE;
262   }
263
264   if (new_name) {
265     if (entry->name)
266       if (!silc_hash_table_del_by_context(cache->name_table, entry->name,
267                                           entry))
268         return FALSE;
269
270     if (free_old_name)
271       silc_free(entry->name);
272     entry->name = new_name;
273
274     if (!silc_hash_table_add(cache->name_table, entry->name, entry))
275       return FALSE;
276   }
277
278   return TRUE;
279 }
280
281 /* Update entry by context */
282
283 SilcBool silc_idcache_update_by_context(SilcIDCache cache, void *context,
284                                         void *new_id, char *new_name,
285                                         SilcBool free_old_name)
286 {
287   SilcIDCacheEntry c;
288
289   if (!cache)
290     return FALSE;
291
292   if (!silc_hash_table_find(cache->context_table, context, NULL, (void *)&c))
293     return FALSE;
294
295   return silc_idcache_update(cache, c, new_id, new_name, free_old_name);
296 }
297
298 /* Move entry to another cache */
299
300 SilcBool silc_idcache_move(SilcIDCache from_cache, SilcIDCache to_cache,
301                            SilcIDCacheEntry entry)
302 {
303   SilcIDCacheEntry c;
304
305   SILC_LOG_DEBUG(("Moving entry %p from %p cache to %p cache", entry,
306                   from_cache, to_cache));
307
308   if (!from_cache || !to_cache || !entry)
309     return FALSE;
310
311   if (from_cache->id_type != to_cache->id_type) {
312     SILC_LOG_ERROR(("Incompatible ID caches, cannot move entry"));
313     return FALSE;
314   }
315
316   if (entry->context) {
317     if (!silc_hash_table_find(from_cache->context_table, entry->context,
318                               NULL, (void *)&c))
319       return FALSE;
320   } else if (entry->name) {
321     if (!silc_hash_table_find(from_cache->name_table, entry->name,
322                               NULL, (void *)&c))
323       return FALSE;
324   } else if (entry->id) {
325     if (!silc_hash_table_find(from_cache->id_table, entry->id,
326                               NULL, (void *)&c))
327       return FALSE;
328   } else {
329     return FALSE;
330   }
331
332   if (entry != c)
333     return FALSE;
334
335   /* See if this entry is added already to cache */
336   if (c->id && silc_idcache_find_by_id_one(to_cache, c->id, NULL)) {
337     SILC_LOG_ERROR(("Attempted to add same ID twice to ID Cache, id %s",
338                     silc_id_render(c->id, to_cache->id_type)));
339     SILC_ASSERT(FALSE);
340     return FALSE;
341   }
342
343   /* Remove from original cache */
344   if (c->name)
345     silc_hash_table_del_by_context(from_cache->name_table, c->name, c);
346   if (c->context)
347     silc_hash_table_del_by_context(from_cache->context_table, c->context, c);
348   if (c->id)
349     silc_hash_table_del_by_context(from_cache->id_table, c->id, c);
350
351   /* Move to the other cache */
352   if (c->id)
353     silc_hash_table_add(to_cache->id_table, c->id, c);
354   if (c->name)
355     silc_hash_table_add(to_cache->name_table, c->name, c);
356   if (c->context)
357     silc_hash_table_add(to_cache->context_table, c->context, c);
358
359   return TRUE;
360 }
361
362 /* Returns all cache entrys from the ID cache to the `ret' ID Cache List. */
363
364 SilcBool silc_idcache_get_all(SilcIDCache cache, SilcList *ret_list)
365 {
366   if (!cache || !ret_list)
367     return FALSE;
368
369   if (!silc_hash_table_count(cache->id_table))
370     return FALSE;
371
372   silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
373   silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach,
374                           ret_list);
375
376   if (!silc_list_count(*ret_list))
377     return FALSE;
378
379   return TRUE;
380 }
381
382 /* Find ID Cache entry by ID. May return multiple entries. */
383
384 SilcBool silc_idcache_find_by_id(SilcIDCache cache, void *id,
385                                  SilcList *ret_list)
386 {
387   if (!cache || !ret_list)
388     return FALSE;
389
390   if (!silc_hash_table_count(cache->id_table))
391     return FALSE;
392
393   silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
394   silc_hash_table_find_foreach(cache->id_table, id,
395                                silc_idcache_get_all_foreach, ret_list);
396
397   if (!silc_list_count(*ret_list))
398     return FALSE;
399
400   return TRUE;
401 }
402
403 /* Find one specific ID entry.  Compare full IDs */
404
405 SilcBool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
406                                      SilcIDCacheEntry *ret)
407 {
408   if (!cache)
409     return FALSE;
410   return silc_hash_table_find_ext(cache->id_table, id, NULL, (void *)ret,
411                                   NULL, NULL,
412                                   silc_hash_id_compare_full,
413                                   SILC_32_TO_PTR(cache->id_type));
414 }
415
416 /* Finds cache entry by context. */
417
418 SilcBool silc_idcache_find_by_context(SilcIDCache cache, void *context,
419                                       SilcIDCacheEntry *ret)
420 {
421   if (!cache)
422     return FALSE;
423   return silc_hash_table_find(cache->context_table, context, NULL,
424                               (void *)ret);
425 }
426
427 /* Find ID Cache entry by name. Returns list of cache entries. */
428
429 SilcBool silc_idcache_find_by_name(SilcIDCache cache, char *name,
430                                    SilcList *ret_list)
431 {
432   if (!cache || !ret_list)
433     return FALSE;
434
435   if (!silc_hash_table_count(cache->name_table))
436     return FALSE;
437
438   silc_list_init(*ret_list, struct SilcIDCacheEntryStruct, next);
439   silc_hash_table_find_foreach(cache->name_table, name,
440                                silc_idcache_get_all_foreach, ret_list);
441
442   if (!silc_list_count(*ret_list))
443     return FALSE;
444
445   return TRUE;
446 }
447
448 /* Find ID Cache entry by name. Returns one cache entry. */
449
450 SilcBool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
451                                        SilcIDCacheEntry *ret)
452 {
453   if (!cache)
454     return FALSE;
455   return silc_hash_table_find(cache->name_table, name, NULL, (void *)ret);
456 }