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.6 2000/07/17 11:47:30 priikone
24 * Added command lagging support. Added idle counting support.
26 * Revision 1.5 2000/07/12 05:59:41 priikone
27 * Major rewrite of ID Cache system. Support added for the new
28 * ID cache system. Major rewrite of ID List stuff on server. All
29 * SilcXXXList's are now called SilcXXXEntry's and they are pointers
30 * by default. A lot rewritten ID list functions.
32 * Revision 1.4 2000/07/06 07:16:13 priikone
33 * Added SilcPublicKey's
35 * Revision 1.3 2000/07/05 06:14:01 priikone
36 * Global costemic changes.
38 * Revision 1.2 2000/07/03 05:52:11 priikone
39 * Fixed typo and a bug.
41 * Revision 1.1.1.1 2000/06/27 11:36:56 priikone
42 * Imported from internal CVS/Added Log headers.
47 #include "serverincludes.h"
50 /******************************************************************************
52 Server entry functions
54 ******************************************************************************/
56 /* Add new server entry. This adds the new server entry to ID cache and
57 returns the allocated entry object or NULL on error. This is called
58 when new server connects to us. We also add ourselves to cache with
62 silc_idlist_add_server(SilcIDList id_list,
63 char *server_name, int server_type,
64 SilcServerID *id, SilcServerEntry router,
65 SilcCipher send_key, SilcCipher receive_key,
66 SilcPKCS pkcs, SilcHmac hmac,
67 SilcPublicKey public_key, void *connection)
69 SilcServerEntry server;
71 SILC_LOG_DEBUG(("Adding new server entry"));
73 server = silc_calloc(1, sizeof(*server));
74 server->server_name = server_name;
75 server->server_type = server_type;
77 server->router = router;
78 server->send_key = send_key;
79 server->receive_key = receive_key;
82 server->public_key = public_key;
83 server->connection = connection;
85 if (!silc_idcache_add(id_list->servers, server->server_name, SILC_ID_SERVER,
86 (void *)server->id, (void *)server, TRUE)) {
94 /******************************************************************************
96 Client entry functions
98 ******************************************************************************/
100 /* Add new client entry. This adds the client entry to ID cache system
101 and returns the allocated client entry or NULL on error. This is
102 called when new client connection is accepted to the server. */
105 silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username,
106 char *userinfo, SilcClientID *id,
107 SilcServerEntry router,
108 SilcCipher send_key, SilcCipher receive_key,
109 SilcPKCS pkcs, SilcHmac hmac,
110 SilcPublicKey public_key, void *connection)
112 SilcClientEntry client;
114 SILC_LOG_DEBUG(("Adding new client entry"));
116 client = silc_calloc(1, sizeof(*client));
117 client->nickname = nickname;
118 client->username = username;
119 client->userinfo = userinfo;
121 client->router = router;
122 client->send_key = send_key;
123 client->receive_key = receive_key;
126 client->public_key = public_key;
127 client->connection = connection;
129 if (!silc_idcache_add(id_list->clients, client->nickname, SILC_ID_CLIENT,
130 (void *)client->id, (void *)client, TRUE)) {
138 /* Free client entry. This free's everything and removes the entry
141 void silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry)
144 /* Remove from cache */
146 silc_idcache_del_by_id(id_list->clients, SILC_ID_CLIENT,
151 silc_free(entry->nickname);
153 silc_free(entry->username);
155 silc_free(entry->userinfo);
157 silc_free(entry->id);
159 silc_cipher_free(entry->send_key);
160 if (entry->receive_key)
161 silc_cipher_free(entry->receive_key);
163 silc_pkcs_free(entry->pkcs);
164 if (entry->public_key)
165 silc_pkcs_public_key_free(entry->public_key);
167 silc_hmac_free(entry->hmac);
171 /* Finds client entry by nickname. */
174 silc_idlist_find_client_by_nickname(SilcIDList id_list, char *nickname,
177 SilcIDCacheList list = NULL;
178 SilcIDCacheEntry id_cache = NULL;
179 SilcClientEntry client = NULL;
181 SILC_LOG_DEBUG(("Finding client by nickname"));
184 if (!silc_idcache_find_by_data(id_list->clients, nickname, &list))
188 while (silc_idcache_list_next(list, &id_cache)) {
189 client = (SilcClientEntry)id_cache->context;
191 if (!strcmp(server, XXX, strlen(server)))
201 silc_idcache_list_free(list);
203 if (!silc_idcache_find_by_data_one(id_list->clients, nickname, &id_cache))
206 client = (SilcClientEntry)id_cache->context;
212 /* Finds client by nickname hash. */
215 silc_idlist_find_client_by_hash(SilcIDList id_list, unsigned char *hash,
222 /* Finds client by Client ID */
225 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id)
227 SilcIDCacheEntry id_cache = NULL;
228 SilcClientEntry client;
233 SILC_LOG_DEBUG(("Finding client by ID"));
235 if (!silc_idcache_find_by_id_one(id_list->clients, (void *)id,
236 SILC_ID_CLIENT, &id_cache))
239 client = (SilcClientEntry)id_cache->context;
244 /******************************************************************************
246 Channel entry functions
248 ******************************************************************************/
250 /* Add new channel entry. This add the new channel entry to the ID cache
251 system and returns the allocated entry or NULL on error. */
254 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
255 SilcChannelID *id, SilcServerEntry router,
256 SilcCipher channel_key)
258 SilcChannelEntry channel;
260 channel = silc_calloc(1, sizeof(*channel));
261 channel->channel_name = channel_name;
262 channel->mode = mode;
264 channel->router = router;
265 channel->channel_key = channel_key;
267 if (!silc_idcache_add(id_list->channels, channel->channel_name,
268 SILC_ID_CHANNEL, (void *)channel->id,
269 (void *)channel, TRUE)) {
277 /* Free channel entry. This free's everything. */
279 void silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry)
282 /* Remove from cache */
284 silc_idcache_del_by_id(id_list->channels, SILC_ID_CHANNEL,
288 if (entry->channel_name)
289 silc_free(entry->channel_name);
291 silc_free(entry->id);
293 silc_free(entry->topic);
294 if (entry->channel_key)
295 silc_cipher_free(entry->channel_key);
297 memset(entry->key, 0, entry->key_len / 8);
298 silc_free(entry->key);
300 memset(entry->iv, 0, sizeof(entry->iv));
302 if (entry->user_list_count)
303 silc_free(entry->user_list);
307 /* Finds channel by channel name. Channel names are unique and they
308 are not case-sensitive. */
311 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name)
313 SilcIDCacheList list = NULL;
314 SilcIDCacheEntry id_cache = NULL;
315 SilcChannelEntry channel;
317 SILC_LOG_DEBUG(("Finding channel by name"));
319 if (!silc_idcache_find_by_data_loose(id_list->channels, name, &list))
322 if (!silc_idcache_list_first(list, &id_cache))
325 channel = (SilcChannelEntry)id_cache->context;
327 silc_idcache_list_free(list);
332 /* Finds channel by Channel ID. */
335 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id)
337 SilcIDCacheEntry id_cache = NULL;
338 SilcChannelEntry channel;
343 SILC_LOG_DEBUG(("Finding channel by ID"));
345 if (!silc_idcache_find_by_id_one(id_list->channels, (void *)id,
346 SILC_ID_CHANNEL, &id_cache))
349 channel = (SilcChannelEntry)id_cache->context;