5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "serverincludes.h"
22 #include "server_internal.h"
24 /* Removes the client from channels and possibly removes the channels
25 as well. After removing those channels that exist, their channel
26 keys are regnerated. This is called only by the function
27 silc_server_remove_clients_by_server. */
29 static void silc_server_remove_clients_channels(SilcServer server,
30 SilcSocketConnection sock,
31 SilcClientEntry client,
32 SilcHashTable channels)
34 SilcChannelEntry channel;
35 SilcChannelClientEntry chl;
36 SilcHashTableList htl;
39 SILC_LOG_DEBUG(("Start"));
41 if (!client || !client->id)
44 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
46 /* Remove the client from all channels. The client is removed from
47 the channels' user list. */
48 silc_hash_table_list(client->channels, &htl);
49 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
50 channel = chl->channel;
52 /* Remove channel from client's channel list */
53 silc_hash_table_del(client->channels, channel);
55 /* Remove channel if there is no users anymore */
56 if (server->server_type == SILC_ROUTER &&
57 silc_hash_table_count(channel->user_list) < 2) {
59 if (silc_hash_table_find(channels, channel, NULL, NULL))
60 silc_hash_table_del(channels, channel);
63 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
65 if (!silc_idlist_del_channel(server->local_list, channel))
66 silc_idlist_del_channel(server->global_list, channel);
67 server->stat.my_channels--;
71 /* Remove client from channel's client list */
72 silc_hash_table_del(channel->user_list, chl->client);
74 /* If there is no global users on the channel anymore mark the channel
75 as local channel. Do not check if the removed client is local client. */
76 if (server->server_type != SILC_ROUTER && channel->global_users &&
77 chl->client->router && !silc_server_channel_has_global(channel))
78 channel->global_users = FALSE;
81 server->stat.my_chanclients--;
83 /* If there is not at least one local user on the channel then we don't
84 need the channel entry anymore, we can remove it safely. */
85 if (server->server_type != SILC_ROUTER &&
86 !silc_server_channel_has_local(channel)) {
88 if (silc_hash_table_find(channels, channel, NULL, NULL))
89 silc_hash_table_del(channels, channel);
92 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
94 if (channel->founder_key) {
95 /* The founder auth data exists, do not remove the channel entry */
96 SilcChannelClientEntry chl2;
97 SilcHashTableList htl2;
101 silc_hash_table_list(channel->user_list, &htl2);
102 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
103 silc_hash_table_del(chl2->client->channels, channel);
104 silc_hash_table_del(channel->user_list, chl2->client);
110 /* Remove the channel entry */
111 if (!silc_idlist_del_channel(server->local_list, channel))
112 silc_idlist_del_channel(server->global_list, channel);
113 server->stat.my_channels--;
117 /* Add the channel to the the channels list to regenerate the
119 if (!silc_hash_table_find(channels, channel, NULL, NULL))
120 silc_hash_table_add(channels, channel, channel);
123 silc_buffer_free(clidp);
126 /* This function is used to remove all client entries by the server `entry'.
127 This is called when the connection is lost to the server. In this case
128 we must invalidate all the client entries owned by the server `entry'.
129 If the `server_signoff' is TRUE then the SERVER_SIGNOFF notify is
130 distributed to our local clients. */
132 bool silc_server_remove_clients_by_server(SilcServer server,
133 SilcServerEntry entry,
136 SilcIDCacheList list = NULL;
137 SilcIDCacheEntry id_cache = NULL;
138 SilcClientEntry client = NULL;
140 SilcClientEntry *clients = NULL;
141 uint32 clients_c = 0;
142 unsigned char **argv = NULL;
143 uint32 *argv_lens = NULL, *argv_types = NULL, argc = 0;
144 SilcHashTableList htl;
145 SilcChannelEntry channel;
146 SilcHashTable channels;
149 SILC_LOG_DEBUG(("Start"));
151 /* Allocate the hash table that holds the channels that require
152 channel key re-generation after we've removed this server's clients
153 from the channels. */
154 channels = silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL, NULL,
157 if (server_signoff) {
158 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
159 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
160 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) * (argc + 1));
161 argv_types = silc_realloc(argv_types, sizeof(*argv_types) * (argc + 1));
162 argv[argc] = silc_calloc(idp->len, sizeof(*argv[0]));
163 memcpy(argv[argc], idp->data, idp->len);
164 argv_lens[argc] = idp->len;
165 argv_types[argc] = argc + 1;
167 silc_buffer_free(idp);
170 if (silc_idcache_get_all(server->local_list->clients, &list)) {
172 if (silc_idcache_list_first(list, &id_cache)) {
174 client = (SilcClientEntry)id_cache->context;
175 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
176 if (!silc_idcache_list_next(list, &id_cache))
182 if (client->router != entry) {
183 if (server_signoff && client->connection) {
184 clients = silc_realloc(clients,
185 sizeof(*clients) * (clients_c + 1));
186 clients[clients_c] = client;
190 if (!silc_idcache_list_next(list, &id_cache))
196 if (server_signoff) {
197 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
198 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
199 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) *
201 argv_types = silc_realloc(argv_types, sizeof(*argv_types) *
203 argv[argc] = silc_calloc(idp->len, sizeof(*argv[0]));
204 memcpy(argv[argc], idp->data, idp->len);
205 argv_lens[argc] = idp->len;
206 argv_types[argc] = argc + 1;
208 silc_buffer_free(idp);
211 /* Remove the client entry */
212 silc_server_remove_clients_channels(server, NULL, client, channels);
213 silc_idlist_del_client(server->local_list, client);
215 if (!silc_idcache_list_next(list, &id_cache))
219 silc_idcache_list_free(list);
222 if (silc_idcache_get_all(server->global_list->clients, &list)) {
224 if (silc_idcache_list_first(list, &id_cache)) {
226 client = (SilcClientEntry)id_cache->context;
227 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
228 if (!silc_idcache_list_next(list, &id_cache))
234 if (client->router != entry) {
235 if (server_signoff && client->connection) {
236 clients = silc_realloc(clients,
237 sizeof(*clients) * (clients_c + 1));
238 clients[clients_c] = client;
242 if (!silc_idcache_list_next(list, &id_cache))
248 if (server_signoff) {
249 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
250 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
251 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) *
253 argv_types = silc_realloc(argv_types, sizeof(*argv_types) *
255 argv[argc] = silc_calloc(idp->len, sizeof(*argv[0]));
256 memcpy(argv[argc], idp->data, idp->len);
257 argv_lens[argc] = idp->len;
258 argv_types[argc] = argc + 1;
260 silc_buffer_free(idp);
263 /* Remove the client entry */
264 silc_server_remove_clients_channels(server, NULL, client, channels);
265 silc_idlist_del_client(server->global_list, client);
267 if (!silc_idcache_list_next(list, &id_cache))
271 silc_idcache_list_free(list);
274 /* Send the SERVER_SIGNOFF notify */
275 if (server_signoff) {
278 /* Send SERVER_SIGNOFF notify to our primary router */
279 if (!server->standalone && server->router &&
280 server->router != entry) {
281 args = silc_argument_payload_encode(1, argv, argv_lens,
283 silc_server_send_notify_args(server,
284 server->router->connection,
285 server->server_type == SILC_SERVER ?
287 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
289 silc_buffer_free(args);
292 args = silc_argument_payload_encode(argc, argv, argv_lens,
294 /* Send to local clients */
295 for (i = 0; i < clients_c; i++) {
296 silc_server_send_notify_args(server, clients[i]->connection,
297 FALSE, SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
302 silc_buffer_free(args);
303 for (i = 0; i < argc; i++)
306 silc_free(argv_lens);
307 silc_free(argv_types);
310 /* We must now re-generate the channel key for all channels that had
311 this server's client(s) on the channel. As they left the channel we
312 must re-generate the channel key. */
313 silc_hash_table_list(channels, &htl);
314 while (silc_hash_table_get(&htl, NULL, (void *)&channel)) {
315 if (!silc_server_create_channel_key(server, channel, 0))
318 /* Do not send the channel key if private channel key mode is set */
319 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
322 silc_server_send_channel_key(server, NULL, channel,
323 server->server_type == SILC_ROUTER ?
324 FALSE : !server->standalone);
326 silc_hash_table_free(channels);
331 static SilcServerEntry
332 silc_server_update_clients_by_real_server(SilcServer server,
333 SilcClientEntry client)
335 SilcServerEntry server_entry;
336 SilcIDCacheEntry id_cache = NULL;
337 SilcIDCacheList list;
339 if (!silc_idcache_get_all(server->local_list->servers, &list))
342 if (silc_idcache_list_first(list, &id_cache)) {
344 server_entry = (SilcServerEntry)id_cache->context;
345 if (SILC_ID_COMPARE(server_entry->id, client->id,
346 client->id->ip.data_len)) {
347 SILC_LOG_DEBUG(("Found"));
348 silc_idcache_list_free(list);
352 if (!silc_idcache_list_next(list, &id_cache))
357 silc_idcache_list_free(list);
362 /* Updates the clients that are originated from the `from' to be originated
363 from the `to'. If the `resolve_real_server' is TRUE then this will
364 attempt to figure out which clients really are originated from the
365 `from' and which are originated from a server that we have connection
366 to, when we've acting as backup router. If it is FALSE the `to' will
367 be the new source. This function also removes the clients that are
368 *really* originated from `from' if `remove_from' is TRUE. These are
369 clients that the `from' owns, and not just clients that are behind
372 void silc_server_update_clients_by_server(SilcServer server,
373 SilcServerEntry from,
375 bool resolve_real_server,
378 SilcIDCacheList list = NULL;
379 SilcIDCacheEntry id_cache = NULL;
380 SilcClientEntry client = NULL;
382 SILC_LOG_DEBUG(("Start"));
384 SILC_LOG_DEBUG(("Updating %s", silc_id_render(from->id,
386 SILC_LOG_DEBUG(("to %s", silc_id_render(to->id,
389 if (silc_idcache_get_all(server->local_list->clients, &list)) {
390 if (silc_idcache_list_first(list, &id_cache)) {
392 client = (SilcClientEntry)id_cache->context;
393 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
394 if (!silc_idcache_list_next(list, &id_cache))
400 if (client->router == from) {
401 /* Skip clients that are *really* owned by the `from' */
402 if (remove_from && SILC_ID_COMPARE(from->id, client->id,
403 client->id->ip.data_len)) {
404 SILC_LOG_DEBUG(("Found really owned client, skip it"));
405 if (!silc_idcache_list_next(list, &id_cache))
411 if (resolve_real_server) {
413 silc_server_update_clients_by_real_server(server, client);
421 if (!silc_idcache_list_next(list, &id_cache))
425 silc_idcache_list_free(list);
428 if (silc_idcache_get_all(server->global_list->clients, &list)) {
429 if (silc_idcache_list_first(list, &id_cache)) {
431 client = (SilcClientEntry)id_cache->context;
432 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
433 if (!silc_idcache_list_next(list, &id_cache))
439 if (client->router == from) {
440 /* Skip clients that are *really* owned by the `from' */
441 if (remove_from && SILC_ID_COMPARE(from->id, client->id,
442 client->id->ip.data_len)) {
443 SILC_LOG_DEBUG(("Found really owned client, skip it"));
444 if (!silc_idcache_list_next(list, &id_cache))
450 if (resolve_real_server) {
452 silc_server_update_clients_by_real_server(server, client);
460 if (!silc_idcache_list_next(list, &id_cache))
464 silc_idcache_list_free(list);
468 /* Now remove the clients that are still marked as orignated from the
469 `from'. These are the clients that really was owned by the `from' and
470 not just exist behind the `from'. */
471 silc_server_remove_clients_by_server(server, from, TRUE);
474 /* Checks whether given channel has global users. If it does this returns
475 TRUE and FALSE if there is only locally connected clients on the channel. */
477 bool silc_server_channel_has_global(SilcChannelEntry channel)
479 SilcChannelClientEntry chl;
480 SilcHashTableList htl;
482 silc_hash_table_list(channel->user_list, &htl);
483 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
484 if (chl->client->router)
491 /* Checks whether given channel has locally connected users. If it does this
492 returns TRUE and FALSE if there is not one locally connected client. */
494 bool silc_server_channel_has_local(SilcChannelEntry channel)
496 SilcChannelClientEntry chl;
497 SilcHashTableList htl;
499 silc_hash_table_list(channel->user_list, &htl);
500 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
501 if (!chl->client->router)
508 /* Returns TRUE if the given client is on the channel. FALSE if not.
509 This works because we assure that the user list on the channel is
510 always in up to date thus we can only check the channel list from
511 `client' which is faster than checking the user list from `channel'. */
513 bool silc_server_client_on_channel(SilcClientEntry client,
514 SilcChannelEntry channel)
516 if (!client || !channel)
519 if (silc_hash_table_find(client->channels, channel, NULL, NULL))