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.3 2000/07/05 06:14:01 priikone
24 * Global costemic changes.
26 * Revision 1.2 2000/07/03 05:52:11 priikone
27 * Fixed typo and a bug.
29 * Revision 1.1.1.1 2000/06/27 11:36:56 priikone
30 * Imported from internal CVS/Added Log headers.
35 #include "serverincludes.h"
38 /* Adds a new server to the list. The pointer sent as argument is allocated
41 void silc_idlist_add_server(SilcServerList **list,
42 char *server_name, int server_type,
43 SilcServerID *id, SilcServerList *router,
44 SilcCipher send_key, SilcCipher receive_key,
45 SilcPKCS public_key, SilcHmac hmac,
46 SilcServerList **new_idlist)
48 SilcServerList *last, *idlist;
50 SILC_LOG_DEBUG(("Adding new server to id list"));
52 idlist = silc_calloc(1, sizeof(*idlist));
54 SILC_LOG_ERROR(("Could not allocate new server list object"));
59 /* Set the pointers */
60 idlist->server_name = server_name;
61 idlist->server_type = server_type;
63 idlist->router = router;
64 idlist->send_key = send_key;
65 idlist->receive_key = receive_key;
66 idlist->public_key = public_key;
68 idlist->next = idlist;
69 idlist->prev = idlist;
71 /* First on the list? */
78 /* Add it to the list */
81 (*list)->prev = idlist;
82 idlist->next = (*list);
89 /* Adds a new client to the client list. This is called when new client
90 connection is accepted to the server. This adds all the relevant data
91 about the client and session with it to the list. This list is
92 referenced for example when sending message to the client. */
94 void silc_idlist_add_client(SilcClientList **list, char *nickname,
95 char *username, char *userinfo,
96 SilcClientID *id, SilcServerList *router,
97 SilcCipher send_key, SilcCipher receive_key,
98 SilcPKCS public_key, SilcHmac hmac,
99 SilcClientList **new_idlist)
101 SilcClientList *last, *idlist;
103 SILC_LOG_DEBUG(("Adding new client to id list"));
105 idlist = silc_calloc(1, sizeof(*idlist));
106 if (idlist == NULL) {
107 SILC_LOG_ERROR(("Could not allocate new client list object"));
111 /* Set the pointers */
112 idlist->nickname = nickname;
113 idlist->username = username;
114 idlist->userinfo = userinfo;
116 idlist->router = router;
117 idlist->send_key = send_key;
118 idlist->receive_key = receive_key;
119 idlist->public_key = public_key;
121 idlist->next = idlist;
122 idlist->prev = idlist;
124 /* First on the list? */
128 *new_idlist = idlist;
132 /* Add it to the list */
133 last = (*list)->prev;
135 (*list)->prev = idlist;
136 idlist->next = *list;
140 *new_idlist = idlist;
143 /* Free client entry. This free's everything. */
145 void silc_idlist_del_client(SilcClientList **list, SilcClientList *entry)
149 silc_free(entry->nickname);
151 silc_free(entry->username);
153 silc_free(entry->userinfo);
155 silc_free(entry->id);
157 silc_cipher_free(entry->send_key);
158 if (entry->receive_key)
159 silc_cipher_free(entry->receive_key);
160 if (entry->public_key)
161 silc_pkcs_free(entry->public_key);
163 silc_hmac_free(entry->hmac);
164 if (entry->hmac_key) {
165 memset(entry->hmac_key, 0, entry->hmac_key_len);
166 silc_free(entry->hmac_key);
169 /* Last one in list? */
170 if (*list == entry && entry->next == entry) {
176 /* At the start of list? */
177 if (*list == entry && entry->next != entry) {
179 entry->next->prev = entry->prev;
180 entry->prev->next = *list;
185 /* Remove from list */
186 entry->prev->next = entry->next;
187 entry->next->prev = entry->prev;
194 silc_idlist_find_client_by_nickname(SilcClientList *list,
198 SilcClientList *first, *entry;
200 SILC_LOG_DEBUG(("Finding client by nickname"));
205 first = entry = list;
206 if (!strcmp(entry->nickname, nickname)) {
207 SILC_LOG_DEBUG(("Found"));
212 while(entry != first) {
213 if (!strcmp(entry->nickname, nickname)) {
214 SILC_LOG_DEBUG(("Found"));
225 silc_idlist_find_client_by_hash(SilcClientList *list,
226 char *nickname, SilcHash md5hash)
228 SilcClientList *first, *entry;
229 unsigned char hash[16];
231 SILC_LOG_DEBUG(("Finding client by nickname hash"));
236 /* Make hash of the nickname */
237 silc_hash_make(md5hash, nickname, strlen(nickname), hash);
239 first = entry = list;
240 if (entry && !SILC_ID_COMPARE_HASH(entry->id, hash)) {
241 SILC_LOG_DEBUG(("Found"));
246 while(entry != first) {
247 if (entry && !SILC_ID_COMPARE_HASH(entry->id, hash)) {
248 SILC_LOG_DEBUG(("Found"));
259 silc_idlist_find_client_by_id(SilcClientList *list, SilcClientID *id)
261 SilcClientList *first, *entry;
263 SILC_LOG_DEBUG(("Finding client by Client ID"));
268 first = entry = list;
269 if (entry && !SILC_ID_CLIENT_COMPARE(entry->id, id)) {
270 SILC_LOG_DEBUG(("Found"));
275 while(entry != first) {
276 if (entry && !SILC_ID_CLIENT_COMPARE(entry->id, id)) {
277 SILC_LOG_DEBUG(("Found"));
287 /* Adds new channel to the list. */
289 void silc_idlist_add_channel(SilcChannelList **list,
290 char *channel_name, int mode,
291 SilcChannelID *id, SilcServerList *router,
292 SilcCipher channel_key,
293 SilcChannelList **new_idlist)
295 SilcChannelList *last, *idlist;
297 SILC_LOG_DEBUG(("Adding new channel to id list"));
299 idlist = silc_calloc(1, sizeof(*idlist));
300 if (idlist == NULL) {
301 SILC_LOG_ERROR(("Could not allocate new channel list object"));
305 /* Set the pointers */
306 idlist->channel_name = channel_name;
309 idlist->router = router;
310 idlist->channel_key = channel_key;
311 idlist->next = idlist;
312 idlist->prev = idlist;
314 /* First on the list? */
318 *new_idlist = idlist;
322 /* Add it to the list */
323 last = (*list)->prev;
325 (*list)->prev = idlist;
326 idlist->next = (*list);
330 *new_idlist = idlist;
334 silc_idlist_find_channel_by_id(SilcChannelList *list, SilcChannelID *id)
336 SilcChannelList *first, *entry;
338 SILC_LOG_DEBUG(("Finding channel by Channel ID"));
343 first = entry = list;
344 if (entry && !SILC_ID_CHANNEL_COMPARE(entry->id, id)) {
345 SILC_LOG_DEBUG(("Found"));
350 while(entry != first) {
351 if (entry && !SILC_ID_CHANNEL_COMPARE(entry->id, id)) {
352 SILC_LOG_DEBUG(("Found"));
362 /* Free channel entry. This free's everything. */
364 void silc_idlist_del_channel(SilcChannelList **list, SilcChannelList *entry)
367 if (entry->channel_name)
368 silc_free(entry->channel_name);
370 silc_free(entry->id);
372 silc_free(entry->topic);
373 if (entry->channel_key)
374 silc_cipher_free(entry->channel_key);
376 memset(entry->key, 0, entry->key_len / 8);
377 silc_free(entry->key);
379 memset(entry->iv, 0, sizeof(entry->iv));
381 if (entry->user_list_count)
382 silc_free(entry->user_list);
384 /* Last one in list? */
385 if (*list == entry && entry->next == entry) {
391 /* At the start of list? */
392 if (*list == entry && entry->next != entry) {
394 entry->next->prev = entry->prev;
395 entry->prev->next = *list;
400 /* Remove from list */
401 entry->prev->next = entry->next;
402 entry->next->prev = entry->prev;