Merged silc_1_0_branch to trunk.
[silc.git] / lib / silccore / silcidcache.h
1 /*
2
3   silcidcache.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2000 - 2005 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; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 /****h* silccore/SILC ID Cache Interface
22  *
23  * DESCRIPTION
24  *
25  * SILC ID Cache is an cache for all kinds of ID's used in the SILC
26  * protocol.  Application can save here the ID's it uses and the interface
27  * provides fast retrieval of the ID's from the cache.
28  *
29  ***/
30
31 #ifndef SILCIDCACHE_H
32 #define SILCIDCACHE_H
33
34 /****s* silccore/SilcIDCacheAPI/SilcIDCacheEntry
35  *
36  * NAME
37  *
38  *    typedef struct { ... } SilcIDCacheEntry;
39  *
40  * DESCRIPTION
41  *
42  *    This is one entry in the SILC ID Cache system. Contents of this is
43  *    allocated outside the ID cache system, however, all the fields are
44  *    filled with ID cache utility functions. The ID cache system does not
45  *    allocate any of these fields nor free them.
46  *
47  *    void *id
48  *
49  *      The actual ID.
50  *
51  *    char name
52  *
53  *      A name associated with the ID.
54  *
55  *    SilcUInt32 expire
56  *
57  *      Time when this cache entry expires.  This is normal time() value
58  *      plus the validity.  Cache entry has expired if current time is
59  *      more than value in this field.  If this value is zero (0) the
60  *      entry never expires.
61  *
62  *    void *context
63  *
64  *      Any caller specified context.
65  *
66  * SOURCE
67  */
68 typedef struct {
69   void *id;
70   char *name;
71   SilcUInt32 expire;
72   void *context;
73 } *SilcIDCacheEntry;
74 /***/
75
76 /****s* silccore/SilcIDCacheAPI/SilcIDCache
77  *
78  * NAME
79  *
80  *    typedef struct SilcIDCacheStruct *SilcIDCache;
81  *
82  * DESCRIPTION
83  *
84  *    This context is the actual ID Cache and is allocated by
85  *    silc_idcache_alloc and given as argument usually to all
86  *    silc_idcache_* functions.  It is freed by the
87  *    silc_idcache_free function.
88  *
89  ***/
90 typedef struct SilcIDCacheStruct *SilcIDCache;
91
92 /****s* silccore/SilcIDCacheAPI/SilcIDCacheList
93  *
94  * NAME
95  *
96  *    typedef struct SilcIDCacheListStruct *SilcIDCacheList;
97  *
98  * DESCRIPTION
99  *
100  *    This context is the ID Cache List and is allocated by
101  *    some of the silc_idcache_* functions. Functions that may return
102  *    multiple entries from the cache allocate the entries in to the
103  *    SilcIDCacheList. The context is freed by silc_idcache_list_free
104  *    function.
105  *
106  ***/
107 typedef struct SilcIDCacheListStruct *SilcIDCacheList;
108
109 /****f* silccore/SilcIDCacheAPI/SilcIDCacheDestructor
110  *
111  * SYNOPSIS
112  *
113  *    typedef void (*SilcIDCacheDestructor)(SilcIDCache cache,
114  *                                          SilcIDCacheEntry entry);
115  *
116  * DESCRIPTION
117  *
118  *    Destructor callback that is called when an cache entry expires or is
119  *    purged from the ID cache. The application must not free cache entry
120  *    because the library will do it automatically. The appliation, however,
121  *    is responsible of freeing any data in the entry.
122  *
123  ***/
124 typedef void (*SilcIDCacheDestructor)(SilcIDCache cache,
125                                       SilcIDCacheEntry entry);
126
127 #define SILC_ID_CACHE_EXPIRE 3600
128 #define SILC_ID_CACHE_EXPIRE_DEF (time(NULL) + SILC_ID_CACHE_EXPIRE)
129
130 /* Prototypes */
131
132 /****f* silccore/SilcIDCacheAPI/silc_idcache_alloc
133  *
134  * SYNOPSIS
135  *
136  *    SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type,
137  *                                   SilcIDCacheDestructor destructor);
138  *
139  * DESCRIPTION
140  *
141  *    Allocates new ID cache object. The initial amount of allocated entries
142  *    can be sent as argument. If `count' is 0 the system uses default values.
143  *    The `id_type' defines the types of the ID's that will be saved to the
144  *    cache.
145  *
146  *    If 'delete_id' is TRUE then library will free the ID when a
147  *    cache entry is deleted.  If 'delete_name' is TRUE then library
148  *    will delete the associated name when a cache entry is deleted.
149  *
150  ***/
151 SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type,
152                                SilcIDCacheDestructor destructor,
153                                bool delete_id, bool delete_name);
154
155 /****f* silccore/SilcIDCacheAPI/silc_idcache_free
156  *
157  * SYNOPSIS
158  *
159  *    void silc_idcache_free(SilcIDCache cache);
160  *
161  * DESCRIPTION
162  *
163  *    Frees ID cache object and all cache entries.
164  *
165  ***/
166 void silc_idcache_free(SilcIDCache cache);
167
168 /****f* silccore/SilcIDCacheAPI/silc_idcache_add
169  *
170  * SYNOPSIS
171  *
172  *    bool silc_idcache_add(SilcIDCache cache, char *name, void *id,
173  *                          void *context, int expire, SilcIDCacheEntry *ret);
174  *
175  * DESCRIPTION
176  *
177  *    Add new entry to the cache. Returns TRUE if the entry was added and
178  *    FALSE if it could not be added. The `name' is the name associated with
179  *    the ID, the `id' the actual ID and the `context' a user specific context.
180  *    If the `expire' is non-zero the entry expires in that specified time.
181  *    If zero the entry never expires from the cache.
182  *
183  *    The `name', `id' and `context' pointers will be saved in the cache,
184  *    and if the caller frees these pointers the caller is also responsible
185  *    of deleting the cache entry.  Otherwise the cache will have the freed
186  *    pointers stored.
187  *
188  *    If the `ret' is non-NULL the created ID Cache entry is returned to
189  *    that pointer.
190  *
191  ***/
192 bool silc_idcache_add(SilcIDCache cache, char *name, void *id,
193                       void *context, int expire, SilcIDCacheEntry *ret);
194
195 /****f* silccore/SilcIDCacheAPI/silc_idcache_del
196  *
197  * SYNOPSIS
198  *
199  *    bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old);
200  *
201  * DESCRIPTION
202  *
203  *    Delete cache entry from cache. Returns TRUE if the entry was deleted.
204  *    The destructor function is not called.
205  *
206  ***/
207 bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old);
208
209 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_id
210  *
211  * SYNOPSIS
212  *
213  *    bool silc_idcache_del_by_id(SilcIDCache cache, void *id);
214  *
215  * DESCRIPTION
216  *
217  *    Delete cache entry by ID. Returns TRUE if the entry was deleted.
218  *    The destructor function is not called.
219  *
220  ***/
221 bool silc_idcache_del_by_id(SilcIDCache cache, void *id);
222
223 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_id_ext
224  *
225  * SYNOPSIS
226  *
227  *    bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
228  *                                    SilcHashFunction hash,
229  *                                    void *hash_context,
230  *                                    SilcHashCompare compare,
231  *                                    void *compare_context);
232  *
233  * DESCRIPTION
234  *
235  *    Same as silc_idcache_del_by_id but with specific hash and comparison
236  *    functions. If the functions are NULL then default values are used.
237  *    Returns TRUE if the entry was deleted. The destructor function is
238  *    called.
239  *
240  ***/
241 bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
242                                 SilcHashFunction hash,
243                                 void *hash_context,
244                                 SilcHashCompare compare,
245                                 void *compare_context);
246
247 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_context
248  *
249  * SYNOPSIS
250  *
251  *    bool silc_idcache_del_by_context(SilcIDCache cache, void *context);
252  *
253  * DESCRIPTION
254  *
255  *    Deletes cachen entry by the user specified context. Returns TRUE
256  *    if the entry was deleted. The destructor function is not called.
257  *
258  ***/
259 bool silc_idcache_del_by_context(SilcIDCache cache, void *context);
260
261 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_all
262  *
263  * SYNOPSIS
264  *
265  *    bool silc_idcache_del_all(SilcIDCache cache);
266  *
267  * DESCRIPTION
268  *
269  *    Deletes all cache entries from the cache and frees all memory.
270  *    The destructor function is not called.
271  *
272  ***/
273 bool silc_idcache_del_all(SilcIDCache cache);
274
275 /****f* silccore/SilcIDCacheAPI/silc_idcache_purge
276  *
277  * SYNOPSIS
278  *
279  *    bool silc_idcache_purge(SilcIDCache cache);
280  *
281  * DESCRIPTION
282  *
283  *    Purges the cache by removing expired cache entires. Note that this
284  *    may be very slow operation. Returns TRUE if the purging was successful.
285  *    The destructor function is called for each purged cache entry.
286  *
287  ***/
288 bool silc_idcache_purge(SilcIDCache cache);
289
290 /****f* silccore/SilcIDCacheAPI/silc_idcache_by_context
291  *
292  * SYNOPSIS
293  *
294  *    bool silc_idcache_purge_by_context(SilcIDCache cache, void *context);
295  *
296  * DESCRIPTION
297  *
298  *    Purges the cache by context and removes expired cache entires.
299  *    Returns TRUE if the puring was successful. The destructor function
300  *    is called for the purged cache entry.
301  *
302  ***/
303 bool silc_idcache_purge_by_context(SilcIDCache cache, void *context);
304
305 /****f* silccore/SilcIDCacheAPI/silc_idcache_get_all
306  *
307  * SYNOPSIS
308  *
309  *    bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret);
310  *
311  * DESCRIPTION
312  *
313  *    Returns all cache entries from the ID cache to the `ret' SilcIDCacheList.
314  *    Returns TRUE if the retrieval was successful. The caller must free
315  *    the returned SilcIDCacheList.
316  *
317  ***/
318 bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret);
319
320 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id
321  *
322  * SYNOPSIS
323  *
324  *    bool silc_idcache_find_by_id(SilcIDCache cache, void *id,
325  *                                 SilcIDCacheList *ret);
326  *
327  * DESCRIPTION
328  *
329  *    Find ID Cache entry by ID. This may return multiple entry and the
330  *    `ret' SilcIDCacheList is allocated. Returns TRUE if the entry was
331  *    found. The caller must free the returned SilcIDCacheList.
332  *
333  ***/
334 bool silc_idcache_find_by_id(SilcIDCache cache, void *id,
335                              SilcIDCacheList *ret);
336
337 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id_one
338  *
339  * SYNOPSIS
340  *
341  *     bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
342  *                                      SilcIDCacheEntry *ret);
343  *
344  * DESCRIPTION
345  *
346  *    Find ID Cache entry by ID. Returns only one entry from the cache
347  *    and the found entry is considered to be exact match. Returns TRUE
348  *    if the entry was found.
349  *
350  ***/
351 bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
352                                  SilcIDCacheEntry *ret);
353
354 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id_one_ext
355  *
356  * SYNOPSIS
357  *
358  *    bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id,
359  *                                         SilcHashFunction hash,
360  *                                         void *hash_context,
361  *                                         SilcHashCompare compare,
362  *                                         void *compare_context,
363  *                                         SilcIDCacheEntry *ret);
364  *
365  * DESCRIPTION
366  *
367  *    Same as silc_idcache_find_by_id_one but with specific hash and
368  *    comparison functions. If `hash' is NULL then the default hash
369  *    funtion is used and if `compare' is NULL default comparison function
370  *    is used. Returns TRUE if the entry was found.
371  *
372  ***/
373 bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id,
374                                      SilcHashFunction hash,
375                                      void *hash_context,
376                                      SilcHashCompare compare,
377                                      void *compare_context,
378                                      SilcIDCacheEntry *ret);
379
380 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_context
381  *
382  * SYNOPSIS
383  *
384  *    bool silc_idcache_find_by_context(SilcIDCache cache, void *context,
385  *                                      SilcIDCacheEntry *ret);
386  *
387  * DESCRIPTION
388  *
389  *    Find cache entry by user specified context. Returns TRUE if the
390  *    entry was found.
391  *
392  ***/
393 bool silc_idcache_find_by_context(SilcIDCache cache, void *context,
394                                   SilcIDCacheEntry *ret);
395
396 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_name
397  *
398  * SYNOPSIS
399  *
400  *    bool silc_idcache_find_by_name(SilcIDCache cache, char *name,
401  *                                   SilcIDCacheList *ret);
402  *
403  * DESCRIPTION
404  *
405  *    Find cache entries by the name associated with the ID. This may
406  *    return muliptle entries allocated to the SilcIDCacheList. Returns
407  *    TRUE if the entry was found. The caller must free the SIlcIDCacheList.
408  *
409  ***/
410 bool silc_idcache_find_by_name(SilcIDCache cache, char *name,
411                                SilcIDCacheList *ret);
412
413 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_name_one
414  *
415  * SYNOPSIS
416  *
417  *    bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
418  *                                       SilcIDCacheEntry *ret);
419  *
420  * DESCRIPTION
421  *
422  *    Find cache entry by the name associated with the ID. This returns
423  *    one entry and the found entry is considered to be exact match.
424  *    return muliptle entries allocated to the SilcIDCacheList. Returns
425  *    TRUE if the entry was found.
426  *
427  ***/
428 bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
429                                    SilcIDCacheEntry *ret);
430
431 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_count
432  *
433  * SYNOPSIS
434  *
435  *    int silc_idcache_list_count(SilcIDCacheList list);
436  *
437  * DESCRIPTION
438  *
439  *    Returns the number of cache entries in the ID cache list.
440  *
441  ***/
442 int silc_idcache_list_count(SilcIDCacheList list);
443
444 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_first
445  *
446  * SYNOPSIS
447  *
448  *    bool silc_idcache_list_first(SilcIDCacheList list,
449  *                                 SilcIDCacheEntry *ret);
450  *
451  * DESCRIPTION
452  *
453  *    Returns the first cache entry from the ID cache list. Returns FALSE
454  *    If the entry could not be retrieved.
455  *
456  ***/
457 bool silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret);
458
459 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_next
460  *
461  * SYNOPSIS
462  *
463  *    bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret);
464  *
465  * DESCRIPTION
466  *
467  *    Returns the next cache entry from the ID Cache list. Returns FALSE
468  *    when there are not anymore entries in the list.
469  *
470  ***/
471 bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret);
472
473 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_free
474  *
475  * SYNOPSIS
476  *
477  *    void silc_idcache_list_free(SilcIDCacheList list);
478  *
479  * DESCRIPTION
480  *
481  *     Frees ID cache list. User must free the list context returned by
482  *     any of the searching functions.
483  *
484  ***/
485 void silc_idcache_list_free(SilcIDCacheList list);
486
487 #endif