5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2000 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.
20 /* XXX: This ID cache system sucks and must be rewritten! */
24 * Revision 1.2 2000/07/05 06:06:35 priikone
25 * Global cosmetic change.
27 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
28 * Imported from internal CVS/Added Log headers.
33 #include "silcincludes.h"
35 /* qsort() sorter function. */
37 static int silc_idcache_sorter(const void *a, const void *b)
41 a1 = (SilcIDCache *)a;
42 b1 = (SilcIDCache *)b;
44 return a1->data[0] - b1->data[0];
47 /* Sorts given cache by data */
49 void silc_idcache_sort_by_data(SilcIDCache *cache, unsigned int count)
51 qsort(cache, count, sizeof(*cache), silc_idcache_sorter);
54 /* Find ID Cache entry by data. The data maybe anything that must
57 int silc_idcache_find_by_data(SilcIDCache *cache, unsigned int cache_count,
58 char *data, SilcIDCache **ret)
68 for (i = 0; i < cache_count; i++)
69 if (cache[i].data && !memcmp(cache[i].data, data, strlen(data))) {
78 /* Find ID Cache entry by ID. */
80 int silc_idcache_find_by_id(SilcIDCache *cache, unsigned int cache_count,
81 void *id, SilcIdType type, SilcIDCache **ret)
91 id_len = silc_id_get_len(type);
93 for (i = 0; i < cache_count; i++)
94 if (cache[i].id && !memcmp(cache[i].id, id, id_len)) {
103 /* Add new entry to the cache. Returns number of allocated cache
104 entries in the cache. */
106 int silc_idcache_add(SilcIDCache **cache, unsigned int cache_count,
107 char *data, SilcIdType id_type, void *id,
112 unsigned long curtime = time(NULL);
114 SILC_LOG_DEBUG(("Adding cache entry"));
119 c = silc_calloc(5, sizeof(*c));
123 /* See if it exists already */
124 if (silc_idcache_find_by_id(c, cache_count, id, id_type, NULL) == TRUE)
127 for (i = 0; i < cache_count; i++) {
128 if (c[i].data == NULL) {
132 c[i].expire = curtime + SILC_ID_CACHE_EXPIRE;
133 c[i].context = context;
138 if (i == cache_count) {
139 c = silc_realloc(c, sizeof(*c) * (cache_count + 5));
140 for (i = cache_count; i < cache_count + 5; i++) {
144 c[cache_count].data = data;
145 c[cache_count].type = id_type;
146 c[cache_count].id = id;
147 c[cache_count].expire = curtime + SILC_ID_CACHE_EXPIRE;
148 c[cache_count].context = context;
157 /* Delete cache entry from cache. */
160 int silc_idcache_del(SilcIDCache *cache, SilcIDCache *old)
168 int silc_idcache_del_by_data(SilcIDCache *cache, unsigned int cache_count,
175 /* Deletes ID cache entry by ID. */
177 int silc_idcache_del_by_id(SilcIDCache *cache, unsigned int cache_count,
178 SilcIdType type, void *id)
188 id_len = silc_id_get_len(type);
190 for (i = 0; i < cache_count; i++)
191 if (cache[i].id && !memcmp(cache[i].id, id, id_len)) {
193 cache[i].data = NULL;
195 cache[i].context = NULL;
202 /* Deletes all ID entries from cache. Free's memory as well. */
204 int silc_idcache_del_all(SilcIDCache **cache, unsigned int cache_count)
206 SilcIDCache *c = *cache;
212 for (i = 0; i < cache_count; i++) {
226 /* Purges the cache by removing expired cache entires. This does not
227 free any memory though. */
229 int silc_idcache_purge(SilcIDCache *cache, unsigned int cache_count)
231 unsigned long curtime = time(NULL);
237 for (i = 0; i < cache_count; i++) {
239 (cache[i].expire == 0 || cache[i].expire < curtime)) {
241 cache[i].data = NULL;
244 cache[i].context = NULL;