5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2000 - 2001 Pekka Riikonen
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.
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.
21 /****h* silccore/SILC ID Cache Interface
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.
34 /****s* silccore/SilcIDCacheAPI/SilcIDCacheEntry
38 * typedef struct { ... } SilcIDCacheEntry;
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.
53 * A name associated with the ID.
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.
64 * Any caller specified context.
76 /****s* silccore/SilcIDCacheAPI/SilcIDCache
80 * typedef struct SilcIDCacheStruct *SilcIDCache;
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.
90 typedef struct SilcIDCacheStruct *SilcIDCache;
92 /****s* silccore/SilcIDCacheAPI/SilcIDCacheList
96 * typedef struct SilcIDCacheListStruct *SilcIDCacheList;
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
107 typedef struct SilcIDCacheListStruct *SilcIDCacheList;
109 /****f* silccore/SilcIDCacheAPI/SilcIDCacheDestructor
113 * typedef void (*SilcIDCacheDestructor)(SilcIDCache cache,
114 * SilcIDCacheEntry entry);
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.
124 typedef void (*SilcIDCacheDestructor)(SilcIDCache cache,
125 SilcIDCacheEntry entry);
127 #define SILC_ID_CACHE_EXPIRE 3600
128 #define SILC_ID_CACHE_EXPIRE_DEF (time(NULL) + SILC_ID_CACHE_EXPIRE)
132 /****f* silccore/SilcIDCacheAPI/silc_idcache_alloc
136 * SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type,
137 * SilcIDCacheDestructor destructor);
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
147 SilcIDCache silc_idcache_alloc(SilcUInt32 count, SilcIdType id_type,
148 SilcIDCacheDestructor destructor);
150 /****f* silccore/SilcIDCacheAPI/silc_idcache_free
154 * void silc_idcache_free(SilcIDCache cache);
158 * Frees ID cache object and all cache entries.
161 void silc_idcache_free(SilcIDCache cache);
163 /****f* silccore/SilcIDCacheAPI/silc_idcache_add
167 * bool silc_idcache_add(SilcIDCache cache, char *name, void *id,
168 * void *context, int expire, SilcIDCacheEntry *ret);
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 user specific context.
175 * If the `expire' is non-zero the entry expires in that specified time.
176 * If zero the entry never expires from the cache.
178 * The `name', `id' and `context' pointers will be saved in the cache,
179 * and if the caller frees these pointers the caller is also responsible
180 * of deleting the cache entry. Otherwise the cache will have the freed
183 * If the `ret' is non-NULL the created ID Cache entry is returned to
187 bool silc_idcache_add(SilcIDCache cache, char *name, void *id,
188 void *context, int expire, SilcIDCacheEntry *ret);
190 /****f* silccore/SilcIDCacheAPI/silc_idcache_del
194 * bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old);
198 * Delete cache entry from cache. Returns TRUE if the entry was deleted.
199 * The destructor function is not called.
202 bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old);
204 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_id
208 * bool silc_idcache_del_by_id(SilcIDCache cache, void *id);
212 * Delete cache entry by ID. Returns TRUE if the entry was deleted.
213 * The destructor function is not called.
216 bool silc_idcache_del_by_id(SilcIDCache cache, void *id);
218 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_id_ext
222 * bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
223 * SilcHashFunction hash,
224 * void *hash_context,
225 * SilcHashCompare compare,
226 * void *compare_context);
230 * Same as silc_idcache_del_by_id but with specific hash and comparison
231 * functions. If the functions are NULL then default values are used.
232 * Returns TRUE if the entry was deleted. The destructor function is
236 bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
237 SilcHashFunction hash,
239 SilcHashCompare compare,
240 void *compare_context);
242 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_by_context
246 * bool silc_idcache_del_by_context(SilcIDCache cache, void *context);
250 * Deletes cachen entry by the user specified context. Returns TRUE
251 * if the entry was deleted. The destructor function is not called.
254 bool silc_idcache_del_by_context(SilcIDCache cache, void *context);
256 /****f* silccore/SilcIDCacheAPI/silc_idcache_del_all
260 * bool silc_idcache_del_all(SilcIDCache cache);
264 * Deletes all cache entries from the cache and frees all memory.
265 * The destructor function is not called.
268 bool silc_idcache_del_all(SilcIDCache cache);
270 /****f* silccore/SilcIDCacheAPI/silc_idcache_purge
274 * bool silc_idcache_purge(SilcIDCache cache);
278 * Purges the cache by removing expired cache entires. Note that this
279 * may be very slow operation. Returns TRUE if the purging was successful.
280 * The destructor function is called for each purged cache entry.
283 bool silc_idcache_purge(SilcIDCache cache);
285 /****f* silccore/SilcIDCacheAPI/silc_idcache_by_context
289 * bool silc_idcache_purge_by_context(SilcIDCache cache, void *context);
293 * Purges the cache by context and removes expired cache entires.
294 * Returns TRUE if the puring was successful. The destructor function
295 * is called for the purged cache entry.
298 bool silc_idcache_purge_by_context(SilcIDCache cache, void *context);
300 /****f* silccore/SilcIDCacheAPI/silc_idcache_get_all
304 * bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret);
308 * Returns all cache entries from the ID cache to the `ret' SilcIDCacheList.
309 * Returns TRUE if the retrieval was successful. The caller must free
310 * the returned SilcIDCacheList.
313 bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret);
315 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id
319 * bool silc_idcache_find_by_id(SilcIDCache cache, void *id,
320 * SilcIDCacheList *ret);
324 * Find ID Cache entry by ID. This may return multiple entry and the
325 * `ret' SilcIDCacheList is allocated. Returns TRUE if the entry was
326 * found. The caller must free the returned SilcIDCacheList.
329 bool silc_idcache_find_by_id(SilcIDCache cache, void *id,
330 SilcIDCacheList *ret);
332 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id_one
336 * bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
337 * SilcIDCacheEntry *ret);
341 * Find ID Cache entry by ID. Returns only one entry from the cache
342 * and the found entry is considered to be exact match. Returns TRUE
343 * if the entry was found.
346 bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id,
347 SilcIDCacheEntry *ret);
349 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_id_one_ext
353 * bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id,
354 * SilcHashFunction hash,
355 * void *hash_context,
356 * SilcHashCompare compare,
357 * void *compare_context,
358 * SilcIDCacheEntry *ret);
362 * Same as silc_idcache_find_by_id_one but with specific hash and
363 * comparison functions. If `hash' is NULL then the default hash
364 * funtion is used and if `compare' is NULL default comparison function
365 * is used. Returns TRUE if the entry was found.
368 bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id,
369 SilcHashFunction hash,
371 SilcHashCompare compare,
372 void *compare_context,
373 SilcIDCacheEntry *ret);
375 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_context
379 * bool silc_idcache_find_by_context(SilcIDCache cache, void *context,
380 * SilcIDCacheEntry *ret);
384 * Find cache entry by user specified context. Returns TRUE if the
388 bool silc_idcache_find_by_context(SilcIDCache cache, void *context,
389 SilcIDCacheEntry *ret);
391 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_name
395 * bool silc_idcache_find_by_name(SilcIDCache cache, char *name,
396 * SilcIDCacheList *ret);
400 * Find cache entries by the name associated with the ID. This may
401 * return muliptle entries allocated to the SilcIDCacheList. Returns
402 * TRUE if the entry was found. The caller must free the SIlcIDCacheList.
405 bool silc_idcache_find_by_name(SilcIDCache cache, char *name,
406 SilcIDCacheList *ret);
408 /****f* silccore/SilcIDCacheAPI/silc_idcache_find_by_name_one
412 * bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
413 * SilcIDCacheEntry *ret);
417 * Find cache entry by the name associated with the ID. This returns
418 * one entry and the found entry is considered to be exact match.
419 * return muliptle entries allocated to the SilcIDCacheList. Returns
420 * TRUE if the entry was found.
423 bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
424 SilcIDCacheEntry *ret);
426 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_count
430 * int silc_idcache_list_count(SilcIDCacheList list);
434 * Returns the number of cache entries in the ID cache list.
437 int silc_idcache_list_count(SilcIDCacheList list);
439 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_first
443 * bool silc_idcache_list_first(SilcIDCacheList list,
444 * SilcIDCacheEntry *ret);
448 * Returns the first cache entry from the ID cache list. Returns FALSE
449 * If the entry could not be retrieved.
452 bool silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret);
454 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_next
458 * bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret);
462 * Returns the next cache entry from the ID Cache list. Returns FALSE
463 * when there are not anymore entries in the list.
466 bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret);
468 /****f* silccore/SilcIDCacheAPI/silc_idcache_list_free
472 * void silc_idcache_list_free(SilcIDCacheList list);
476 * Frees ID cache list. User must free the list context returned by
477 * any of the searching functions.
480 void silc_idcache_list_free(SilcIDCacheList list);