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