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 SilcServerEntry from,
334 SilcClientEntry client,
336 SilcIDCacheEntry client_cache)
338 SilcServerEntry server_entry;
339 SilcIDCacheEntry id_cache = NULL;
340 SilcIDCacheList list;
342 if (!silc_idcache_get_all(server->local_list->servers, &list))
345 if (silc_idcache_list_first(list, &id_cache)) {
347 server_entry = (SilcServerEntry)id_cache->context;
348 if (server_entry != from &&
349 SILC_ID_COMPARE(server_entry->id, client->id,
350 client->id->ip.data_len)) {
351 SILC_LOG_DEBUG(("Found (local) %s",
352 silc_id_render(server_entry->id, SILC_ID_SERVER)));
354 /* If the client is not marked as local then move it to local list
355 since the server is local. */
356 if (server_entry->server_type != SILC_BACKUP_ROUTER && !local) {
357 SILC_LOG_DEBUG(("Moving client to local list"));
358 silc_idcache_add(server->local_list->clients, client_cache->name,
359 client_cache->id, client_cache->context,
360 client_cache->expire);
361 silc_idcache_del_by_context(server->global_list->clients, client);
364 silc_idcache_list_free(list);
368 if (!silc_idcache_list_next(list, &id_cache))
373 silc_idcache_list_free(list);
375 if (!silc_idcache_get_all(server->global_list->servers, &list))
378 if (silc_idcache_list_first(list, &id_cache)) {
380 server_entry = (SilcServerEntry)id_cache->context;
381 if (server_entry != from &&
382 SILC_ID_COMPARE(server_entry->id, client->id,
383 client->id->ip.data_len)) {
384 SILC_LOG_DEBUG(("Found (global) %s",
385 silc_id_render(server_entry->id, SILC_ID_SERVER)));
387 /* If the client is marked as local then move it to global list
388 since the server is global. */
389 if (server_entry->server_type != SILC_BACKUP_ROUTER && local) {
390 SILC_LOG_DEBUG(("Moving client to global list"));
391 silc_idcache_add(server->global_list->clients, client_cache->name,
392 client_cache->id, client_cache->context,
393 client_cache->expire);
394 silc_idcache_del_by_context(server->local_list->clients, client);
397 silc_idcache_list_free(list);
401 if (!silc_idcache_list_next(list, &id_cache))
406 silc_idcache_list_free(list);
411 /* Updates the clients that are originated from the `from' to be originated
412 from the `to'. If the `resolve_real_server' is TRUE then this will
413 attempt to figure out which clients really are originated from the
414 `from' and which are originated from a server that we have connection
415 to, when we've acting as backup router. If it is FALSE the `to' will
416 be the new source. This function also removes the clients that are
417 *really* originated from `from' if `remove_from' is TRUE. These are
418 clients that the `from' owns, and not just clients that are behind
421 void silc_server_update_clients_by_server(SilcServer server,
422 SilcServerEntry from,
424 bool resolve_real_server,
427 SilcIDCacheList list = NULL;
428 SilcIDCacheEntry id_cache = NULL;
429 SilcClientEntry client = NULL;
432 SILC_LOG_DEBUG(("Start"));
434 SILC_LOG_DEBUG(("Updating %s", silc_id_render(from->id,
436 SILC_LOG_DEBUG(("to %s", silc_id_render(to->id,
441 if (silc_idcache_get_all(server->local_list->clients, &list)) {
442 if (silc_idcache_list_first(list, &id_cache)) {
444 client = (SilcClientEntry)id_cache->context;
446 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
447 if (!silc_idcache_list_next(list, &id_cache))
453 SILC_LOG_DEBUG(("Client (local) %s",
454 silc_id_render(client->id, SILC_ID_CLIENT)));
456 if (client->router == from) {
457 /* Skip clients that are *really* owned by the `from' */
458 if (remove_from && SILC_ID_COMPARE(from->id, client->id,
459 client->id->ip.data_len)) {
460 SILC_LOG_DEBUG(("Found really owned client, skip it"));
461 if (!silc_idcache_list_next(list, &id_cache))
467 if (resolve_real_server) {
469 silc_server_update_clients_by_real_server(server, from, client,
478 if (!silc_idcache_list_next(list, &id_cache))
482 silc_idcache_list_free(list);
486 if (silc_idcache_get_all(server->global_list->clients, &list)) {
487 if (silc_idcache_list_first(list, &id_cache)) {
489 client = (SilcClientEntry)id_cache->context;
492 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
493 if (!silc_idcache_list_next(list, &id_cache))
499 SILC_LOG_DEBUG(("Client (global) %s",
500 silc_id_render(client->id, SILC_ID_CLIENT)));
502 if (client->router == from) {
503 /* Skip clients that are *really* owned by the `from' */
504 if (remove_from && SILC_ID_COMPARE(from->id, client->id,
505 client->id->ip.data_len)) {
506 SILC_LOG_DEBUG(("Found really owned client, skip it"));
507 if (!silc_idcache_list_next(list, &id_cache))
513 if (resolve_real_server) {
515 silc_server_update_clients_by_real_server(server, from, client,
524 if (!silc_idcache_list_next(list, &id_cache))
528 silc_idcache_list_free(list);
532 /* Now remove the clients that are still marked as orignated from the
533 `from'. These are the clients that really was owned by the `from' and
534 not just exist behind the `from'. */
535 silc_server_remove_clients_by_server(server, from, TRUE);
538 /* Checks whether given channel has global users. If it does this returns
539 TRUE and FALSE if there is only locally connected clients on the channel. */
541 bool silc_server_channel_has_global(SilcChannelEntry channel)
543 SilcChannelClientEntry chl;
544 SilcHashTableList htl;
546 silc_hash_table_list(channel->user_list, &htl);
547 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
548 if (chl->client->router)
555 /* Checks whether given channel has locally connected users. If it does this
556 returns TRUE and FALSE if there is not one locally connected client. */
558 bool silc_server_channel_has_local(SilcChannelEntry channel)
560 SilcChannelClientEntry chl;
561 SilcHashTableList htl;
563 silc_hash_table_list(channel->user_list, &htl);
564 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
565 if (!chl->client->router)
572 /* Returns TRUE if the given client is on the channel. FALSE if not.
573 This works because we assure that the user list on the channel is
574 always in up to date thus we can only check the channel list from
575 `client' which is faster than checking the user list from `channel'. */
577 bool silc_server_client_on_channel(SilcClientEntry client,
578 SilcChannelEntry channel)
580 if (!client || !channel)
583 if (silc_hash_table_find(client->channels, channel, NULL, NULL))