updates.
[silc.git] / lib / silccore / idcache.h
1 /*
2
3   idcache.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2000 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    unsigned char *data
33
34       The data that is usually used to find the data from the cache.
35       For example for Client ID's this is nickname.
36
37    SilcIdType type
38
39       Type of the ID.
40
41    void *id
42
43       The actual ID.
44
45    unsigned long expire
46
47       Time when this cache entry expires.  This is normal time() value
48       plus the validity.  Cache entry has expired if current time is
49       more than value in this field.  If this value is zero (0) the
50       entry never expires.
51
52    void *context
53
54       Any caller specified context.
55
56 */
57 typedef struct {
58   unsigned char *data;
59   SilcIdType type;
60   void *id;
61   unsigned long expire;
62   void *context;
63 } *SilcIDCacheEntry;
64
65 /* Forward declaration for SILC ID Cache object. */
66 typedef struct SilcIDCacheStruct *SilcIDCache;
67
68 /* Forward declaration for ID Cache List */
69 typedef struct SilcIDCacheListStruct *SilcIDCacheList;
70
71 /* Destructor callback that is called when an cache entry expires or is
72    purged from the ID cache. The application must not free cache entry
73    because the library will do it automatically. The appliation, however,
74    is responsible of freeing any data in the entry. */
75 typedef void (*SilcIDCacheDestructor)(SilcIDCache cache,
76                                       SilcIDCacheEntry entry);
77
78 #define SILC_ID_CACHE_ANY ((void *)1)
79
80 #define SILC_ID_CACHE_EXPIRE 3600
81
82 /* Prototypes */
83 SilcIDCache silc_idcache_alloc(unsigned int count,
84                                SilcIDCacheDestructor destructor);
85 void silc_idcache_free(SilcIDCache cache);
86 void silc_idcache_sort_by_data(SilcIDCache cache);
87 int silc_idcache_find_by_data(SilcIDCache cache, unsigned char *data, 
88                               SilcIDCacheList *ret);
89 int silc_idcache_find_by_data_one(SilcIDCache cache, unsigned char *data,
90                                   SilcIDCacheEntry *ret);
91 int silc_idcache_find_by_data_loose(SilcIDCache cache, unsigned char *data, 
92                                     SilcIDCacheList *ret);
93 int silc_idcache_find_by_id(SilcIDCache cache, void *id, SilcIdType type,
94                             SilcIDCacheList *ret);
95 int silc_idcache_find_by_id_one(SilcIDCache cache, void *id, SilcIdType type, 
96                                 SilcIDCacheEntry *ret);
97 int silc_idcache_find_by_context(SilcIDCache cache, void *context, 
98                                  SilcIDCacheEntry *ret);
99 int silc_idcache_add(SilcIDCache cache, unsigned char *data, 
100                      SilcIdType id_type, void *id, void *context, int sort,
101                      int expire);
102 int silc_idcache_del(SilcIDCache cache, SilcIDCacheEntry old);
103 int silc_idcache_del_by_data(SilcIDCache cache, unsigned char *data);
104 int silc_idcache_del_by_id(SilcIDCache cache, SilcIdType type, void *id);
105 int silc_idcache_del_all(SilcIDCache cache);
106 int silc_idcache_purge(SilcIDCache cache);
107 int silc_idcache_purge_by_context(SilcIDCache cache, void *context);
108 int silc_idcache_list_count(SilcIDCacheList list);
109 int silc_idcache_list_first(SilcIDCacheList list, SilcIDCacheEntry *ret);
110 int silc_idcache_list_next(SilcIDCacheList list, SilcIDCacheEntry *ret);
111 void silc_idcache_list_free(SilcIDCacheList list);
112
113 #endif