cadda8c0cfdd561f5ce28667f96a8f01b5d5b40c
[silc.git] / lib / silccore / silcidcache.h
1 /*
2  
3   silcidcache.h
4  
5   Author: Pekka Riikonen <priikone@silcnet.org>
6  
7   Copyright (C) 2000 - 2001 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/SilcIDCacheAPI
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  *    uint32 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   uint32 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(uint32 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  ***/
147 SilcIDCache silc_idcache_alloc(uint32 count, SilcIdType id_type,
148                                SilcIDCacheDestructor destructor);
149
150 /****f* silccore/SilcIDCacheAPI/silc_idcache_free
151  *
152  * SYNOPSIS
153  *
154  *    void silc_idcache_free(SilcIDCache cache);
155  *
156  * DESCRIPTION
157  *
158  *    Frees ID cache object and all cache entries.
159  *
160  ***/
161 void silc_idcache_free(SilcIDCache cache);
162
163 /****f* silccore/SilcIDCacheAPI/silc_idcache_add
164  *
165  * SYNOPSIS
166  *
167  *    bool silc_idcache_add(SilcIDCache cache, char *name, void *id, 
168  *                          void *context, int expire);
169  *
170  * DESCRIPTION
171  *
172  *    Add new entry to the cache. Returns TRUE if the entry was added and
173  *    FALSE if it could not be added. The `name' is the name associated with
174  *    the ID, the `id' the actual ID and the `context' a used specific context.
175  *    If the `expire' is TRUE the entry expires in default time and if FALSE
176  *    the entry never expires from the cache.
177  *
178  ***/
179 bool silc_idcache_add(SilcIDCache cache, char *name, void *id, 
180                       void *context, int expire);
181
182 /****f* silccore/SilcIDCacheAPI/silc_idcache_del
183  *
184  * SYNOPSIS
185  *
186  *    bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old);
187  *
188  * DESCRIPTION
189  *
190  *    Delete cache entry from cache. Returns TRUE if the entry was deleted.
191  *    The destructor function is not called.
192  *
193  ***/
194 bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old);
195
196 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_id
197  *
198  * SYNOPSIS
199  *
200  *    bool silc_idcache_del_by_id(SilcIDCache cache, void *id);
201  *
202  * DESCRIPTION
203  *
204  *    Delete cache entry by ID. Returns TRUE if the entry was deleted.
205  *    The destructor function is not called.
206  *
207  ***/
208 bool silc_idcache_del_by_id(SilcIDCache cache, void *id);
209
210 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_id_ext
211  *
212  * SYNOPSIS
213  *
214  *    bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
215  *                                    SilcHashFunction hash, 
216  *                                    void *hash_context,
217  *                                    SilcHashCompare compare, 
218  *                                    void *compare_context);
219  *
220  * DESCRIPTION
221  *
222  *    Same as silc_idcache_del_by_id but with specific hash and comparison
223  *    functions. If the functions are NULL then default values are used.
224  *    Returns TRUE if the entry was deleted. The destructor function is
225  *    not called.
226  *
227  ***/
228 bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
229                                 SilcHashFunction hash, 
230                                 void *hash_context,
231                                 SilcHashCompare compare, 
232                                 void *compare_context);
233
234 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_context
235  *
236  * SYNOPSIS
237  *
238  *    bool silc_idcache_del_by_context(SilcIDCache cache, void *context);
239  *
240  * DESCRIPTION
241  *
242  *    Deletes cachen entry by the user specified context. Returns TRUE
243  *    if the entry was deleted. The destructor function is not called.
244  *
245  ***/
246 bool silc_idcache_del_by_context(SilcIDCache cache, void *context);
247
248 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_all
249  *
250  * SYNOPSIS
251  *
252  *    bool silc_idcache_del_all(SilcIDCache cache);
253  *
254  * DESCRIPTION
255  *
256  *    Deletes all cache entries from the cache and frees all memory.
257  *    The destructor function is not called.
258  *
259  ***/
260 bool silc_idcache_del_all(SilcIDCache cache);
261
262 /****f* silccore/SilcIDCacheAPI/silc_idcache_purge
263  *
264  * SYNOPSIS
265  *
266  *    bool silc_idcache_purge(SilcIDCache cache);
267  *
268  * DESCRIPTION
269  *
270  *    Purges the cache by removing expired cache entires. Note that this
271  *    may be very slow operation. Returns TRUE if the purging was successful.
272  *    The destructor function is called for each purged cache entry.
273  *
274  ***/
275 bool silc_idcache_purge(SilcIDCache cache);
276
277 /****f* silccore/SilcIDCacheAPI/silc_idcache_by_context
278  *
279  * SYNOPSIS
280  *
281  *    bool silc_idcache_purge_by_context(SilcIDCache cache, void *context);
282  *
283  * DESCRIPTION
284  *
285  *    Purges the cache by context and removes expired cache entires. 
286  *    Returns TRUE if the puring was successful. The destructor function
287  *    is called for the purged cache entry.
288  *
289  ***/
290 bool silc_idcache_purge_by_context(SilcIDCache cache, void *context);
291
292 /****f* silccore/SilcIDCacheAPI/silc_idcache_get_all
293  *
294  * SYNOPSIS
295  *
296  *    bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret);
297  *
298  * DESCRIPTION
299  *
300  *    Returns all cache entries from the ID cache to the `ret' SilcIDCacheList.
301  *    Returns TRUE if the retrieval was successful. The caller must free
302  *    the returned SilcIDCacheList.
303  *
304  ***/
305 bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret);
306
307 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id
308  *
309  * SYNOPSIS
310  *
311  *    bool silc_idcache_find_by_id(SilcIDCache cache, void *id, 
312  *                                 SilcIDCacheList *ret);
313  *
314  * DESCRIPTION
315  *
316  *    Find ID Cache entry by ID. This may return multiple entry and the
317  *    `ret' SilcIDCacheList is allocated. Returns TRUE if the entry was
318  *    found. The caller must free the returned SilcIDCacheList.
319  *
320  ***/
321 bool silc_idcache_find_by_id(SilcIDCache cache, void *id, 
322                              SilcIDCacheList *ret);
323
324 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id_one
325  *
326  * SYNOPSIS
327  *
328  *     bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id, 
329  *                                      SilcIDCacheEntry *ret);
330  *
331  * DESCRIPTION
332  *
333  *    Find ID Cache entry by ID. Returns only one entry from the cache
334  *    and the found entry is considered to be exact match. Returns TRUE
335  *    if the entry was found.
336  *
337  ***/
338 bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id, 
339                                  SilcIDCacheEntry *ret);
340
341 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id_one_ext
342  *
343  * SYNOPSIS
344  *
345  *    bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id, 
346  *                                         SilcHashFunction hash, 
347  *                                         void *hash_context,
348  *                                         SilcHashCompare compare, 
349  *                                         void *compare_context,
350  *                                         SilcIDCacheEntry *ret);
351  *
352  * DESCRIPTION
353  *
354  *    Same as silc_idcache_find_by_id_one but with specific hash and
355  *    comparison functions. If `hash' is NULL then the default hash
356  *    funtion is used and if `compare' is NULL default comparison function
357  *    is used. Returns TRUE if the entry was found.
358  *
359  ***/
360 bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id, 
361                                      SilcHashFunction hash, 
362                                      void *hash_context,
363                                      SilcHashCompare compare, 
364                                      void *compare_context,
365                                      SilcIDCacheEntry *ret);
366
367 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_context
368  *
369  * SYNOPSIS
370  *
371  *    bool silc_idcache_find_by_context(SilcIDCache cache, void *context, 
372  *                                      SilcIDCacheEntry *ret);
373  *
374  * DESCRIPTION
375  *
376  *    Find cache entry by user specified context. Returns TRUE if the
377  *    entry was found.
378  *
379  ***/
380 bool silc_idcache_find_by_context(SilcIDCache cache, void *context, 
381                                   SilcIDCacheEntry *ret);
382
383 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_name
384  *
385  * SYNOPSIS
386  *
387  *    bool silc_idcache_find_by_name(SilcIDCache cache, char *name, 
388  *                                   SilcIDCacheList *ret);
389  *
390  * DESCRIPTION
391  *
392  *    Find cache entries by the name associated with the ID. This may
393  *    return muliptle entries allocated to the SilcIDCacheList. Returns
394  *    TRUE if the entry was found. The caller must free the SIlcIDCacheList.
395  *
396  ***/
397 bool silc_idcache_find_by_name(SilcIDCache cache, char *name, 
398                                SilcIDCacheList *ret);
399
400 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_name_one
401  *
402  * SYNOPSIS
403  *
404  *    bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
405  *                                       SilcIDCacheEntry *ret);
406  *
407  * DESCRIPTION
408  *
409  *    Find cache entry by the name associated with the ID. This returns
410  *    one entry and the found entry is considered to be exact match.
411  *    return muliptle entries allocated to the SilcIDCacheList. Returns
412  *    TRUE if the entry was found.
413  *
414  ***/
415 bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
416                                    SilcIDCacheEntry *ret);
417
418 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_count
419  *
420  * SYNOPSIS
421  *
422  *    int silc_idcache_list_count(SilcIDCacheList list);
423  *
424  * DESCRIPTION
425  *
426  *    Returns the number of cache entries in the ID cache list.
427  *
428  ***/
429 int silc_idcache_list_count(SilcIDCacheList list);
430
431 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_first
432  *
433  * SYNOPSIS
434  *
435  *    bool silc_idcache_list_first(SilcIDCacheList list, 
436  *                                 SilcIDCacheEntry *ret);
437  *
438  * DESCRIPTION
439  *
440  *    Returns the first cache entry from the ID cache list. Returns FALSE
441  *    If the entry could not be retrieved.
442  *
443  ***/
444 bool silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret);
445
446 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_next
447  *
448  * SYNOPSIS
449  *
450  *    bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret);
451  *
452  * DESCRIPTION
453  *
454  *    Returns the next cache entry from the ID Cache list. Returns FALSE
455  *    when there are not anymore entries in the list.
456  *
457  ***/
458 bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret);
459
460 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_free
461  *
462  * SYNOPSIS
463  *
464  *    void silc_idcache_list_free(SilcIDCacheList list);
465  *
466  * DESCRIPTION
467  *
468  *     Frees ID cache list. User must free the list context returned by
469  *     any of the searching functions.
470  *
471  ***/
472 void silc_idcache_list_free(SilcIDCacheList list);
473
474 #endif