updates
[silc.git] / lib / silccore / silcidcache.h
1 /*
2
3   silcidcache.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
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 #ifndef IDCACHE_H
22 #define IDCACHE_H
23
24 /* 
25    Silc ID Cache Entry object.
26
27    This is one entry in the SILC ID Cache system. Contents of this is
28    allocated outside the ID cache system, however, all the fields are 
29    filled with ID cache utility functions. The ID cache system does not
30    allocate any of these fields nor free them.
31
32    void *id
33
34       The actual ID.
35
36    char name
37
38       A name associated with the ID.
39
40    uint32 expire
41
42       Time when this cache entry expires.  This is normal time() value
43       plus the validity.  Cache entry has expired if current time is
44       more than value in this field.  If this value is zero (0) the
45       entry never expires.
46
47    void *context
48
49       Any caller specified context.
50
51 */
52 typedef struct {
53   void *id;
54   char *name;
55   uint32 expire;
56   void *context;
57 } *SilcIDCacheEntry;
58
59 /* Forward declaration for SILC ID Cache object. */
60 typedef struct SilcIDCacheStruct *SilcIDCache;
61
62 /* Forward declaration for ID Cache List */
63 typedef struct SilcIDCacheListStruct *SilcIDCacheList;
64
65 /* Destructor callback that is called when an cache entry expires or is
66    purged from the ID cache. The application must not free cache entry
67    because the library will do it automatically. The appliation, however,
68    is responsible of freeing any data in the entry. */
69 typedef void (*SilcIDCacheDestructor)(SilcIDCache cache,
70                                       SilcIDCacheEntry entry);
71
72 #define SILC_ID_CACHE_ANY ((void *)1)
73
74 #define SILC_ID_CACHE_EXPIRE 3600
75 #define SILC_ID_CACHE_EXPIRE_DEF (time(NULL) + SILC_ID_CACHE_EXPIRE)
76
77 /* Prototypes */
78 SilcIDCache silc_idcache_alloc(uint32 count, SilcIdType id_type,
79                                SilcIDCacheDestructor destructor);
80 void silc_idcache_free(SilcIDCache cache);
81 bool silc_idcache_add(SilcIDCache cache, char *name, void *id, 
82                       void *context, int expire);
83 bool silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old);
84 bool silc_idcache_del_by_id(SilcIDCache cache, void *id);
85 bool silc_idcache_del_by_id_ext(SilcIDCache cache, void *id,
86                                 SilcHashFunction hash, 
87                                 void *hash_context,
88                                 SilcHashCompare compare, 
89                                 void *compare_context);
90 bool silc_idcache_del_by_context(SilcIDCache cache, void *context);
91 bool silc_idcache_del_all(SilcIDCache cache);
92 bool silc_idcache_purge(SilcIDCache cache);
93 bool silc_idcache_purge_by_context(SilcIDCache cache, void *context);
94 bool silc_idcache_get_all(SilcIDCache cache, SilcIDCacheList *ret);
95 bool silc_idcache_find_by_id(SilcIDCache cache, void *id, 
96                              SilcIDCacheList *ret);
97 bool silc_idcache_find_by_id_one(SilcIDCache cache, void *id, 
98                                  SilcIDCacheEntry *ret);
99 bool silc_idcache_find_by_id_one_ext(SilcIDCache cache, void *id, 
100                                      SilcHashFunction hash, 
101                                      void *hash_context,
102                                      SilcHashCompare compare, 
103                                      void *compare_context,
104                                      SilcIDCacheEntry *ret);
105 bool silc_idcache_find_by_context(SilcIDCache cache, void *context, 
106                                   SilcIDCacheEntry *ret);
107 bool silc_idcache_find_by_name(SilcIDCache cache, char *name, 
108                                SilcIDCacheList *ret);
109 bool silc_idcache_find_by_name_one(SilcIDCache cache, char *name,
110                                    SilcIDCacheEntry *ret);
111 int silc_idcache_list_count(SilcIDCacheList list);
112 bool silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret);
113 bool silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret);
114 void silc_idcache_list_free(SilcIDCacheList list);
115
116 #endif