5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 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.
23 * Revision 1.5 2000/07/12 05:59:41 priikone
24 * Major rewrite of ID Cache system. Support added for the new
25 * ID cache system. Major rewrite of ID List stuff on server. All
26 * SilcXXXList's are now called SilcXXXEntry's and they are pointers
27 * by default. A lot rewritten ID list functions.
29 * Revision 1.4 2000/07/06 07:16:13 priikone
30 * Added SilcPublicKey's
32 * Revision 1.3 2000/07/05 06:14:01 priikone
33 * Global costemic changes.
35 * Revision 1.2 2000/07/03 05:52:11 priikone
36 * Fixed typo and a bug.
38 * Revision 1.1.1.1 2000/06/27 11:36:56 priikone
39 * Imported from internal CVS/Added Log headers.
44 #include "serverincludes.h"
47 /******************************************************************************
49 Server entry functions
51 ******************************************************************************/
53 /* Add new server entry. This adds the new server entry to ID cache and
54 returns the allocated entry object or NULL on error. This is called
55 when new server connects to us. We also add ourselves to cache with
59 silc_idlist_add_server(SilcIDList id_list,
60 char *server_name, int server_type,
61 SilcServerID *id, SilcServerEntry router,
62 SilcCipher send_key, SilcCipher receive_key,
63 SilcPKCS pkcs, SilcHmac hmac,
64 SilcPublicKey public_key, void *connection)
66 SilcServerEntry server;
68 SILC_LOG_DEBUG(("Adding new server entry"));
70 server = silc_calloc(1, sizeof(*server));
71 server->server_name = server_name;
72 server->server_type = server_type;
74 server->router = router;
75 server->send_key = send_key;
76 server->receive_key = receive_key;
79 server->public_key = public_key;
80 server->connection = connection;
82 if (!silc_idcache_add(id_list->servers, server->server_name, SILC_ID_SERVER,
83 (void *)server->id, (void *)server, TRUE)) {
91 /******************************************************************************
93 Client entry functions
95 ******************************************************************************/
97 /* Add new client entry. This adds the client entry to ID cache system
98 and returns the allocated client entry or NULL on error. This is
99 called when new client connection is accepted to the server. */
102 silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username,
103 char *userinfo, SilcClientID *id,
104 SilcServerEntry router,
105 SilcCipher send_key, SilcCipher receive_key,
106 SilcPKCS pkcs, SilcHmac hmac,
107 SilcPublicKey public_key, void *connection)
109 SilcClientEntry client;
111 SILC_LOG_DEBUG(("Adding new client entry"));
113 client = silc_calloc(1, sizeof(*client));
114 client->nickname = nickname;
115 client->username = username;
116 client->userinfo = userinfo;
118 client->router = router;
119 client->send_key = send_key;
120 client->receive_key = receive_key;
123 client->public_key = public_key;
124 client->connection = connection;
126 if (!silc_idcache_add(id_list->clients, client->nickname, SILC_ID_CLIENT,
127 (void *)client->id, (void *)client, TRUE)) {
135 /* Free client entry. This free's everything and removes the entry
138 void silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry)
141 /* Remove from cache */
143 silc_idcache_del_by_id(id_list->clients, SILC_ID_CLIENT,
148 silc_free(entry->nickname);
150 silc_free(entry->username);
152 silc_free(entry->userinfo);
154 silc_free(entry->id);
156 silc_cipher_free(entry->send_key);
157 if (entry->receive_key)
158 silc_cipher_free(entry->receive_key);
160 silc_pkcs_free(entry->pkcs);
161 if (entry->public_key)
162 silc_pkcs_public_key_free(entry->public_key);
164 silc_hmac_free(entry->hmac);
165 if (entry->hmac_key) {
166 memset(entry->hmac_key, 0, entry->hmac_key_len);
167 silc_free(entry->hmac_key);
172 /* Finds client entry by nickname. */
175 silc_idlist_find_client_by_nickname(SilcIDList id_list, char *nickname,
178 SilcIDCacheList list = NULL;
179 SilcIDCacheEntry id_cache = NULL;
180 SilcClientEntry client = NULL;
182 SILC_LOG_DEBUG(("Finding client by nickname"));
185 if (!silc_idcache_find_by_data(id_list->clients, nickname, &list))
189 while (silc_idcache_list_next(list, &id_cache)) {
190 client = (SilcClientEntry)id_cache->context;
192 if (!strcmp(server, XXX, strlen(server)))
202 silc_idcache_list_free(list);
204 if (!silc_idcache_find_by_data_one(id_list->clients, nickname, &id_cache))
207 client = (SilcClientEntry)id_cache->context;
213 /* Finds client by nickname hash. */
216 silc_idlist_find_client_by_hash(SilcIDList id_list, unsigned char *hash,
223 /* Finds client by Client ID */
226 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id)
228 SilcIDCacheEntry id_cache = NULL;
229 SilcClientEntry client;
234 SILC_LOG_DEBUG(("Finding client by ID"));
236 if (!silc_idcache_find_by_id_one(id_list->clients, (void *)id,
237 SILC_ID_CLIENT, &id_cache))
240 client = (SilcClientEntry)id_cache->context;
245 /******************************************************************************
247 Channel entry functions
249 ******************************************************************************/
251 /* Add new channel entry. This add the new channel entry to the ID cache
252 system and returns the allocated entry or NULL on error. */
255 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
256 SilcChannelID *id, SilcServerEntry router,
257 SilcCipher channel_key)
259 SilcChannelEntry channel;
261 channel = silc_calloc(1, sizeof(*channel));
262 channel->channel_name = channel_name;
263 channel->mode = mode;
265 channel->router = router;
266 channel->channel_key = channel_key;
268 if (!silc_idcache_add(id_list->channels, channel->channel_name,
269 SILC_ID_CHANNEL, (void *)channel->id,
270 (void *)channel, TRUE)) {
278 /* Free channel entry. This free's everything. */
280 void silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry)
283 /* Remove from cache */
285 silc_idcache_del_by_id(id_list->channels, SILC_ID_CHANNEL,
289 if (entry->channel_name)
290 silc_free(entry->channel_name);
292 silc_free(entry->id);
294 silc_free(entry->topic);
295 if (entry->channel_key)
296 silc_cipher_free(entry->channel_key);
298 memset(entry->key, 0, entry->key_len / 8);
299 silc_free(entry->key);
301 memset(entry->iv, 0, sizeof(entry->iv));
303 if (entry->user_list_count)
304 silc_free(entry->user_list);
308 /* Finds channel by channel name. Channel names are unique and they
309 are not case-sensitive. */
312 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name)
314 SilcIDCacheList list = NULL;
315 SilcIDCacheEntry id_cache = NULL;
316 SilcChannelEntry channel;
318 SILC_LOG_DEBUG(("Finding channel by name"));
320 if (!silc_idcache_find_by_data_loose(id_list->channels, name, &list))
323 if (!silc_idcache_list_first(list, &id_cache))
326 channel = (SilcChannelEntry)id_cache->context;
328 silc_idcache_list_free(list);
333 /* Finds channel by Channel ID. */
336 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id)
338 SilcIDCacheEntry id_cache = NULL;
339 SilcChannelEntry channel;
344 SILC_LOG_DEBUG(("Finding channel by ID"));
346 if (!silc_idcache_find_by_id_one(id_list->channels, (void *)id,
347 SILC_ID_CHANNEL, &id_cache))
350 channel = (SilcChannelEntry)id_cache->context;