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.1 2000/06/27 11:36:55 priikone
30 #include "silcincludes.h"
32 /* qsort() sorter function. */
34 static int silc_idcache_sorter(const void *a, const void *b)
38 a1 = (SilcIDCache *)a;
39 b1 = (SilcIDCache *)b;
41 return a1->data[0] - b1->data[0];
44 /* Sorts given cache by data */
46 void silc_idcache_sort_by_data(SilcIDCache *cache, unsigned int count)
48 qsort(cache, count, sizeof(*cache), silc_idcache_sorter);
51 /* Find ID Cache entry by data. The data maybe anything that must
54 int silc_idcache_find_by_data(SilcIDCache *cache, unsigned int cache_count,
55 char *data, SilcIDCache **ret)
65 for (i = 0; i < cache_count; i++)
66 if (cache[i].data && !memcmp(cache[i].data, data, strlen(data))) {
75 /* Find ID Cache entry by ID. */
77 int silc_idcache_find_by_id(SilcIDCache *cache, unsigned int cache_count,
78 void *id, SilcIdType type, SilcIDCache **ret)
88 id_len = silc_id_get_len(type);
90 for (i = 0; i < cache_count; i++)
91 if (cache[i].id && !memcmp(cache[i].id, id, id_len)) {
100 /* Add new entry to the cache. Returns number of allocated cache
101 entries in the cache. */
103 int silc_idcache_add(SilcIDCache **cache, unsigned int cache_count,
104 char *data, SilcIdType id_type, void *id,
109 unsigned long curtime = time(NULL);
111 SILC_LOG_DEBUG(("Adding cache entry"));
116 c = silc_calloc(5, sizeof(*c));
122 /* See if it exists already */
123 if (silc_idcache_find_by_id(c, cache_count, id, id_type, NULL) == TRUE)
126 for (i = 0; i < cache_count; i++) {
127 if (c[i].data == NULL) {
131 c[i].expire = curtime + SILC_ID_CACHE_EXPIRE;
132 c[i].context = context;
137 if (i == cache_count) {
138 c = silc_realloc(c, sizeof(*c) * (cache_count + 5));
141 for (i = cache_count; i < cache_count + 5; i++) {
145 c[cache_count].data = data;
146 c[cache_count].type = id_type;
147 c[cache_count].id = id;
148 c[cache_count].expire = curtime + SILC_ID_CACHE_EXPIRE;
149 c[cache_count].context = context;
158 /* Delete cache entry from cache. */
161 int silc_idcache_del(SilcIDCache *cache, SilcIDCache *old)
169 int silc_idcache_del_by_data(SilcIDCache *cache, unsigned int cache_count,
176 /* Deletes ID cache entry by ID. */
178 int silc_idcache_del_by_id(SilcIDCache *cache, unsigned int cache_count,
179 SilcIdType type, void *id)
189 id_len = silc_id_get_len(type);
191 for (i = 0; i < cache_count; i++)
192 if (cache[i].id && !memcmp(cache[i].id, id, id_len)) {
194 cache[i].data = NULL;
196 cache[i].context = NULL;
203 /* Deletes all ID entries from cache. Free's memory as well. */
205 int silc_idcache_del_all(SilcIDCache **cache, unsigned int cache_count)
207 SilcIDCache *c = *cache;
213 for (i = 0; i < cache_count; i++) {
227 /* Purges the cache by removing expired cache entires. This does not
228 free any memory though. */
230 int silc_idcache_purge(SilcIDCache *cache, unsigned int cache_count)
232 unsigned long curtime = time(NULL);
238 for (i = 0; i < cache_count; i++) {
240 (cache[i].expire == 0 || cache[i].expire < curtime)) {
242 cache[i].data = NULL;
245 cache[i].context = NULL;