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