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.2 2000/07/03 05:52:11 priikone
24 * Fixed typo and a bug.
26 * Revision 1.1.1.1 2000/06/27 11:36:56 priikone
27 * Importet from internal CVS/Added Log headers.
32 #include "serverincludes.h"
35 /* Adds a new server to the list. The pointer sent as argument is allocated
38 void silc_idlist_add_server(SilcServerList **list,
39 char *server_name, int server_type,
40 SilcServerID *id, SilcServerList *router,
41 SilcCipher send_key, SilcCipher receive_key,
42 SilcPKCS public_key, SilcHmac hmac,
43 SilcServerList **new_idlist)
45 SilcServerList *last, *idlist;
47 SILC_LOG_DEBUG(("Adding new server to id list"));
49 idlist = silc_calloc(1, sizeof(*idlist));
51 SILC_LOG_ERROR(("Could not allocate new server list object"));
56 /* Set the pointers */
57 idlist->server_name = server_name;
58 idlist->server_type = server_type;
60 idlist->router = router;
61 idlist->send_key = send_key;
62 idlist->receive_key = receive_key;
63 idlist->public_key = public_key;
65 idlist->next = idlist;
66 idlist->prev = idlist;
68 /* First on the list? */
75 /* Add it to the list */
78 (*list)->prev = idlist;
79 idlist->next = (*list);
86 /* Adds a new client to the client list. This is called when new client
87 connection is accepted to the server. This adds all the relevant data
88 about the client and session with it to the list. This list is
89 referenced for example when sending message to the client. */
91 void silc_idlist_add_client(SilcClientList **list, char *nickname,
92 char *username, char *userinfo,
93 SilcClientID *id, SilcServerList *router,
94 SilcCipher send_key, SilcCipher receive_key,
95 SilcPKCS public_key, SilcHmac hmac,
96 SilcClientList **new_idlist)
98 SilcClientList *last, *idlist;
100 SILC_LOG_DEBUG(("Adding new client to id list"));
102 idlist = silc_calloc(1, sizeof(*idlist));
103 if (idlist == NULL) {
104 SILC_LOG_ERROR(("Could not allocate new client list object"));
108 /* Set the pointers */
109 idlist->nickname = nickname;
110 idlist->username = username;
111 idlist->userinfo = userinfo;
113 idlist->router = router;
114 idlist->send_key = send_key;
115 idlist->receive_key = receive_key;
116 idlist->public_key = public_key;
118 idlist->next = idlist;
119 idlist->prev = idlist;
121 /* First on the list? */
125 *new_idlist = idlist;
129 /* Add it to the list */
130 last = (*list)->prev;
132 (*list)->prev = idlist;
133 idlist->next = *list;
137 *new_idlist = idlist;
140 /* Free client entry. This free's everything. */
142 void silc_idlist_del_client(SilcClientList **list, SilcClientList *entry)
146 silc_free(entry->nickname);
148 silc_free(entry->username);
150 silc_free(entry->userinfo);
152 silc_free(entry->id);
154 silc_cipher_free(entry->send_key);
155 if (entry->receive_key)
156 silc_cipher_free(entry->receive_key);
157 if (entry->public_key)
158 silc_pkcs_free(entry->public_key);
160 silc_hmac_free(entry->hmac);
161 if (entry->hmac_key) {
162 memset(entry->hmac_key, 0, entry->hmac_key_len);
163 silc_free(entry->hmac_key);
166 /* Last one in list? */
167 if (*list == entry && entry->next == entry) {
173 /* At the start of list? */
174 if (*list == entry && entry->next != entry) {
176 entry->next->prev = entry->prev;
177 entry->prev->next = *list;
182 /* Remove from list */
183 entry->prev->next = entry->next;
184 entry->next->prev = entry->prev;
191 silc_idlist_find_client_by_nickname(SilcClientList *list,
195 SilcClientList *first, *entry;
197 SILC_LOG_DEBUG(("Finding client by nickname"));
202 first = entry = list;
203 if (!strcmp(entry->nickname, nickname)) {
204 SILC_LOG_DEBUG(("Found"));
209 while(entry != first) {
210 if (!strcmp(entry->nickname, nickname)) {
211 SILC_LOG_DEBUG(("Found"));
222 silc_idlist_find_client_by_hash(SilcClientList *list,
223 char *nickname, SilcHash md5hash)
225 SilcClientList *first, *entry;
226 unsigned char hash[16];
228 SILC_LOG_DEBUG(("Finding client by nickname hash"));
233 /* Make hash of the nickname */
234 silc_hash_make(md5hash, nickname, strlen(nickname), hash);
236 first = entry = list;
237 if (entry && !SILC_ID_COMPARE_HASH(entry->id, hash)) {
238 SILC_LOG_DEBUG(("Found"));
243 while(entry != first) {
244 if (entry && !SILC_ID_COMPARE_HASH(entry->id, hash)) {
245 SILC_LOG_DEBUG(("Found"));
256 silc_idlist_find_client_by_id(SilcClientList *list, SilcClientID *id)
258 SilcClientList *first, *entry;
260 SILC_LOG_DEBUG(("Finding client by Client ID"));
265 first = entry = list;
266 if (entry && !SILC_ID_CLIENT_COMPARE(entry->id, id)) {
267 SILC_LOG_DEBUG(("Found"));
272 while(entry != first) {
273 if (entry && !SILC_ID_CLIENT_COMPARE(entry->id, id)) {
274 SILC_LOG_DEBUG(("Found"));
284 /* Adds new channel to the list. */
286 void silc_idlist_add_channel(SilcChannelList **list,
287 char *channel_name, int mode,
288 SilcChannelID *id, SilcServerList *router,
289 SilcCipher channel_key,
290 SilcChannelList **new_idlist)
292 SilcChannelList *last, *idlist;
294 SILC_LOG_DEBUG(("Adding new channel to id list"));
296 idlist = silc_calloc(1, sizeof(*idlist));
297 if (idlist == NULL) {
298 SILC_LOG_ERROR(("Could not allocate new channel list object"));
302 /* Set the pointers */
303 idlist->channel_name = channel_name;
306 idlist->router = router;
307 idlist->channel_key = channel_key;
308 idlist->next = idlist;
309 idlist->prev = idlist;
311 /* First on the list? */
315 *new_idlist = idlist;
319 /* Add it to the list */
320 last = (*list)->prev;
322 (*list)->prev = idlist;
323 idlist->next = (*list);
327 *new_idlist = idlist;
331 silc_idlist_find_channel_by_id(SilcChannelList *list, SilcChannelID *id)
333 SilcChannelList *first, *entry;
335 SILC_LOG_DEBUG(("Finding channel by Channel ID"));
340 first = entry = list;
341 if (entry && !SILC_ID_CHANNEL_COMPARE(entry->id, id)) {
342 SILC_LOG_DEBUG(("Found"));
347 while(entry != first) {
348 if (entry && !SILC_ID_CHANNEL_COMPARE(entry->id, id)) {
349 SILC_LOG_DEBUG(("Found"));
359 /* Free channel entry. This free's everything. */
361 void silc_idlist_del_channel(SilcChannelList **list, SilcChannelList *entry)
364 if (entry->channel_name)
365 silc_free(entry->channel_name);
367 silc_free(entry->id);
369 silc_free(entry->topic);
370 if (entry->channel_key)
371 silc_cipher_free(entry->channel_key);
373 memset(entry->key, 0, entry->key_len / 8);
374 silc_free(entry->key);
376 memset(entry->iv, 0, sizeof(entry->iv));
378 if (entry->user_list_count)
379 silc_free(entry->user_list);
381 /* Last one in list? */
382 if (*list == entry && entry->next == entry) {
388 /* At the start of list? */
389 if (*list == entry && entry->next != entry) {
391 entry->next->prev = entry->prev;
392 entry->prev->next = *list;
397 /* Remove from list */
398 entry->prev->next = entry->next;
399 entry->next->prev = entry->prev;