5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2003 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.
20 * This is the actual SILC server than handles everything relating to
21 * servicing the SILC connections. This is also a SILC router as a router
22 * is also normal server.
26 #include "serverincludes.h"
27 #include "server_internal.h"
29 /* Static prototypes */
30 SILC_TASK_CALLBACK(silc_server_rehash_close_connection);
31 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry);
32 SILC_TASK_CALLBACK(silc_server_connect_router);
33 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
34 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
35 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
36 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
37 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
38 SILC_TASK_CALLBACK(silc_server_packet_process);
39 SILC_TASK_CALLBACK(silc_server_packet_parse_real);
40 SILC_TASK_CALLBACK(silc_server_close_connection_final);
41 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout);
42 SILC_TASK_CALLBACK(silc_server_timeout_remote);
43 SILC_TASK_CALLBACK(silc_server_channel_key_rekey);
44 SILC_TASK_CALLBACK(silc_server_get_stats);
46 /* Allocates a new SILC server object. This has to be done before the server
47 can be used. After allocation one must call silc_server_init to initialize
48 the server. The new allocated server object is returned to the new_server
51 int silc_server_alloc(SilcServer *new_server)
55 SILC_LOG_DEBUG(("Allocating new server object"));
57 server = silc_calloc(1, sizeof(*server));
58 server->server_type = SILC_SERVER;
59 server->standalone = TRUE;
60 server->local_list = silc_calloc(1, sizeof(*server->local_list));
61 server->global_list = silc_calloc(1, sizeof(*server->global_list));
62 server->pending_commands = silc_dlist_init();
64 server->sim = silc_dlist_init();
72 /* Free's the SILC server object. This is called at the very end before
75 void silc_server_free(SilcServer server)
78 SilcIDCacheEntry cache;
86 silc_dlist_start(server->sim);
87 while ((sim = silc_dlist_get(server->sim)) != SILC_LIST_END) {
88 silc_dlist_del(server->sim, sim);
92 silc_dlist_uninit(server->sim);
96 silc_server_backup_free(server);
97 silc_server_config_unref(&server->config_ref);
99 silc_rng_free(server->rng);
101 silc_pkcs_free(server->pkcs);
102 if (server->public_key)
103 silc_pkcs_public_key_free(server->public_key);
104 if (server->private_key)
105 silc_pkcs_private_key_free(server->private_key);
106 if (server->pending_commands)
107 silc_dlist_uninit(server->pending_commands);
108 if (server->id_entry)
109 silc_idlist_del_server(server->local_list, server->id_entry);
111 /* Delete all channels */
113 if (silc_idcache_get_all(server->local_list->channels, &list) &&
114 silc_idcache_list_first(list, &cache)) {
115 silc_idlist_del_channel(server->local_list, cache->context);
116 while (silc_idcache_list_next(list, &cache))
117 silc_idlist_del_channel(server->local_list, cache->context);
120 silc_idcache_list_free(list);
122 if (silc_idcache_get_all(server->global_list->channels, &list) &&
123 silc_idcache_list_first(list, &cache)) {
124 silc_idlist_del_channel(server->global_list, cache->context);
125 while (silc_idcache_list_next(list, &cache))
126 silc_idlist_del_channel(server->global_list, cache->context);
129 silc_idcache_list_free(list);
131 /* Delete all clients */
133 if (silc_idcache_get_all(server->local_list->clients, &list) &&
134 silc_idcache_list_first(list, &cache)) {
135 silc_idlist_del_client(server->local_list, cache->context);
136 while (silc_idcache_list_next(list, &cache))
137 silc_idlist_del_client(server->local_list, cache->context);
140 silc_idcache_list_free(list);
142 if (silc_idcache_get_all(server->global_list->clients, &list) &&
143 silc_idcache_list_first(list, &cache)) {
144 silc_idlist_del_client(server->global_list, cache->context);
145 while (silc_idcache_list_next(list, &cache))
146 silc_idlist_del_client(server->global_list, cache->context);
149 silc_idcache_list_free(list);
151 /* Delete all servers */
153 if (silc_idcache_get_all(server->local_list->servers, &list) &&
154 silc_idcache_list_first(list, &cache)) {
155 silc_idlist_del_server(server->local_list, cache->context);
156 while (silc_idcache_list_next(list, &cache))
157 silc_idlist_del_server(server->local_list, cache->context);
160 silc_idcache_list_free(list);
162 if (silc_idcache_get_all(server->global_list->servers, &list) &&
163 silc_idcache_list_first(list, &cache)) {
164 silc_idlist_del_server(server->global_list, cache->context);
165 while (silc_idcache_list_next(list, &cache))
166 silc_idlist_del_server(server->global_list, cache->context);
169 silc_idcache_list_free(list);
171 silc_idcache_free(server->local_list->clients);
172 silc_idcache_free(server->local_list->servers);
173 silc_idcache_free(server->local_list->channels);
174 silc_idcache_free(server->global_list->clients);
175 silc_idcache_free(server->global_list->servers);
176 silc_idcache_free(server->global_list->channels);
177 silc_hash_table_free(server->watcher_list);
179 silc_hash_free(server->md5hash);
180 silc_hash_free(server->sha1hash);
181 silc_hmac_unregister_all();
182 silc_hash_unregister_all();
183 silc_cipher_unregister_all();
184 silc_pkcs_unregister_all();
186 silc_free(server->local_list);
187 silc_free(server->global_list);
188 silc_free(server->server_name);
189 silc_free(server->id_string);
190 silc_free(server->purge_i);
191 silc_free(server->purge_g);
195 /* Creates a new server listener. */
197 static bool silc_server_listen(SilcServer server, const char *server_ip,
198 SilcUInt16 port, int *sock)
200 *sock = silc_net_create_server(port, server_ip);
202 SILC_SERVER_LOG_ERROR(("Could not create server listener: %s on %hu",
209 /* Adds a secondary listener. */
211 bool silc_server_init_secondary(SilcServer server)
213 int sock = 0, sock_list[server->config->param.connections_max];
214 SilcSocketConnection newsocket = NULL;
215 SilcServerConfigServerInfoInterface *interface;
217 for (interface = server->config->server_info->secondary; interface;
218 interface = interface->next, sock++) {
220 if (!silc_server_listen(server,
221 interface->server_ip, interface->port, &sock_list[sock]))
224 /* Set socket to non-blocking mode */
225 silc_net_set_socket_nonblock(sock_list[sock]);
227 /* Add ourselves also to the socket table. The entry allocated above
228 is sent as argument for fast referencing in the future. */
229 silc_socket_alloc(sock_list[sock],
230 SILC_SOCKET_TYPE_SERVER, NULL, &newsocket);
231 server->sockets[sock_list[sock]] = newsocket;
232 SILC_SET_LISTENER(newsocket);
234 /* Perform name and address lookups to resolve the listenning address
236 if (!silc_net_check_local_by_sock(sock_list[sock], &newsocket->hostname,
238 if ((server->config->require_reverse_lookup && !newsocket->hostname) ||
240 SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
241 newsocket->hostname ? newsocket->hostname :
242 newsocket->ip ? newsocket->ip : ""));
243 server->stat.conn_failures++;
246 if (!newsocket->hostname)
247 newsocket->hostname = strdup(newsocket->ip);
249 newsocket->port = silc_net_get_local_port(sock);
251 newsocket->user_data = (void *)server->id_entry;
252 silc_schedule_task_add(server->schedule, sock_list[sock],
253 silc_server_accept_new_connection,
254 (void *)server, 0, 0,
256 SILC_TASK_PRI_NORMAL);
262 do silc_net_close_server(sock_list[sock--]); while (sock >= 0);
266 /* Initializes the entire SILC server. This is called always before running
267 the server. This is called only once at the initialization of the program.
268 This binds the server to its listenning port. After this function returns
269 one should call silc_server_run to start the server. This returns TRUE
270 when everything is ok to run the server. Configuration file must be
271 read and parsed before calling this. */
273 bool silc_server_init(SilcServer server)
277 SilcServerEntry id_entry;
278 SilcIDListPurge purge;
279 SilcSocketConnection newsocket = NULL;
281 SILC_LOG_DEBUG(("Initializing server"));
283 server->starttime = time(NULL);
285 /* Take config object for us */
286 silc_server_config_ref(&server->config_ref, server->config,
290 /* Set debugging on if configured */
291 if (server->config->debug_string) {
293 silc_log_set_debug_string(server->config->debug_string);
297 #endif /* SILC_DEBUG */
299 /* Steal public and private key from the config object */
300 server->public_key = server->config->server_info->public_key;
301 server->private_key = server->config->server_info->private_key;
302 server->config->server_info->public_key = NULL;
303 server->config->server_info->private_key = NULL;
305 /* Register all configured ciphers, PKCS and hash functions. */
306 if (!silc_server_config_register_ciphers(server))
307 silc_cipher_register_default();
308 if (!silc_server_config_register_pkcs(server))
309 silc_pkcs_register_default();
310 if (!silc_server_config_register_hashfuncs(server))
311 silc_hash_register_default();
312 if (!silc_server_config_register_hmacs(server))
313 silc_hmac_register_default();
315 /* Initialize random number generator for the server. */
316 server->rng = silc_rng_alloc();
317 silc_rng_init(server->rng);
318 silc_rng_global_init(server->rng);
320 /* Initialize hash functions for server to use */
321 silc_hash_alloc("md5", &server->md5hash);
322 silc_hash_alloc("sha1", &server->sha1hash);
324 /* Allocate PKCS context for local public and private keys */
325 if (!silc_pkcs_alloc(server->public_key->name, &server->pkcs))
327 silc_pkcs_public_key_set(server->pkcs, server->public_key);
328 silc_pkcs_private_key_set(server->pkcs, server->private_key);
330 /* Initialize the scheduler */
331 server->schedule = silc_schedule_init(server->config->param.connections_max,
333 if (!server->schedule)
336 /* First, register log files configuration for error output */
337 silc_server_config_setlogfiles(server);
339 /* Initialize ID caches */
340 server->local_list->clients =
341 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
342 server->local_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
343 server->local_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
345 /* These are allocated for normal server as well as these hold some
346 global information that the server has fetched from its router. For
347 router these are used as they are supposed to be used on router. */
348 server->global_list->clients =
349 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
350 server->global_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
351 server->global_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
353 /* Init watcher list */
354 server->watcher_list =
355 silc_hash_table_alloc(1, silc_hash_client_id_hash, NULL,
356 silc_hash_data_compare, (void *)CLIENTID_HASH_LEN,
358 if (!server->watcher_list)
361 /* Create a listening server */
362 if (!silc_server_listen(server,
363 server->config->server_info->primary == NULL ? NULL :
364 server->config->server_info->primary->server_ip,
365 server->config->server_info->primary == NULL ? 0 :
366 server->config->server_info->primary->port,
370 /* Set socket to non-blocking mode */
371 silc_net_set_socket_nonblock(sock);
374 /* Allocate the entire socket list that is used in server. Eventually
375 all connections will have entry in this table (it is a table of
376 pointers to the actual object that is allocated individually
378 server->sockets = silc_calloc(server->config->param.connections_max,
379 sizeof(*server->sockets));
380 if (!server->sockets)
383 /* Add ourselves also to the socket table. The entry allocated above
384 is sent as argument for fast referencing in the future. */
385 silc_socket_alloc(sock, SILC_SOCKET_TYPE_SERVER, NULL, &newsocket);
386 server->sockets[sock] = newsocket;
387 SILC_SET_LISTENER(newsocket);
389 /* Perform name and address lookups to resolve the listenning address
391 if (!silc_net_check_local_by_sock(sock, &newsocket->hostname,
393 if ((server->config->require_reverse_lookup && !newsocket->hostname) ||
395 SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
396 newsocket->hostname ? newsocket->hostname :
397 newsocket->ip ? newsocket->ip : ""));
398 server->stat.conn_failures++;
401 if (!newsocket->hostname)
402 newsocket->hostname = strdup(newsocket->ip);
404 newsocket->port = silc_net_get_local_port(sock);
406 /* Create a Server ID for the server. */
407 silc_id_create_server_id(newsocket->ip, newsocket->port, server->rng, &id);
412 server->id_string = silc_id_id2str(id, SILC_ID_SERVER);
413 server->id_string_len = silc_id_get_len(id, SILC_ID_SERVER);
414 server->server_name = server->config->server_info->server_name;
415 server->config->server_info->server_name = NULL;
417 /* Add ourselves to the server list. We don't have a router yet
418 beacuse we haven't established a route yet. It will be done later.
419 For now, NULL is sent as router. This allocates new entry to
422 silc_idlist_add_server(server->local_list, strdup(server->server_name),
423 server->server_type, server->id, NULL, NULL);
425 SILC_LOG_ERROR(("Could not add ourselves to cache"));
428 id_entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
430 /* Put the allocated socket pointer also to the entry allocated above
431 for fast back-referencing to the socket list. */
432 newsocket->user_data = (void *)id_entry;
433 id_entry->connection = (void *)newsocket;
434 server->id_entry = id_entry;
436 /* Register protocols */
437 silc_server_protocols_register();
439 /* Add the first task to the scheduler. This is task that is executed by
440 timeout. It expires as soon as the caller calls silc_server_run. This
441 task performs authentication protocol and key exchange with our
443 silc_server_create_connections(server);
445 /* Add listener task to the scheduler. This task receives new connections
446 to the server. This task remains on the queue until the end of the
448 silc_schedule_task_add(server->schedule, sock,
449 silc_server_accept_new_connection,
450 (void *)server, 0, 0,
452 SILC_TASK_PRI_NORMAL);
454 if (silc_server_init_secondary(server) == FALSE)
457 server->listenning = TRUE;
459 /* If server connections has been configured then we must be router as
460 normal server cannot have server connections, only router connections. */
461 if (server->config->servers) {
462 SilcServerConfigServer *ptr = server->config->servers;
464 server->server_type = SILC_ROUTER;
466 if (ptr->backup_router) {
467 server->server_type = SILC_BACKUP_ROUTER;
468 server->backup_router = TRUE;
469 server->id_entry->server_type = SILC_BACKUP_ROUTER;
476 /* Register the ID Cache purge task. This periodically purges the ID cache
477 and removes the expired cache entries. */
479 /* Clients local list */
480 server->purge_i = purge = silc_calloc(1, sizeof(*purge));
481 purge->cache = server->local_list->clients;
482 purge->timeout = 600;
483 silc_schedule_task_add(server->schedule, 0, silc_idlist_purge,
484 (void *)purge, purge->timeout, 0,
485 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
487 /* Clients global list */
488 server->purge_g = purge = silc_calloc(1, sizeof(*purge));
489 purge->cache = server->global_list->clients;
490 purge->timeout = 300;
491 silc_schedule_task_add(server->schedule, 0, silc_idlist_purge,
492 (void *)purge, purge->timeout, 0,
493 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
495 /* If we are normal server we'll retrieve network statisticial information
496 once in a while from the router. */
497 if (server->server_type != SILC_ROUTER)
498 silc_schedule_task_add(server->schedule, 0, silc_server_get_stats,
499 server, 10, 0, SILC_TASK_TIMEOUT,
502 if (server->server_type == SILC_ROUTER)
503 server->stat.routers++;
505 SILC_LOG_DEBUG(("Server initialized"));
507 /* We are done here, return succesfully */
511 silc_server_config_unref(&server->config_ref);
512 silc_net_close_server(sock);
516 /* Task callback to close a socket connection after rehash */
518 SILC_TASK_CALLBACK(silc_server_rehash_close_connection)
520 SilcServer server = context;
521 SilcSocketConnection sock = server->sockets[fd];
526 SILC_LOG_INFO(("Connection %s:%d [%s] is unconfigured",
527 sock->hostname, sock->port,
528 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
529 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
530 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
532 silc_schedule_task_del_by_context(server->schedule, sock);
533 silc_server_disconnect_remote(server, sock,
534 SILC_STATUS_ERR_BANNED_FROM_SERVER,
535 "This connection is removed from "
538 silc_server_free_sock_user_data(server, sock, NULL);
541 /* This function basically reads the config file again and switches the config
542 object pointed by the server object. After that, we have to fix various
543 things such as the server_name and the listening ports.
544 Keep in mind that we no longer have the root privileges at this point. */
546 bool silc_server_rehash(SilcServer server)
548 SilcServerConfig newconfig;
550 SILC_LOG_INFO(("Rehashing server"));
552 /* Reset the logging system */
553 silc_log_quick = TRUE;
554 silc_log_flush_all();
556 /* Start the main rehash phase (read again the config file) */
557 newconfig = silc_server_config_alloc(server->config_file);
559 SILC_LOG_ERROR(("Rehash FAILED."));
563 /* Reinit scheduler if necessary */
564 if (newconfig->param.connections_max > server->config->param.connections_max)
565 if (!silc_schedule_reinit(server->schedule,
566 newconfig->param.connections_max))
569 /* Fix the server_name field */
570 if (strcmp(server->server_name, newconfig->server_info->server_name)) {
571 silc_free(server->server_name);
572 server->server_name = newconfig->server_info->server_name;
573 newconfig->server_info->server_name = NULL;
575 /* Update the idcache list with a fresh pointer */
576 silc_free(server->id_entry->server_name);
577 server->id_entry->server_name = strdup(server->server_name);
578 if (!silc_idcache_del_by_context(server->local_list->servers,
581 if (!silc_idcache_add(server->local_list->servers,
582 server->id_entry->server_name,
583 server->id_entry->id, server->id_entry, 0, NULL))
588 silc_server_config_setlogfiles(server);
590 /* Change new key pair if necessary */
591 if (newconfig->server_info->public_key &&
592 !silc_pkcs_public_key_compare(server->public_key,
593 newconfig->server_info->public_key)) {
594 silc_pkcs_public_key_free(server->public_key);
595 silc_pkcs_private_key_free(server->private_key);
596 server->public_key = newconfig->server_info->public_key;
597 server->private_key = newconfig->server_info->private_key;
598 newconfig->server_info->public_key = NULL;
599 newconfig->server_info->private_key = NULL;
601 /* Allocate PKCS context for local public and private keys */
602 silc_pkcs_free(server->pkcs);
603 if (!silc_pkcs_alloc(server->public_key->name, &server->pkcs))
605 silc_pkcs_public_key_set(server->pkcs, server->public_key);
606 silc_pkcs_private_key_set(server->pkcs, server->private_key);
609 /* Check for unconfigured server and router connections and close
610 connections that were unconfigured. */
612 if (server->config->routers) {
613 SilcServerConfigRouter *ptr;
614 SilcServerConfigRouter *newptr;
617 for (ptr = server->config->routers; ptr; ptr = ptr->next) {
620 /* Check whether new config has this one too */
621 for (newptr = newconfig->routers; newptr; newptr = newptr->next) {
622 if (silc_string_compare(newptr->host, ptr->host) &&
623 newptr->port == ptr->port &&
624 newptr->initiator == ptr->initiator) {
630 if (!found && ptr->host) {
631 /* Remove this connection */
632 SilcSocketConnection sock;
633 sock = silc_server_find_socket_by_host(server, SILC_SOCKET_TYPE_ROUTER,
634 ptr->host, ptr->port);
635 if (sock && !SILC_IS_LISTENER(sock))
636 silc_schedule_task_add(server->schedule, sock->sock,
637 silc_server_rehash_close_connection,
638 server, 0, 1, SILC_TASK_TIMEOUT,
639 SILC_TASK_PRI_NORMAL);
644 if (server->config->servers) {
645 SilcServerConfigServer *ptr;
646 SilcServerConfigServer *newptr;
649 for (ptr = server->config->servers; ptr; ptr = ptr->next) {
652 /* Check whether new config has this one too */
653 for (newptr = newconfig->servers; newptr; newptr = newptr->next) {
654 if (silc_string_compare(newptr->host, ptr->host)) {
660 if (!found && ptr->host) {
661 /* Remove this connection */
662 SilcSocketConnection sock;
663 sock = silc_server_find_socket_by_host(server, SILC_SOCKET_TYPE_SERVER,
665 if (sock && !SILC_IS_LISTENER(sock))
666 silc_schedule_task_add(server->schedule, sock->sock,
667 silc_server_rehash_close_connection,
668 server, 0, 1, SILC_TASK_TIMEOUT,
669 SILC_TASK_PRI_NORMAL);
674 if (server->config->clients) {
675 SilcServerConfigClient *ptr;
676 SilcServerConfigClient *newptr;
679 for (ptr = server->config->clients; ptr; ptr = ptr->next) {
682 /* Check whether new config has this one too */
683 for (newptr = newconfig->clients; newptr; newptr = newptr->next) {
684 if (silc_string_compare(newptr->host, ptr->host)) {
690 if (!found && ptr->host) {
691 /* Remove this connection */
692 SilcSocketConnection sock;
693 sock = silc_server_find_socket_by_host(server, SILC_SOCKET_TYPE_CLIENT,
696 silc_schedule_task_add(server->schedule, sock->sock,
697 silc_server_rehash_close_connection,
698 server, 0, 1, SILC_TASK_TIMEOUT,
699 SILC_TASK_PRI_NORMAL);
704 /* Create connections after rehash */
705 silc_server_create_connections(server);
707 /* Check whether our router status has changed */
708 if (newconfig->servers) {
709 SilcServerConfigServer *ptr = newconfig->servers;
711 server->server_type = SILC_ROUTER;
713 if (ptr->backup_router) {
714 server->server_type = SILC_BACKUP_ROUTER;
715 server->backup_router = TRUE;
716 server->id_entry->server_type = SILC_BACKUP_ROUTER;
723 /* Our old config is gone now. We'll unreference our reference made in
724 silc_server_init and then destroy it since we are destroying it
725 underneath the application (layer which called silc_server_init). */
726 silc_server_config_unref(&server->config_ref);
727 silc_server_config_destroy(server->config);
729 /* Take new config context */
730 server->config = newconfig;
731 silc_server_config_ref(&server->config_ref, server->config, server->config);
733 SILC_LOG_DEBUG(("Server rehashed"));
738 /* The heart of the server. This runs the scheduler thus runs the server.
739 When this returns the server has been stopped and the program will
742 void silc_server_run(SilcServer server)
744 SILC_LOG_INFO(("SILC Server started"));
746 /* Start the scheduler, the heart of the SILC server. When this returns
747 the program will be terminated. */
748 silc_schedule(server->schedule);
751 /* Stops the SILC server. This function is used to shutdown the server.
752 This is usually called after the scheduler has returned. After stopping
753 the server one should call silc_server_free. */
755 void silc_server_stop(SilcServer server)
757 SILC_LOG_INFO(("SILC Server shutting down"));
759 if (server->schedule) {
762 server->server_shutdown = TRUE;
764 /* Close all connections */
765 for (i = 0; i < server->config->param.connections_max; i++) {
766 if (!server->sockets[i])
768 if (!SILC_IS_LISTENER(server->sockets[i])) {
769 SilcSocketConnection sock = server->sockets[i];
770 SilcIDListData idata = sock->user_data;
773 idata->status &= ~SILC_IDLIST_STATUS_DISABLED;
775 silc_schedule_task_del_by_context(server->schedule,
777 silc_server_disconnect_remote(server, server->sockets[i],
779 "Server is shutting down");
781 silc_server_free_sock_user_data(server, sock,
782 "Server is shutting down");
783 silc_socket_free(sock);
785 silc_socket_free(server->sockets[i]);
786 server->sockets[i] = NULL;
790 /* We are not connected to network anymore */
791 server->standalone = TRUE;
793 silc_schedule_stop(server->schedule);
794 silc_schedule_uninit(server->schedule);
795 server->schedule = NULL;
797 silc_free(server->sockets);
798 server->sockets = NULL;
801 silc_server_protocols_unregister();
803 SILC_LOG_DEBUG(("Server stopped"));
806 /* Function that is called when the network connection to a router has
807 been established. This will continue with the key exchange protocol
808 with the remote router. */
810 void silc_server_start_key_exchange(SilcServer server,
811 SilcServerConnection sconn,
814 SilcSocketConnection newsocket;
815 SilcProtocol protocol;
816 SilcServerKEInternalContext *proto_ctx;
817 SilcServerConfigRouter *conn =
818 (SilcServerConfigRouter *) sconn->conn.ref_ptr;
821 /* Cancel any possible retry timeouts */
822 silc_schedule_task_del_by_callback(server->schedule,
823 silc_server_connect_to_router_retry);
825 /* Set socket options */
826 silc_net_set_socket_nonblock(sock);
827 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
829 /* Create socket connection for the connection. Even though we
830 know that we are connecting to a router we will mark the socket
831 to be unknown connection until we have executed authentication
833 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
834 server->sockets[sock] = newsocket;
835 newsocket->hostname = strdup(sconn->remote_host);
836 newsocket->ip = strdup(sconn->remote_host);
837 newsocket->port = sconn->remote_port;
838 sconn->sock = newsocket;
840 /* Allocate internal protocol context. This is sent as context
842 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
843 proto_ctx->server = (void *)server;
844 proto_ctx->context = (void *)sconn;
845 proto_ctx->sock = newsocket;
846 proto_ctx->rng = server->rng;
847 proto_ctx->responder = FALSE;
849 /* Set Key Exchange flags from configuration, but fall back to global
851 SILC_GET_SKE_FLAGS(conn, proto_ctx);
852 if (server->config->param.key_exchange_pfs)
853 proto_ctx->flags |= SILC_SKE_SP_FLAG_PFS;
855 /* Perform key exchange protocol. silc_server_connect_to_router_second
856 will be called after the protocol is finished. */
857 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
858 &protocol, proto_ctx,
859 silc_server_connect_to_router_second);
860 newsocket->protocol = protocol;
862 /* Register a timeout task that will be executed if the protocol
863 is not executed within set limit. */
864 proto_ctx->timeout_task =
865 silc_schedule_task_add(server->schedule, sock,
866 silc_server_timeout_remote,
867 server, server->config->key_exchange_timeout, 0,
871 /* Register the connection for network input and output. This sets
872 that scheduler will listen for incoming packets for this connection
873 and sets that outgoing packets may be sent to this connection as
874 well. However, this doesn't set the scheduler for outgoing traffic,
875 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
876 later when outgoing data is available. */
877 context = (void *)server;
878 SILC_REGISTER_CONNECTION_FOR_IO(sock);
880 /* Run the protocol */
881 silc_protocol_execute(protocol, server->schedule, 0, 0);
884 /* Timeout callback that will be called to retry connecting to remote
885 router. This is used by both normal and router server. This will wait
886 before retrying the connecting. The timeout is generated by exponential
887 backoff algorithm. */
889 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry)
891 SilcServer server = app_context;
892 SilcServerConnection sconn = (SilcServerConnection)context;
893 SilcServerConfigRouter *conn = sconn->conn.ref_ptr;
894 SilcServerConfigConnParams *param =
895 (conn->param ? conn->param : &server->config->param);
897 /* Don't retry if we are shutting down. */
898 if (server->server_shutdown) {
899 silc_server_config_unref(&sconn->conn);
900 silc_free(sconn->remote_host);
901 silc_free(sconn->backup_replace_ip);
906 SILC_LOG_INFO(("Retrying connecting to a router"));
908 /* Calculate next timeout */
909 if (sconn->retry_count >= 1) {
910 sconn->retry_timeout = sconn->retry_timeout * SILC_SERVER_RETRY_MULTIPLIER;
911 if (sconn->retry_timeout > param->reconnect_interval_max)
912 sconn->retry_timeout = param->reconnect_interval_max;
914 sconn->retry_timeout = param->reconnect_interval;
916 sconn->retry_count++;
917 sconn->retry_timeout = sconn->retry_timeout +
918 silc_rng_get_rn32(server->rng) % SILC_SERVER_RETRY_RANDOMIZER;
920 /* If we've reached max retry count, give up. */
921 if ((sconn->retry_count > param->reconnect_count) &&
922 !param->reconnect_keep_trying) {
923 SILC_LOG_ERROR(("Could not connect to router, giving up"));
924 silc_server_config_unref(&sconn->conn);
925 silc_free(sconn->remote_host);
926 silc_free(sconn->backup_replace_ip);
931 SILC_LOG_DEBUG(("Retrying connecting to a router in %d seconds",
932 sconn->retry_timeout));
934 /* We will lookup a fresh pointer later */
935 silc_server_config_unref(&sconn->conn);
937 /* Wait one before retrying */
938 silc_schedule_task_add(server->schedule, 0, silc_server_connect_router,
939 context, sconn->retry_timeout, 0,
940 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
943 /* Generic routine to use connect to a router. */
945 SILC_TASK_CALLBACK(silc_server_connect_router)
947 SilcServer server = app_context;
948 SilcServerConnection sconn = (SilcServerConnection)context;
949 SilcServerConfigRouter *rconn;
952 /* Don't connect if we are shutting down. */
953 if (server->server_shutdown) {
954 silc_free(sconn->remote_host);
955 silc_free(sconn->backup_replace_ip);
960 SILC_LOG_INFO(("Connecting to the %s %s on port %d",
961 (sconn->backup ? "backup router" : "router"),
962 sconn->remote_host, sconn->remote_port));
964 server->router_connect = time(NULL);
965 rconn = silc_server_config_find_router_conn(server, sconn->remote_host,
968 SILC_LOG_INFO(("Unconfigured %s connection %s:%d, cannot connect",
969 (sconn->backup ? "backup router" : "router"),
970 sconn->remote_host, sconn->remote_port));
971 silc_free(sconn->remote_host);
972 silc_free(sconn->backup_replace_ip);
976 silc_server_config_ref(&sconn->conn, server->config, (void *)rconn);
978 /* Connect to remote host */
979 sock = silc_net_create_connection(
980 (!server->config->server_info->primary ? NULL :
981 server->config->server_info->primary->server_ip),
982 sconn->remote_port, sconn->remote_host);
984 SILC_LOG_ERROR(("Could not connect to router %s:%d",
985 sconn->remote_host, sconn->remote_port));
986 if (!sconn->no_reconnect)
987 silc_schedule_task_add(server->schedule, 0,
988 silc_server_connect_to_router_retry,
989 context, 0, 1, SILC_TASK_TIMEOUT,
990 SILC_TASK_PRI_NORMAL);
992 silc_server_config_unref(&sconn->conn);
993 silc_free(sconn->remote_host);
994 silc_free(sconn->backup_replace_ip);
1000 /* Continue with key exchange protocol */
1001 silc_server_start_key_exchange(server, sconn, sock);
1004 /* This function connects to our primary router or if we are a router this
1005 establishes all our primary routes. This is called at the start of the
1006 server to do authentication and key exchange with our router - called
1009 SILC_TASK_CALLBACK_GLOBAL(silc_server_connect_to_router)
1011 SilcServer server = (SilcServer)context;
1012 SilcServerConnection sconn;
1013 SilcServerConfigRouter *ptr;
1015 /* Don't connect if we are shutting down. */
1016 if (server->server_shutdown)
1019 SILC_LOG_DEBUG(("We are %s",
1020 (server->server_type == SILC_SERVER ?
1021 "normal server" : server->server_type == SILC_ROUTER ?
1022 "router" : "backup router/normal server")));
1024 if (!server->config->routers) {
1025 /* There wasn't a configured router, we will continue but we don't
1026 have a connection to outside world. We will be standalone server. */
1027 SILC_LOG_DEBUG(("No router(s), we are standalone"));
1028 server->standalone = TRUE;
1032 /* Cancel any possible retry timeouts */
1033 silc_schedule_task_del_by_callback(server->schedule,
1034 silc_server_connect_router);
1035 silc_schedule_task_del_by_callback(server->schedule,
1036 silc_server_connect_to_router_retry);
1038 /* Create the connections to all our routes */
1039 for (ptr = server->config->routers; ptr; ptr = ptr->next) {
1041 SILC_LOG_DEBUG(("%s connection [%s] %s:%d",
1042 ptr->backup_router ? "Backup router" : "Router",
1043 ptr->initiator ? "Initiator" : "Responder",
1044 ptr->host, ptr->port));
1046 if (server->server_type == SILC_ROUTER && ptr->backup_router &&
1047 ptr->initiator == FALSE && !server->backup_router &&
1048 !silc_server_config_get_backup_router(server))
1049 server->wait_backup = TRUE;
1051 if (ptr->initiator) {
1052 /* Check whether we are connecting or connected to this host already */
1053 if (silc_server_num_sockets_by_remote(server,
1054 silc_net_is_ip(ptr->host) ?
1056 silc_net_is_ip(ptr->host) ?
1057 NULL : ptr->host, ptr->port,
1058 SILC_SOCKET_TYPE_ROUTER)) {
1059 SILC_LOG_DEBUG(("We are already connected to this router"));
1062 if (silc_server_num_sockets_by_remote(server,
1063 silc_net_is_ip(ptr->host) ?
1065 silc_net_is_ip(ptr->host) ?
1066 NULL : ptr->host, ptr->port,
1067 SILC_SOCKET_TYPE_UNKNOWN)) {
1068 SILC_LOG_DEBUG(("We are already connecting to this router"));
1072 /* Allocate connection object for hold connection specific stuff. */
1073 sconn = silc_calloc(1, sizeof(*sconn));
1074 sconn->remote_host = strdup(ptr->host);
1075 sconn->remote_port = ptr->port;
1076 sconn->backup = ptr->backup_router;
1077 if (sconn->backup) {
1078 sconn->backup_replace_ip = strdup(ptr->backup_replace_ip);
1079 sconn->backup_replace_port = ptr->backup_replace_port;
1082 if (!server->router_conn && !sconn->backup)
1083 server->router_conn = sconn;
1085 silc_schedule_task_add(server->schedule, 0,
1086 silc_server_connect_router,
1087 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
1088 SILC_TASK_PRI_NORMAL);
1093 /* Second part of connecting to router(s). Key exchange protocol has been
1094 executed and now we will execute authentication protocol. */
1096 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
1098 SilcProtocol protocol = (SilcProtocol)context;
1099 SilcServerKEInternalContext *ctx =
1100 (SilcServerKEInternalContext *)protocol->context;
1101 SilcServer server = (SilcServer)ctx->server;
1102 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
1103 SilcSocketConnection sock = ctx->sock;
1104 SilcServerConnAuthInternalContext *proto_ctx;
1105 SilcServerConfigRouter *conn = NULL;
1107 SILC_LOG_DEBUG(("Start"));
1109 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1110 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1111 /* Error occured during protocol */
1112 silc_protocol_free(protocol);
1113 sock->protocol = NULL;
1114 silc_ske_free_key_material(ctx->keymat);
1116 silc_packet_context_free(ctx->packet);
1118 silc_ske_free(ctx->ske);
1119 silc_free(ctx->dest_id);
1121 silc_server_disconnect_remote(server, sock,
1122 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
1124 /* Try reconnecting if configuration wants it */
1125 if (!sconn->no_reconnect) {
1126 silc_schedule_task_add(server->schedule, 0,
1127 silc_server_connect_to_router_retry,
1128 sconn, 0, 1, SILC_TASK_TIMEOUT,
1129 SILC_TASK_PRI_NORMAL);
1133 /* Call completion to indicate error */
1134 if (sconn->callback)
1135 (*sconn->callback)(server, NULL, sconn->callback_context);
1137 silc_server_config_unref(&sconn->conn);
1138 silc_free(sconn->remote_host);
1139 silc_free(sconn->backup_replace_ip);
1144 /* We now have the key material as the result of the key exchange
1145 protocol. Take the key material into use. Free the raw key material
1146 as soon as we've set them into use. */
1147 if (!silc_server_protocol_ke_set_keys(server, ctx->ske,
1148 ctx->sock, ctx->keymat,
1149 ctx->ske->prop->cipher,
1150 ctx->ske->prop->pkcs,
1151 ctx->ske->prop->hash,
1152 ctx->ske->prop->hmac,
1153 ctx->ske->prop->group,
1155 silc_protocol_free(protocol);
1156 sock->protocol = NULL;
1157 silc_ske_free_key_material(ctx->keymat);
1159 silc_packet_context_free(ctx->packet);
1161 silc_ske_free(ctx->ske);
1162 silc_free(ctx->dest_id);
1164 silc_server_disconnect_remote(server, sock,
1165 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
1167 /* Try reconnecting if configuration wants it */
1168 if (!sconn->no_reconnect) {
1169 silc_schedule_task_add(server->schedule, 0,
1170 silc_server_connect_to_router_retry,
1171 sconn, 0, 1, SILC_TASK_TIMEOUT,
1172 SILC_TASK_PRI_NORMAL);
1176 /* Call completion to indicate error */
1177 if (sconn->callback)
1178 (*sconn->callback)(server, NULL, sconn->callback_context);
1180 silc_server_config_unref(&sconn->conn);
1181 silc_free(sconn->remote_host);
1182 silc_free(sconn->backup_replace_ip);
1186 silc_ske_free_key_material(ctx->keymat);
1188 /* Allocate internal context for the authentication protocol. This
1189 is sent as context for the protocol. */
1190 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1191 proto_ctx->server = (void *)server;
1192 proto_ctx->context = (void *)sconn;
1193 proto_ctx->sock = sock;
1194 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
1195 proto_ctx->dest_id_type = ctx->dest_id_type;
1196 proto_ctx->dest_id = ctx->dest_id;
1198 /* Resolve the authentication method used in this connection. Check if
1199 we find a match from user configured connections */
1200 if (!sconn->conn.ref_ptr)
1201 conn = silc_server_config_find_router_conn(server, sock->hostname,
1204 conn = sconn->conn.ref_ptr;
1207 /* Match found. Use the configured authentication method. Take only
1208 the passphrase, since for public key auth we automatically use
1209 our local key pair. */
1210 if (conn->passphrase) {
1211 if (conn->publickeys && !server->config->prefer_passphrase_auth) {
1212 proto_ctx->auth_meth = SILC_AUTH_PUBLIC_KEY;
1214 proto_ctx->auth_data = strdup(conn->passphrase);
1215 proto_ctx->auth_data_len = strlen(conn->passphrase);
1216 proto_ctx->auth_meth = SILC_AUTH_PASSWORD;
1218 } else if (conn->publickeys) {
1219 proto_ctx->auth_meth = SILC_AUTH_PUBLIC_KEY;
1221 proto_ctx->auth_meth = SILC_AUTH_NONE;
1224 SILC_LOG_ERROR(("Could not find connection data for %s (%s) on port",
1225 sock->hostname, sock->ip, sock->port));
1226 silc_protocol_free(protocol);
1227 sock->protocol = NULL;
1229 silc_packet_context_free(ctx->packet);
1231 silc_ske_free(ctx->ske);
1232 silc_free(ctx->dest_id);
1234 silc_server_config_unref(&sconn->conn);
1235 silc_free(sconn->remote_host);
1236 silc_free(sconn->backup_replace_ip);
1238 silc_server_disconnect_remote(server, sock,
1239 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
1243 /* Free old protocol as it is finished now */
1244 silc_protocol_free(protocol);
1246 silc_packet_context_free(ctx->packet);
1248 sock->protocol = NULL;
1250 /* Allocate the authentication protocol. This is allocated here
1251 but we won't start it yet. We will be receiving party of this
1252 protocol thus we will wait that connecting party will make
1253 their first move. */
1254 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1255 &sock->protocol, proto_ctx,
1256 silc_server_connect_to_router_final);
1258 /* Register timeout task. If the protocol is not executed inside
1259 this timelimit the connection will be terminated. */
1260 proto_ctx->timeout_task =
1261 silc_schedule_task_add(server->schedule, sock->sock,
1262 silc_server_timeout_remote,
1264 server->config->conn_auth_timeout, 0,
1268 /* Run the protocol */
1269 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1272 /* Finalizes the connection to router. Registers a server task to the
1273 queue so that we can accept new connections. */
1275 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
1277 SilcProtocol protocol = (SilcProtocol)context;
1278 SilcServerConnAuthInternalContext *ctx =
1279 (SilcServerConnAuthInternalContext *)protocol->context;
1280 SilcServer server = (SilcServer)ctx->server;
1281 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
1282 SilcSocketConnection sock = ctx->sock;
1283 SilcServerEntry id_entry = NULL;
1285 unsigned char *id_string;
1287 SilcIDListData idata;
1288 SilcServerConfigRouter *conn = NULL;
1289 SilcServerConfigConnParams *param = NULL;
1291 SILC_LOG_DEBUG(("Start"));
1293 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1294 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1295 /* Error occured during protocol */
1296 silc_free(ctx->dest_id);
1297 silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1300 /* Try reconnecting if configuration wants it */
1301 if (!sconn->no_reconnect) {
1302 silc_schedule_task_add(server->schedule, 0,
1303 silc_server_connect_to_router_retry,
1304 sconn, 0, 1, SILC_TASK_TIMEOUT,
1305 SILC_TASK_PRI_NORMAL);
1312 /* Add a task to the queue. This task receives new connections to the
1313 server. This task remains on the queue until the end of the program. */
1314 if (!server->listenning && !sconn->backup) {
1315 silc_schedule_task_add(server->schedule, server->sock,
1316 silc_server_accept_new_connection,
1317 (void *)server, 0, 0,
1319 SILC_TASK_PRI_NORMAL);
1320 server->listenning = TRUE;
1323 /* Send NEW_SERVER packet to the router. We will become registered
1324 to the SILC network after sending this packet. */
1325 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
1326 id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1327 packet = silc_buffer_alloc(2 + 2 + id_len + strlen(server->server_name));
1328 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1329 silc_buffer_format(packet,
1330 SILC_STR_UI_SHORT(id_len),
1331 SILC_STR_UI_XNSTRING(id_string, id_len),
1332 SILC_STR_UI_SHORT(strlen(server->server_name)),
1333 SILC_STR_UI_XNSTRING(server->server_name,
1334 strlen(server->server_name)),
1337 /* Send the packet */
1338 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
1339 packet->data, packet->len, TRUE);
1340 silc_buffer_free(packet);
1341 silc_free(id_string);
1343 SILC_LOG_INFO(("Connected to router %s", sock->hostname));
1345 /* Check that we do not have this ID already */
1346 id_entry = silc_idlist_find_server_by_id(server->local_list,
1347 ctx->dest_id, TRUE, NULL);
1349 silc_idcache_del_by_context(server->local_list->servers, id_entry);
1351 id_entry = silc_idlist_find_server_by_id(server->global_list,
1352 ctx->dest_id, TRUE, NULL);
1354 silc_idcache_del_by_context(server->global_list->servers, id_entry);
1357 SILC_LOG_DEBUG(("New server id(%s)",
1358 silc_id_render(ctx->dest_id, SILC_ID_SERVER)));
1360 /* Add the connected router to global server list. Router is sent
1361 as NULL since it's local to us. */
1362 id_entry = silc_idlist_add_server(server->global_list,
1363 strdup(sock->hostname),
1364 SILC_ROUTER, ctx->dest_id, NULL, sock);
1366 silc_free(ctx->dest_id);
1367 SILC_LOG_ERROR(("Cannot add new server entry to cache"));
1368 silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1373 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1374 silc_free(sock->user_data);
1375 sock->user_data = (void *)id_entry;
1376 sock->type = SILC_SOCKET_TYPE_ROUTER;
1377 idata = (SilcIDListData)sock->user_data;
1378 idata->status |= (SILC_IDLIST_STATUS_REGISTERED |
1379 SILC_IDLIST_STATUS_LOCAL);
1381 conn = sconn->conn.ref_ptr;
1382 param = &server->config->param;
1383 if (conn && conn->param)
1384 param = conn->param;
1386 /* Perform keepalive. */
1387 silc_socket_set_heartbeat(sock, param->keepalive_secs, server,
1388 silc_server_perform_heartbeat,
1391 /* Register re-key timeout */
1392 idata->rekey->timeout = param->key_exchange_rekey;
1393 silc_schedule_task_add(server->schedule, sock->sock,
1394 silc_server_rekey_callback,
1395 (void *)sock, idata->rekey->timeout, 0,
1396 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1398 if (!sconn->backup) {
1399 /* Mark this router our primary router if we're still standalone */
1400 if (server->standalone) {
1401 SILC_LOG_DEBUG(("This connection is our primary router"));
1402 server->id_entry->router = id_entry;
1403 server->router = id_entry;
1404 server->standalone = FALSE;
1406 /* If we are router then announce our possible servers. Backup
1407 router announces also global servers. */
1408 if (server->server_type == SILC_ROUTER)
1409 silc_server_announce_servers(server,
1410 server->backup_router ? TRUE : FALSE,
1411 0, SILC_PRIMARY_ROUTE(server));
1413 /* Announce our clients and channels to the router */
1414 silc_server_announce_clients(server, 0, SILC_PRIMARY_ROUTE(server));
1415 silc_server_announce_channels(server, 0, SILC_PRIMARY_ROUTE(server));
1417 /* If we are backup router then this primary router is whom we are
1419 if (server->server_type == SILC_BACKUP_ROUTER)
1420 silc_server_backup_add(server, server->id_entry, sock->ip,
1421 sconn->remote_port, TRUE);
1424 /* Add this server to be our backup router */
1425 silc_server_backup_add(server, id_entry, sconn->backup_replace_ip,
1426 sconn->backup_replace_port, FALSE);
1429 sock->protocol = NULL;
1432 /* Call the completion callback to indicate that we've connected to
1434 if (sconn && sconn->callback)
1435 (*sconn->callback)(server, id_entry, sconn->callback_context);
1437 /* Free the temporary connection data context */
1439 silc_server_config_unref(&sconn->conn);
1440 silc_free(sconn->remote_host);
1441 silc_free(sconn->backup_replace_ip);
1444 if (sconn == server->router_conn)
1445 server->router_conn = NULL;
1448 /* Free the protocol object */
1449 if (sock->protocol == protocol)
1450 sock->protocol = NULL;
1451 silc_protocol_free(protocol);
1453 silc_packet_context_free(ctx->packet);
1455 silc_ske_free(ctx->ske);
1456 if (ctx->auth_meth == SILC_AUTH_PASSWORD)
1457 silc_free(ctx->auth_data);
1461 /* Host lookup callback that is called after the incoming connection's
1462 IP and FQDN lookup is performed. This will actually check the acceptance
1463 of the incoming connection and will register the key exchange protocol
1464 for this connection. */
1467 silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
1470 SilcServerKEInternalContext *proto_ctx =
1471 (SilcServerKEInternalContext *)context;
1472 SilcServer server = (SilcServer)proto_ctx->server;
1473 SilcServerConfigClient *cconfig = NULL;
1474 SilcServerConfigServer *sconfig = NULL;
1475 SilcServerConfigRouter *rconfig = NULL;
1476 SilcServerConfigDeny *deny;
1479 /* Check whether we could resolve both IP and FQDN. */
1480 if (!sock->ip || (!strcmp(sock->ip, sock->hostname) &&
1481 server->config->require_reverse_lookup)) {
1482 SILC_LOG_ERROR(("IP/DNS lookup failed %s",
1483 sock->hostname ? sock->hostname :
1484 sock->ip ? sock->ip : ""));
1485 server->stat.conn_failures++;
1486 silc_server_disconnect_remote(server, sock,
1487 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1488 "Unknown host or IP");
1489 silc_free(proto_ctx);
1493 /* Register the connection for network input and output. This sets
1494 that scheduler will listen for incoming packets for this connection
1495 and sets that outgoing packets may be sent to this connection as well.
1496 However, this doesn't set the scheduler for outgoing traffic, it
1497 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
1498 later when outgoing data is available. */
1499 context = (void *)server;
1500 SILC_REGISTER_CONNECTION_FOR_IO(sock->sock);
1502 SILC_LOG_INFO(("Incoming connection %s (%s)", sock->hostname,
1505 /* Listenning port */
1506 if (!server->sockets[(SilcUInt32)proto_ctx->context]) {
1507 silc_server_disconnect_remote(server, sock,
1508 SILC_STATUS_ERR_RESOURCE_LIMIT,
1509 "Connection refused");
1510 server->stat.conn_failures++;
1511 silc_free(proto_ctx);
1514 port = server->sockets[(SilcUInt32)proto_ctx->context]->port;
1516 /* Check whether this connection is denied to connect to us. */
1517 deny = silc_server_config_find_denied(server, sock->ip);
1519 deny = silc_server_config_find_denied(server, sock->hostname);
1521 /* The connection is denied */
1522 SILC_LOG_INFO(("Connection %s (%s) is denied",
1523 sock->hostname, sock->ip));
1524 silc_server_disconnect_remote(server, sock,
1525 SILC_STATUS_ERR_BANNED_FROM_SERVER,
1527 server->stat.conn_failures++;
1528 silc_free(proto_ctx);
1532 /* Check whether we have configured this sort of connection at all. We
1533 have to check all configurations since we don't know what type of
1534 connection this is. */
1535 if (!(cconfig = silc_server_config_find_client(server, sock->ip)))
1536 cconfig = silc_server_config_find_client(server, sock->hostname);
1537 if (!(sconfig = silc_server_config_find_server_conn(server, sock->ip)))
1538 sconfig = silc_server_config_find_server_conn(server, sock->hostname);
1539 if (server->server_type == SILC_ROUTER) {
1540 if (!(rconfig = silc_server_config_find_router_conn(server,
1541 sock->ip, sock->port)))
1542 rconfig = silc_server_config_find_router_conn(server, sock->hostname,
1545 if (!cconfig && !sconfig && !rconfig) {
1546 SILC_LOG_INFO(("Connection %s (%s) is not allowed", sock->hostname,
1548 silc_server_disconnect_remote(server, sock,
1549 SILC_STATUS_ERR_BANNED_FROM_SERVER, NULL);
1550 server->stat.conn_failures++;
1551 silc_free(proto_ctx);
1555 /* The connection is allowed */
1557 /* Set internal context for key exchange protocol. This is
1558 sent as context for the protocol. */
1559 proto_ctx->sock = sock;
1560 proto_ctx->rng = server->rng;
1561 proto_ctx->responder = TRUE;
1562 silc_server_config_ref(&proto_ctx->cconfig, server->config, cconfig);
1563 silc_server_config_ref(&proto_ctx->sconfig, server->config, sconfig);
1564 silc_server_config_ref(&proto_ctx->rconfig, server->config, rconfig);
1566 /* Take flags for key exchange. Since we do not know what type of connection
1567 this is, we go through all found configurations and use the global ones
1568 as well. This will result always into strictest key exchange flags. */
1569 SILC_GET_SKE_FLAGS(cconfig, proto_ctx);
1570 SILC_GET_SKE_FLAGS(sconfig, proto_ctx);
1571 SILC_GET_SKE_FLAGS(rconfig, proto_ctx);
1572 if (server->config->param.key_exchange_pfs)
1573 proto_ctx->flags |= SILC_SKE_SP_FLAG_PFS;
1575 /* Prepare the connection for key exchange protocol. We allocate the
1576 protocol but will not start it yet. The connector will be the
1577 initiator of the protocol thus we will wait for initiation from
1578 there before we start the protocol. */
1579 server->stat.auth_attempts++;
1580 SILC_LOG_DEBUG(("Starting key exchange protocol"));
1581 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1582 &sock->protocol, proto_ctx,
1583 silc_server_accept_new_connection_second);
1585 /* Register a timeout task that will be executed if the connector
1586 will not start the key exchange protocol within specified timeout
1587 and the connection will be closed. */
1588 proto_ctx->timeout_task =
1589 silc_schedule_task_add(server->schedule, sock->sock,
1590 silc_server_timeout_remote,
1592 server->config->key_exchange_timeout, 0,
1597 /* Accepts new connections to the server. Accepting new connections are
1598 done in three parts to make it async. */
1600 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
1602 SilcServer server = (SilcServer)context;
1603 SilcSocketConnection newsocket;
1604 SilcServerKEInternalContext *proto_ctx;
1607 SILC_LOG_DEBUG(("Accepting new connection"));
1609 server->stat.conn_attempts++;
1611 sock = silc_net_accept_connection(fd);
1613 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
1614 server->stat.conn_failures++;
1618 /* Check for maximum allowed connections */
1619 if (sock > server->config->param.connections_max) {
1620 SILC_LOG_ERROR(("Refusing connection, server is full"));
1621 server->stat.conn_failures++;
1622 silc_net_close_connection(sock);
1626 /* Set socket options */
1627 silc_net_set_socket_nonblock(sock);
1628 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
1630 /* We don't create a ID yet, since we don't know what type of connection
1631 this is yet. But, we do add the connection to the socket table. */
1632 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
1633 server->sockets[sock] = newsocket;
1635 /* Perform asynchronous host lookup. This will lookup the IP and the
1636 FQDN of the remote connection. After the lookup is done the connection
1637 is accepted further. */
1638 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1639 proto_ctx->server = server;
1640 proto_ctx->context = (void *)fd;
1641 silc_socket_host_lookup(newsocket, TRUE,
1642 silc_server_accept_new_connection_lookup,
1643 (void *)proto_ctx, server->schedule);
1646 /* Second part of accepting new connection. Key exchange protocol has been
1647 performed and now it is time to do little connection authentication
1648 protocol to figure out whether this connection is client or server
1649 and whether it has right to access this server (especially server
1650 connections needs to be authenticated). */
1652 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
1654 SilcProtocol protocol = (SilcProtocol)context;
1655 SilcServerKEInternalContext *ctx =
1656 (SilcServerKEInternalContext *)protocol->context;
1657 SilcServer server = (SilcServer)ctx->server;
1658 SilcSocketConnection sock = ctx->sock;
1659 SilcServerConnAuthInternalContext *proto_ctx;
1661 SILC_LOG_DEBUG(("Start"));
1663 if ((protocol->state == SILC_PROTOCOL_STATE_ERROR) ||
1664 (protocol->state == SILC_PROTOCOL_STATE_FAILURE)) {
1665 /* Error occured during protocol */
1666 SILC_LOG_DEBUG(("Error key exchange protocol"));
1667 silc_protocol_free(protocol);
1668 sock->protocol = NULL;
1669 silc_ske_free_key_material(ctx->keymat);
1671 silc_packet_context_free(ctx->packet);
1673 silc_ske_free(ctx->ske);
1674 silc_free(ctx->dest_id);
1675 silc_server_config_unref(&ctx->cconfig);
1676 silc_server_config_unref(&ctx->sconfig);
1677 silc_server_config_unref(&ctx->rconfig);
1679 silc_server_disconnect_remote(server, sock,
1680 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED,
1682 server->stat.auth_failures++;
1686 /* We now have the key material as the result of the key exchange
1687 protocol. Take the key material into use. Free the raw key material
1688 as soon as we've set them into use. */
1689 if (!silc_server_protocol_ke_set_keys(server, ctx->ske,
1690 ctx->sock, ctx->keymat,
1691 ctx->ske->prop->cipher,
1692 ctx->ske->prop->pkcs,
1693 ctx->ske->prop->hash,
1694 ctx->ske->prop->hmac,
1695 ctx->ske->prop->group,
1697 SILC_LOG_ERROR(("Error setting key material in use"));
1698 silc_protocol_free(protocol);
1699 sock->protocol = NULL;
1700 silc_ske_free_key_material(ctx->keymat);
1702 silc_packet_context_free(ctx->packet);
1704 silc_ske_free(ctx->ske);
1705 silc_free(ctx->dest_id);
1706 silc_server_config_unref(&ctx->cconfig);
1707 silc_server_config_unref(&ctx->sconfig);
1708 silc_server_config_unref(&ctx->rconfig);
1710 silc_server_disconnect_remote(server, sock,
1711 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
1712 server->stat.auth_failures++;
1715 silc_ske_free_key_material(ctx->keymat);
1717 /* Allocate internal context for the authentication protocol. This
1718 is sent as context for the protocol. */
1719 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1720 proto_ctx->server = (void *)server;
1721 proto_ctx->sock = sock;
1722 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
1723 proto_ctx->responder = TRUE;
1724 proto_ctx->dest_id_type = ctx->dest_id_type;
1725 proto_ctx->dest_id = ctx->dest_id;
1726 proto_ctx->cconfig = ctx->cconfig;
1727 proto_ctx->sconfig = ctx->sconfig;
1728 proto_ctx->rconfig = ctx->rconfig;
1730 /* Free old protocol as it is finished now */
1731 silc_protocol_free(protocol);
1733 silc_packet_context_free(ctx->packet);
1735 sock->protocol = NULL;
1737 /* Allocate the authentication protocol. This is allocated here
1738 but we won't start it yet. We will be receiving party of this
1739 protocol thus we will wait that connecting party will make
1740 their first move. */
1741 SILC_LOG_DEBUG(("Starting connection authentication protocol"));
1742 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1743 &sock->protocol, proto_ctx,
1744 silc_server_accept_new_connection_final);
1746 /* Register timeout task. If the protocol is not executed inside
1747 this timelimit the connection will be terminated. */
1748 proto_ctx->timeout_task =
1749 silc_schedule_task_add(server->schedule, sock->sock,
1750 silc_server_timeout_remote,
1752 server->config->conn_auth_timeout, 0,
1757 /* After this is called, server don't wait for backup router anymore.
1758 This gets called automatically even after we have backup router
1759 connection established. */
1761 SILC_TASK_CALLBACK(silc_server_backup_router_wait)
1763 SilcServer server = context;
1764 server->wait_backup = FALSE;
1767 /* Final part of accepting new connection. The connection has now
1768 been authenticated and keys has been exchanged. We also know whether
1769 this is client or server connection. */
1771 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
1773 SilcProtocol protocol = (SilcProtocol)context;
1774 SilcServerConnAuthInternalContext *ctx =
1775 (SilcServerConnAuthInternalContext *)protocol->context;
1776 SilcServer server = (SilcServer)ctx->server;
1777 SilcSocketConnection sock = ctx->sock;
1778 SilcUnknownEntry entry = (SilcUnknownEntry)sock->user_data;
1780 SilcServerConfigConnParams *param = &server->config->param;
1782 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1783 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1784 /* Error occured during protocol */
1785 SILC_LOG_DEBUG(("Error during authentication protocol"));
1786 silc_protocol_free(protocol);
1787 sock->protocol = NULL;
1789 silc_packet_context_free(ctx->packet);
1791 silc_ske_free(ctx->ske);
1792 silc_free(ctx->dest_id);
1793 silc_server_config_unref(&ctx->cconfig);
1794 silc_server_config_unref(&ctx->sconfig);
1795 silc_server_config_unref(&ctx->rconfig);
1797 silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1799 server->stat.auth_failures++;
1803 entry->data.last_receive = time(NULL);
1805 switch (ctx->conn_type) {
1806 case SILC_SOCKET_TYPE_CLIENT:
1808 SilcClientEntry client;
1809 SilcServerConfigClient *conn = ctx->cconfig.ref_ptr;
1811 /* Verify whether this connection is after all allowed to connect */
1812 if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1813 &server->config->param,
1814 conn->param, ctx->ske)) {
1815 server->stat.auth_failures++;
1819 /* If we are primary router and we have backup router configured
1820 but it has not connected to use yet, do not accept any other
1822 if (server->wait_backup && server->server_type == SILC_ROUTER &&
1823 !server->backup_router) {
1824 SilcServerConfigRouter *router;
1825 router = silc_server_config_get_backup_router(server);
1826 if (router && strcmp(server->config->server_info->primary->server_ip,
1828 silc_server_find_socket_by_host(server,
1829 SILC_SOCKET_TYPE_SERVER,
1830 router->backup_replace_ip, 0)) {
1831 SILC_LOG_INFO(("Will not accept connections because we do "
1832 "not have backup router connection established"));
1833 silc_server_disconnect_remote(server, sock,
1834 SILC_STATUS_ERR_PERM_DENIED,
1835 "We do not have connection to backup "
1836 "router established, try later");
1837 silc_free(sock->user_data);
1838 server->stat.auth_failures++;
1840 /* From here on, wait 10 seconds for the backup router to appear. */
1841 silc_schedule_task_add(server->schedule, 0,
1842 silc_server_backup_router_wait,
1843 (void *)server, 10, 0,
1844 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1849 SILC_LOG_DEBUG(("Remote host is client"));
1850 SILC_LOG_INFO(("Connection %s (%s) is client", sock->hostname,
1853 /* Add the client to the client ID cache. The nickname and Client ID
1854 and other information is created after we have received NEW_CLIENT
1855 packet from client. */
1856 client = silc_idlist_add_client(server->local_list,
1857 NULL, NULL, NULL, NULL, NULL, sock, 0);
1859 SILC_LOG_ERROR(("Could not add new client to cache"));
1860 silc_free(sock->user_data);
1861 silc_server_disconnect_remote(server, sock,
1862 SILC_STATUS_ERR_AUTH_FAILED, NULL);
1863 server->stat.auth_failures++;
1866 entry->data.status |= SILC_IDLIST_STATUS_LOCAL;
1869 server->stat.my_clients++;
1870 server->stat.clients++;
1871 server->stat.cell_clients++;
1873 /* Get connection parameters */
1875 param = conn->param;
1877 if (!param->keepalive_secs)
1878 param->keepalive_secs = server->config->param.keepalive_secs;
1880 if (!param->qos && server->config->param.qos) {
1881 param->qos = server->config->param.qos;
1882 param->qos_rate_limit = server->config->param.qos_rate_limit;
1883 param->qos_bytes_limit = server->config->param.qos_bytes_limit;
1884 param->qos_limit_sec = server->config->param.qos_limit_sec;
1885 param->qos_limit_usec = server->config->param.qos_limit_usec;
1888 /* Check if to be anonymous connection */
1889 if (param->anonymous)
1890 client->mode |= SILC_UMODE_ANONYMOUS;
1893 id_entry = (void *)client;
1896 case SILC_SOCKET_TYPE_SERVER:
1897 case SILC_SOCKET_TYPE_ROUTER:
1899 SilcServerEntry new_server;
1900 bool initiator = FALSE;
1901 bool backup_local = FALSE;
1902 bool backup_router = FALSE;
1903 char *backup_replace_ip = NULL;
1904 SilcUInt16 backup_replace_port = 0;
1905 SilcServerConfigServer *sconn = ctx->sconfig.ref_ptr;
1906 SilcServerConfigRouter *rconn = ctx->rconfig.ref_ptr;
1908 /* If we are backup router and this is incoming server connection
1909 and we do not have connection to primary router, do not allow
1911 if (server->server_type == SILC_BACKUP_ROUTER &&
1912 ctx->conn_type == SILC_SOCKET_TYPE_SERVER &&
1913 !SILC_PRIMARY_ROUTE(server)) {
1914 SILC_LOG_INFO(("Will not accept server connection because we do "
1915 "not have primary router connection established"));
1916 silc_server_disconnect_remote(server, sock,
1917 SILC_STATUS_ERR_PERM_DENIED,
1918 "We do not have connection to primary "
1919 "router established, try later");
1920 silc_free(sock->user_data);
1921 server->stat.auth_failures++;
1925 if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1926 /* Verify whether this connection is after all allowed to connect */
1927 if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1928 &server->config->param,
1929 rconn ? rconn->param : NULL,
1931 silc_free(sock->user_data);
1932 server->stat.auth_failures++;
1938 param = rconn->param;
1940 if (!param->keepalive_secs)
1941 param->keepalive_secs = server->config->param.keepalive_secs;
1943 if (!param->qos && server->config->param.qos) {
1944 param->qos = server->config->param.qos;
1945 param->qos_rate_limit = server->config->param.qos_rate_limit;
1946 param->qos_bytes_limit = server->config->param.qos_bytes_limit;
1947 param->qos_limit_sec = server->config->param.qos_limit_sec;
1948 param->qos_limit_usec = server->config->param.qos_limit_usec;
1952 initiator = rconn->initiator;
1953 backup_local = rconn->backup_local;
1954 backup_router = rconn->backup_router;
1955 backup_replace_ip = rconn->backup_replace_ip;
1956 backup_replace_port = rconn->backup_replace_port;
1960 if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER) {
1961 /* Verify whether this connection is after all allowed to connect */
1962 if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1963 &server->config->param,
1964 sconn ? sconn->param : NULL,
1966 silc_free(sock->user_data);
1967 server->stat.auth_failures++;
1972 param = sconn->param;
1974 if (!param->keepalive_secs)
1975 param->keepalive_secs = server->config->param.keepalive_secs;
1977 if (!param->qos && server->config->param.qos) {
1978 param->qos = server->config->param.qos;
1979 param->qos_rate_limit = server->config->param.qos_rate_limit;
1980 param->qos_bytes_limit = server->config->param.qos_bytes_limit;
1981 param->qos_limit_sec = server->config->param.qos_limit_sec;
1982 param->qos_limit_usec = server->config->param.qos_limit_usec;
1986 backup_router = sconn->backup_router;
1990 /* If we are primary router and we have backup router configured
1991 but it has not connected to use yet, do not accept any other
1993 if (server->wait_backup && server->server_type == SILC_ROUTER &&
1994 !server->backup_router && !backup_router) {
1995 SilcServerConfigRouter *router;
1996 router = silc_server_config_get_backup_router(server);
1997 if (router && strcmp(server->config->server_info->primary->server_ip,
1999 silc_server_find_socket_by_host(server,
2000 SILC_SOCKET_TYPE_SERVER,
2001 router->backup_replace_ip, 0)) {
2002 SILC_LOG_INFO(("Will not accept connections because we do "
2003 "not have backup router connection established"));
2004 silc_server_disconnect_remote(server, sock,
2005 SILC_STATUS_ERR_PERM_DENIED,
2006 "We do not have connection to backup "
2007 "router established, try later");
2008 silc_free(sock->user_data);
2009 server->stat.auth_failures++;
2011 /* From here on, wait 10 seconds for the backup router to appear. */
2012 silc_schedule_task_add(server->schedule, 0,
2013 silc_server_backup_router_wait,
2014 (void *)server, 10, 0,
2015 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
2020 SILC_LOG_DEBUG(("Remote host is %s",
2021 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
2022 "server" : (backup_router ?
2023 "backup router" : "router")));
2024 SILC_LOG_INFO(("Connection %s (%s) is %s", sock->hostname,
2025 sock->ip, ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
2026 "server" : (backup_router ?
2027 "backup router" : "router")));
2029 /* Add the server into server cache. The server name and Server ID
2030 is updated after we have received NEW_SERVER packet from the
2031 server. We mark ourselves as router for this server if we really
2034 silc_idlist_add_server((ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
2035 server->local_list : (backup_router ?
2036 server->local_list :
2037 server->global_list)),
2039 (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
2040 SILC_SERVER : SILC_ROUTER),
2042 (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
2043 server->id_entry : (backup_router ?
2044 server->id_entry : NULL)),
2047 SILC_LOG_ERROR(("Could not add new server to cache"));
2048 silc_free(sock->user_data);
2049 silc_server_disconnect_remote(server, sock,
2050 SILC_STATUS_ERR_AUTH_FAILED, NULL);
2051 server->stat.auth_failures++;
2054 entry->data.status |= SILC_IDLIST_STATUS_LOCAL;
2056 id_entry = (void *)new_server;
2058 /* If the incoming connection is router and marked as backup router
2059 then add it to be one of our backups */
2060 if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER && backup_router) {
2061 /* Change it back to SERVER type since that's what it really is. */
2063 ctx->conn_type = SILC_SOCKET_TYPE_SERVER;
2064 new_server->server_type = SILC_BACKUP_ROUTER;
2066 SILC_SERVER_SEND_OPERS(server, FALSE, TRUE, SILC_NOTIFY_TYPE_NONE,
2067 ("Backup router %s is now online",
2070 /* Remove the backup waiting with timeout */
2071 silc_schedule_task_add(server->schedule, 0,
2072 silc_server_backup_router_wait,
2073 (void *)server, 5, 0,
2074 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
2078 if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER) {
2079 server->stat.my_servers++;
2081 server->stat.my_routers++;
2082 server->stat.routers++;
2084 server->stat.servers++;
2086 /* Check whether this connection is to be our primary router connection
2087 if we do not already have the primary route. */
2088 if (!backup_router &&
2089 server->standalone && ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
2090 if (silc_server_config_is_primary_route(server) && !initiator)
2093 SILC_LOG_DEBUG(("We are not standalone server anymore"));
2094 server->standalone = FALSE;
2095 if (!server->id_entry->router) {
2096 server->id_entry->router = id_entry;
2097 server->router = id_entry;
2108 sock->type = ctx->conn_type;
2110 /* Add the common data structure to the ID entry. */
2111 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
2113 /* Add to sockets internal pointer for fast referencing */
2114 silc_free(sock->user_data);
2115 sock->user_data = id_entry;
2117 /* Connection has been fully established now. Everything is ok. */
2118 SILC_LOG_DEBUG(("New connection authenticated"));
2120 /* Perform keepalive. */
2121 if (param->keepalive_secs)
2122 silc_socket_set_heartbeat(sock, param->keepalive_secs, server,
2123 silc_server_perform_heartbeat,
2126 /* Perform Quality of Service */
2128 silc_socket_set_qos(sock, param->qos_rate_limit, param->qos_bytes_limit,
2129 param->qos_limit_sec, param->qos_limit_usec,
2133 silc_protocol_free(protocol);
2135 silc_packet_context_free(ctx->packet);
2137 silc_ske_free(ctx->ske);
2138 silc_free(ctx->dest_id);
2139 silc_server_config_unref(&ctx->cconfig);
2140 silc_server_config_unref(&ctx->sconfig);
2141 silc_server_config_unref(&ctx->rconfig);
2143 sock->protocol = NULL;
2146 /* This function is used to read packets from network and send packets to
2147 network. This is usually a generic task. */
2149 SILC_TASK_CALLBACK(silc_server_packet_process)
2151 SilcServer server = (SilcServer)context;
2152 SilcSocketConnection sock = server->sockets[fd];
2153 SilcIDListData idata;
2154 SilcCipher cipher = NULL;
2155 SilcHmac hmac = NULL;
2156 SilcUInt32 sequence = 0;
2157 bool local_is_router;
2161 SILC_LOG_DEBUG(("Unknown socket connection"));
2165 /* Packet sending */
2167 if (type == SILC_TASK_WRITE) {
2168 /* Do not send data to disconnected connection */
2169 if (SILC_IS_DISCONNECTED(sock)) {
2170 SILC_LOG_DEBUG(("Disconnected socket connection, cannot send"));
2174 server->stat.packets_sent++;
2176 /* Send the packet */
2177 ret = silc_packet_send(sock, TRUE);
2179 /* If returned -2 could not write to connection now, will do
2184 /* The packet has been sent and now it is time to set the connection
2185 back to only for input. When there is again some outgoing data
2186 available for this connection it will be set for output as well.
2187 This call clears the output setting and sets it only for input. */
2188 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, fd);
2189 SILC_UNSET_OUTBUF_PENDING(sock);
2190 silc_buffer_clear(sock->outbuf);
2193 SILC_LOG_ERROR(("Error sending packet to connection "
2194 "%s:%d [%s]", sock->hostname, sock->port,
2195 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2196 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2197 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2200 SILC_SET_DISCONNECTING(sock);
2201 if (sock->user_data)
2202 silc_server_free_sock_user_data(server, sock, NULL);
2203 silc_server_close_connection(server, sock);
2208 /* Packet receiving */
2210 /* Read some data from connection */
2211 ret = silc_packet_receive(sock);
2215 SILC_LOG_ERROR(("Error receiving packet from connection "
2216 "%s:%d [%s] %s", sock->hostname, sock->port,
2217 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2218 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2219 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2220 "Router"), strerror(errno)));
2222 SILC_SET_DISCONNECTING(sock);
2223 if (sock->user_data)
2224 silc_server_free_sock_user_data(server, sock, NULL);
2225 silc_server_close_connection(server, sock);
2232 SILC_LOG_DEBUG(("Read EOF"));
2234 /* If connection is disconnecting already we will finally
2235 close the connection */
2236 if (SILC_IS_DISCONNECTING(sock)) {
2237 if (sock->user_data)
2238 silc_server_free_sock_user_data(server, sock, NULL);
2239 silc_server_close_connection(server, sock);
2243 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
2244 SILC_SET_DISCONNECTING(sock);
2246 if (sock->user_data) {
2248 if (silc_socket_get_error(sock, tmp, sizeof(tmp) - 1))
2249 silc_server_free_sock_user_data(server, sock, tmp);
2251 silc_server_free_sock_user_data(server, sock, NULL);
2252 } else if (server->router_conn && server->router_conn->sock == sock &&
2253 !server->router && server->standalone) {
2254 silc_server_create_connections(server);
2257 silc_server_close_connection(server, sock);
2261 /* If connection is disconnecting or disconnected we will ignore
2263 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
2264 SILC_LOG_DEBUG(("Ignoring read data from disconnected connection"));
2268 /* Get keys and stuff from ID entry */
2269 idata = (SilcIDListData)sock->user_data;
2271 cipher = idata->receive_key;
2272 hmac = idata->hmac_receive;
2273 sequence = idata->psn_receive;
2276 /* Then, process the packet. This will call the parser that will then
2277 decrypt and parse the packet. */
2279 local_is_router = (server->server_type == SILC_ROUTER);
2281 /* If socket connection is our primary, we are backup and we are doing
2282 backup resuming, we won't process the packet as being a router
2283 (affects channel message decryption). */
2284 if (server->backup_router && SILC_SERVER_IS_BACKUP(sock) &&
2285 SILC_PRIMARY_ROUTE(server) == sock)
2286 local_is_router = FALSE;
2288 ret = silc_packet_receive_process(sock, local_is_router,
2289 cipher, hmac, sequence,
2290 silc_server_packet_parse, server);
2292 /* If processing failed the connection is closed. */
2294 /* On packet processing errors we may close our primary router
2295 connection but won't become primary router if we are the backup
2296 since this is local error condition. */
2297 if (SILC_PRIMARY_ROUTE(server) == sock && server->backup_router)
2298 server->backup_noswitch = TRUE;
2300 SILC_SET_DISCONNECTING(sock);
2301 if (sock->user_data)
2302 silc_server_free_sock_user_data(server, sock, NULL);
2303 silc_server_close_connection(server, sock);
2307 /* Parses whole packet, received earlier. */
2309 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
2311 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
2312 SilcServer server = (SilcServer)parse_ctx->context;
2313 SilcSocketConnection sock = parse_ctx->sock;
2314 SilcPacketContext *packet = parse_ctx->packet;
2315 SilcIDListData idata = (SilcIDListData)sock->user_data;
2318 server->stat.packets_received++;
2320 /* Parse the packet */
2321 if (parse_ctx->normal)
2322 ret = silc_packet_parse(packet, idata ? idata->receive_key : NULL);
2324 ret = silc_packet_parse_special(packet, idata ? idata->receive_key : NULL);
2326 /* If entry is disabled ignore what we got. */
2327 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED &&
2328 ret != SILC_PACKET_HEARTBEAT && ret != SILC_PACKET_RESUME_ROUTER &&
2329 ret != SILC_PACKET_REKEY && ret != SILC_PACKET_REKEY_DONE) {
2330 SILC_LOG_DEBUG(("Connection is disabled"));
2334 if (ret == SILC_PACKET_NONE) {
2335 SILC_LOG_DEBUG(("Error parsing packet"));
2339 /* Check that the the current client ID is same as in the client's packet. */
2340 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
2341 SilcClientEntry client = (SilcClientEntry)sock->user_data;
2342 if (client && client->id && packet->src_id) {
2343 void *id = silc_id_str2id(packet->src_id, packet->src_id_len,
2344 packet->src_id_type);
2345 if (!id || !SILC_ID_CLIENT_COMPARE(client->id, id)) {
2347 SILC_LOG_DEBUG(("Packet source is not same as sender"));
2354 if (server->server_type == SILC_ROUTER) {
2355 /* Route the packet if it is not destined to us. Other ID types but
2356 server are handled separately after processing them. */
2357 if (packet->dst_id && !(packet->flags & SILC_PACKET_FLAG_BROADCAST) &&
2358 packet->dst_id_type == SILC_ID_SERVER &&
2359 sock->type != SILC_SOCKET_TYPE_CLIENT &&
2360 memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
2362 /* Route the packet to fastest route for the destination ID */
2363 void *id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
2364 packet->dst_id_type);
2367 silc_server_packet_route(server,
2368 silc_server_route_get(server, id,
2369 packet->dst_id_type),
2376 /* Parse the incoming packet type */
2377 silc_server_packet_parse_type(server, sock, packet);
2379 /* Broadcast packet if it is marked as broadcast packet and it is
2380 originated from router and we are router. */
2381 if (server->server_type == SILC_ROUTER &&
2382 sock->type == SILC_SOCKET_TYPE_ROUTER &&
2383 packet->flags & SILC_PACKET_FLAG_BROADCAST) {
2384 /* Broadcast to our primary route */
2385 silc_server_packet_broadcast(server, SILC_PRIMARY_ROUTE(server), packet);
2387 /* If we have backup routers then we need to feed all broadcast
2388 data to those servers. */
2389 silc_server_backup_broadcast(server, sock, packet);
2393 silc_packet_context_free(packet);
2394 silc_free(parse_ctx);
2397 /* Parser callback called by silc_packet_receive_process. This merely
2398 registers timeout that will handle the actual parsing when appropriate. */
2400 bool silc_server_packet_parse(SilcPacketParserContext *parser_context,
2403 SilcServer server = (SilcServer)context;
2404 SilcSocketConnection sock = parser_context->sock;
2405 SilcIDListData idata = (SilcIDListData)sock->user_data;
2409 idata->psn_receive = parser_context->packet->sequence + 1;
2411 /* If protocol for this connection is key exchange or rekey then we'll
2412 process all packets synchronously, since there might be packets in
2413 queue that we are not able to decrypt without first processing the
2414 packets before them. */
2415 if (sock->protocol && sock->protocol->protocol &&
2416 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
2417 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
2418 silc_server_packet_parse_real(server->schedule, server, 0, sock->sock,
2421 /* Reprocess data since we'll return FALSE here. This is because
2422 the idata->receive_key might have become valid in the last packet
2423 and we want to call this processor with valid cipher. */
2425 ret = silc_packet_receive_process(
2426 sock, server->server_type == SILC_ROUTER,
2428 idata->hmac_receive, idata->psn_receive,
2429 silc_server_packet_parse, server);
2431 ret = silc_packet_receive_process(
2432 sock, server->server_type == SILC_ROUTER,
2434 silc_server_packet_parse, server);
2437 /* On packet processing errors we may close our primary router
2438 connection but won't become primary router if we are the backup
2439 since this is local error condition. */
2440 if (SILC_PRIMARY_ROUTE(server) == sock && server->backup_router)
2441 server->backup_noswitch = TRUE;
2443 SILC_SET_DISCONNECTING(sock);
2444 if (sock->user_data)
2445 silc_server_free_sock_user_data(server, sock, NULL);
2446 silc_server_close_connection(server, sock);
2452 switch (sock->type) {
2453 case SILC_SOCKET_TYPE_UNKNOWN:
2454 case SILC_SOCKET_TYPE_CLIENT:
2455 /* Parse the packet with timeout */
2456 silc_schedule_task_add(server->schedule, sock->sock,
2457 silc_server_packet_parse_real,
2458 (void *)parser_context, 0, 100000,
2460 SILC_TASK_PRI_NORMAL);
2462 case SILC_SOCKET_TYPE_SERVER:
2463 case SILC_SOCKET_TYPE_ROUTER:
2464 /* Packets from servers are parsed immediately */
2465 silc_server_packet_parse_real(server->schedule, server, 0, sock->sock,
2475 /* Parses the packet type and calls what ever routines the packet type
2476 requires. This is done for all incoming packets. */
2478 void silc_server_packet_parse_type(SilcServer server,
2479 SilcSocketConnection sock,
2480 SilcPacketContext *packet)
2482 SilcPacketType type = packet->type;
2483 SilcIDListData idata = (SilcIDListData)sock->user_data;
2485 SILC_LOG_DEBUG(("Received %s packet [flags %d]",
2486 silc_get_packet_name(type), packet->flags));
2488 /* Parse the packet type */
2490 case SILC_PACKET_DISCONNECT:
2493 char *message = NULL;
2495 if (packet->flags & SILC_PACKET_FLAG_LIST)
2497 if (packet->buffer->len < 1)
2500 status = (SilcStatus)packet->buffer->data[0];
2501 if (packet->buffer->len > 1 &&
2502 silc_utf8_valid(packet->buffer->data + 1, packet->buffer->len - 1))
2503 message = silc_memdup(packet->buffer->data + 1,
2504 packet->buffer->len - 1);
2506 SILC_LOG_INFO(("Disconnected by %s (%s): %s (%d) %s",
2507 sock->ip, sock->hostname,
2508 silc_get_status_message(status), status,
2509 message ? message : ""));
2512 /* Do not switch to backup in case of error */
2513 server->backup_noswitch = (status == SILC_STATUS_OK ? FALSE : TRUE);
2515 /* Handle the disconnection from our end too */
2516 SILC_SET_DISCONNECTING(sock);
2517 if (sock->user_data && SILC_IS_LOCAL(sock->user_data))
2518 silc_server_free_sock_user_data(server, sock, NULL);
2519 silc_server_close_connection(server, sock);
2520 server->backup_noswitch = FALSE;
2524 case SILC_PACKET_SUCCESS:
2526 * Success received for something. For now we can have only
2527 * one protocol for connection executing at once hence this
2528 * success message is for whatever protocol is executing currently.
2530 if (packet->flags & SILC_PACKET_FLAG_LIST)
2533 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2536 case SILC_PACKET_FAILURE:
2538 * Failure received for something. For now we can have only
2539 * one protocol for connection executing at once hence this
2540 * failure message is for whatever protocol is executing currently.
2542 if (packet->flags & SILC_PACKET_FLAG_LIST)
2544 if (sock->protocol) {
2545 sock->protocol->state = SILC_PROTOCOL_STATE_FAILURE;
2546 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2550 case SILC_PACKET_REJECT:
2551 if (packet->flags & SILC_PACKET_FLAG_LIST)
2556 case SILC_PACKET_NOTIFY:
2558 * Received notify packet. Server can receive notify packets from
2559 * router. Server then relays the notify messages to clients if needed.
2561 if (packet->flags & SILC_PACKET_FLAG_LIST)
2562 silc_server_notify_list(server, sock, packet);
2564 silc_server_notify(server, sock, packet);
2570 case SILC_PACKET_CHANNEL_MESSAGE:
2572 * Received channel message. Channel messages are special packets
2573 * (although probably most common ones) thus they are handled
2576 if (packet->flags & SILC_PACKET_FLAG_LIST)
2578 idata->last_receive = time(NULL);
2579 silc_server_channel_message(server, sock, packet);
2582 case SILC_PACKET_CHANNEL_KEY:
2584 * Received key for channel. As channels are created by the router
2585 * the keys are as well. We will distribute the key to all of our
2586 * locally connected clients on the particular channel. Router
2587 * never receives this channel and thus is ignored.
2589 if (packet->flags & SILC_PACKET_FLAG_LIST)
2591 silc_server_channel_key(server, sock, packet);
2597 case SILC_PACKET_COMMAND:
2599 * Recived command. Processes the command request and allocates the
2600 * command context and calls the command.
2602 if (packet->flags & SILC_PACKET_FLAG_LIST)
2604 silc_server_command_process(server, sock, packet);
2607 case SILC_PACKET_COMMAND_REPLY:
2609 * Received command reply packet. Received command reply to command. It
2610 * may be reply to command sent by us or reply to command sent by client
2611 * that we've routed further.
2613 if (packet->flags & SILC_PACKET_FLAG_LIST)
2615 silc_server_command_reply(server, sock, packet);
2619 * Private Message packets
2621 case SILC_PACKET_PRIVATE_MESSAGE:
2623 * Received private message packet. The packet is coming from either
2626 if (packet->flags & SILC_PACKET_FLAG_LIST)
2628 idata->last_receive = time(NULL);
2629 silc_server_private_message(server, sock, packet);
2632 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
2634 * Private message key packet.
2636 if (packet->flags & SILC_PACKET_FLAG_LIST)
2638 silc_server_private_message_key(server, sock, packet);
2642 * Key Exchange protocol packets
2644 case SILC_PACKET_KEY_EXCHANGE:
2645 if (packet->flags & SILC_PACKET_FLAG_LIST)
2648 if (sock->protocol && sock->protocol->protocol &&
2649 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
2650 SilcServerKEInternalContext *proto_ctx =
2651 (SilcServerKEInternalContext *)sock->protocol->context;
2653 proto_ctx->packet = silc_packet_context_dup(packet);
2655 /* Let the protocol handle the packet */
2656 silc_protocol_execute(sock->protocol, server->schedule, 0, 100000);
2658 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
2659 "protocol active, packet dropped."));
2663 case SILC_PACKET_KEY_EXCHANGE_1:
2664 if (packet->flags & SILC_PACKET_FLAG_LIST)
2667 if (sock->protocol && sock->protocol->protocol &&
2668 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
2669 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
2671 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2672 SilcServerRekeyInternalContext *proto_ctx =
2673 (SilcServerRekeyInternalContext *)sock->protocol->context;
2675 if (proto_ctx->packet)
2676 silc_packet_context_free(proto_ctx->packet);
2678 proto_ctx->packet = silc_packet_context_dup(packet);
2680 /* Let the protocol handle the packet */
2681 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2683 SilcServerKEInternalContext *proto_ctx =
2684 (SilcServerKEInternalContext *)sock->protocol->context;
2686 if (proto_ctx->packet)
2687 silc_packet_context_free(proto_ctx->packet);
2689 proto_ctx->packet = silc_packet_context_dup(packet);
2690 proto_ctx->dest_id_type = packet->src_id_type;
2691 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2692 packet->src_id_type);
2693 if (!proto_ctx->dest_id)
2696 /* Let the protocol handle the packet */
2697 silc_protocol_execute(sock->protocol, server->schedule,
2701 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
2702 "protocol active, packet dropped."));
2706 case SILC_PACKET_KEY_EXCHANGE_2:
2707 if (packet->flags & SILC_PACKET_FLAG_LIST)
2710 if (sock->protocol && sock->protocol->protocol &&
2711 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
2712 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
2714 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2715 SilcServerRekeyInternalContext *proto_ctx =
2716 (SilcServerRekeyInternalContext *)sock->protocol->context;
2718 if (proto_ctx->packet)
2719 silc_packet_context_free(proto_ctx->packet);
2721 proto_ctx->packet = silc_packet_context_dup(packet);
2723 /* Let the protocol handle the packet */
2724 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2726 SilcServerKEInternalContext *proto_ctx =
2727 (SilcServerKEInternalContext *)sock->protocol->context;
2729 if (proto_ctx->packet)
2730 silc_packet_context_free(proto_ctx->packet);
2732 proto_ctx->packet = silc_packet_context_dup(packet);
2733 proto_ctx->dest_id_type = packet->src_id_type;
2734 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2735 packet->src_id_type);
2736 if (!proto_ctx->dest_id)
2739 /* Let the protocol handle the packet */
2740 silc_protocol_execute(sock->protocol, server->schedule,
2744 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
2745 "protocol active, packet dropped."));
2749 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
2751 * Connection authentication request packet. When we receive this packet
2752 * we will send to the other end information about our mandatory
2753 * authentication method for the connection. This packet maybe received
2756 if (packet->flags & SILC_PACKET_FLAG_LIST)
2758 silc_server_connection_auth_request(server, sock, packet);
2762 * Connection Authentication protocol packets
2764 case SILC_PACKET_CONNECTION_AUTH:
2765 /* Start of the authentication protocol. We receive here the
2766 authentication data and will verify it. */
2767 if (packet->flags & SILC_PACKET_FLAG_LIST)
2770 if (sock->protocol && sock->protocol->protocol->type
2771 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
2773 SilcServerConnAuthInternalContext *proto_ctx =
2774 (SilcServerConnAuthInternalContext *)sock->protocol->context;
2776 proto_ctx->packet = silc_packet_context_dup(packet);
2778 /* Let the protocol handle the packet */
2779 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2781 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
2782 "protocol active, packet dropped."));
2786 case SILC_PACKET_NEW_ID:
2788 * Received New ID packet. This includes some new ID that has been
2789 * created. It may be for client, server or channel. This is the way
2790 * to distribute information about new registered entities in the
2793 if (packet->flags & SILC_PACKET_FLAG_LIST)
2794 silc_server_new_id_list(server, sock, packet);
2796 silc_server_new_id(server, sock, packet);
2799 case SILC_PACKET_NEW_CLIENT:
2801 * Received new client packet. This includes client information that
2802 * we will use to create initial client ID. After creating new
2803 * ID we will send it to the client.
2805 if (packet->flags & SILC_PACKET_FLAG_LIST)
2807 silc_server_new_client(server, sock, packet);
2810 case SILC_PACKET_NEW_SERVER:
2812 * Received new server packet. This includes Server ID and some other
2813 * information that we may save. This is received after server has
2816 if (packet->flags & SILC_PACKET_FLAG_LIST)
2818 silc_server_new_server(server, sock, packet);
2821 case SILC_PACKET_NEW_CHANNEL:
2823 * Received new channel packet. Information about new channel in the
2824 * network are distributed using this packet.
2826 if (packet->flags & SILC_PACKET_FLAG_LIST)
2827 silc_server_new_channel_list(server, sock, packet);
2829 silc_server_new_channel(server, sock, packet);
2832 case SILC_PACKET_HEARTBEAT:
2834 * Received heartbeat.
2836 if (packet->flags & SILC_PACKET_FLAG_LIST)
2840 case SILC_PACKET_KEY_AGREEMENT:
2842 * Received heartbeat.
2844 if (packet->flags & SILC_PACKET_FLAG_LIST)
2846 silc_server_key_agreement(server, sock, packet);
2849 case SILC_PACKET_REKEY:
2851 * Received re-key packet. The sender wants to regenerate the session
2854 if (packet->flags & SILC_PACKET_FLAG_LIST)
2856 silc_server_rekey(server, sock, packet);
2859 case SILC_PACKET_REKEY_DONE:
2861 * The re-key is done.
2863 if (packet->flags & SILC_PACKET_FLAG_LIST)
2866 if (sock->protocol && sock->protocol->protocol &&
2867 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2869 SilcServerRekeyInternalContext *proto_ctx =
2870 (SilcServerRekeyInternalContext *)sock->protocol->context;
2872 if (proto_ctx->packet)
2873 silc_packet_context_free(proto_ctx->packet);
2875 proto_ctx->packet = silc_packet_context_dup(packet);
2877 /* Let the protocol handle the packet */
2878 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2880 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
2881 "protocol active, packet dropped."));
2885 case SILC_PACKET_FTP:
2887 if (packet->flags & SILC_PACKET_FLAG_LIST)
2889 silc_server_ftp(server, sock, packet);
2892 case SILC_PACKET_RESUME_CLIENT:
2894 if (packet->flags & SILC_PACKET_FLAG_LIST)
2896 silc_server_resume_client(server, sock, packet);
2899 case SILC_PACKET_RESUME_ROUTER:
2900 /* Resume router packet received. This packet is received for backup
2901 router resuming protocol. */
2902 if (packet->flags & SILC_PACKET_FLAG_LIST)
2904 silc_server_backup_resume_router(server, sock, packet);
2908 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
2913 /* Creates connection to a remote router. */
2915 void silc_server_create_connection(SilcServer server,
2916 const char *remote_host, SilcUInt32 port)
2918 SilcServerConnection sconn;
2920 /* Allocate connection object for hold connection specific stuff. */
2921 sconn = silc_calloc(1, sizeof(*sconn));
2922 sconn->remote_host = strdup(remote_host);
2923 sconn->remote_port = port;
2924 sconn->no_reconnect = TRUE;
2926 silc_schedule_task_add(server->schedule, 0,
2927 silc_server_connect_router,
2928 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
2929 SILC_TASK_PRI_NORMAL);
2932 SILC_TASK_CALLBACK(silc_server_close_connection_final)
2934 SilcServer server = app_context;
2935 SilcSocketConnection sock = context;
2937 /* Close the actual connection */
2938 silc_net_close_connection(sock->sock);
2940 /* We won't listen for this connection anymore */
2941 silc_schedule_task_del_by_fd(server->schedule, sock->sock);
2942 silc_schedule_unset_listen_fd(server->schedule, sock->sock);
2944 silc_socket_free(sock);
2947 /* Closes connection to socket connection */
2949 void silc_server_close_connection(SilcServer server,
2950 SilcSocketConnection sock)
2954 if (!server->sockets[sock->sock] && SILC_IS_DISCONNECTED(sock)) {
2955 silc_schedule_task_add(server->schedule, sock->sock,
2956 silc_server_close_connection_final,
2957 (void *)sock, 0, 1, SILC_TASK_TIMEOUT,
2958 SILC_TASK_PRI_NORMAL);
2962 memset(tmp, 0, sizeof(tmp));
2963 silc_socket_get_error(sock, tmp, sizeof(tmp));
2964 SILC_LOG_INFO(("Closing connection %s:%d [%s] %s", sock->hostname,
2966 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2967 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2968 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2969 "Router"), tmp[0] ? tmp : ""));
2971 /* Unregister all tasks */
2972 silc_schedule_task_del_by_fd(server->schedule, sock->sock);
2974 server->sockets[sock->sock] = NULL;
2976 /* If sock->user_data is NULL then we'll check for active protocols
2977 here since the silc_server_free_sock_user_data has not been called
2978 for this connection. */
2979 if (!sock->user_data) {
2980 /* If any protocol is active cancel its execution. It will call
2981 the final callback which will finalize the disconnection. */
2982 if (sock->protocol && sock->protocol->protocol &&
2983 sock->protocol->protocol->type != SILC_PROTOCOL_SERVER_BACKUP) {
2984 SILC_LOG_DEBUG(("Cancelling protocol, calling final callback"));
2985 silc_protocol_cancel(sock->protocol, server->schedule);
2986 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2987 silc_protocol_execute_final(sock->protocol, server->schedule);
2988 sock->protocol = NULL;
2993 silc_schedule_task_add(server->schedule, sock->sock,
2994 silc_server_close_connection_final,
2995 (void *)sock, 0, 1, SILC_TASK_TIMEOUT,
2996 SILC_TASK_PRI_NORMAL);
2999 /* Sends disconnect message to remote connection and disconnects the
3002 void silc_server_disconnect_remote(SilcServer server,
3003 SilcSocketConnection sock,
3004 SilcStatus status, ...)
3007 unsigned char buf[512];
3015 if (SILC_IS_DISCONNECTED(sock)) {
3016 silc_server_close_connection(server, sock);
3020 memset(buf, 0, sizeof(buf));
3021 va_start(ap, status);
3022 cp = va_arg(ap, char *);
3024 vsnprintf(buf, sizeof(buf) - 1, cp, ap);
3029 SILC_LOG_DEBUG(("Disconnecting remote host"));
3031 /* Notify remote end that the conversation is over. The notify message
3032 is tried to be sent immediately. */
3036 len += silc_utf8_encoded_len(buf, strlen(buf), SILC_STRING_ASCII);
3038 buffer = silc_buffer_alloc_size(len);
3042 buffer->data[0] = status;
3044 silc_utf8_encode(buf, strlen(buf), SILC_STRING_ASCII, buffer->data + 1,
3046 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
3047 buffer->data, buffer->len, TRUE);
3048 silc_buffer_free(buffer);
3051 silc_server_packet_queue_purge(server, sock);
3053 /* Mark the connection to be disconnected */
3054 SILC_SET_DISCONNECTED(sock);
3055 silc_server_close_connection(server, sock);
3058 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout)
3060 SilcServer server = app_context;
3061 SilcClientEntry client = context;
3063 assert(!silc_hash_table_count(client->channels));
3065 silc_idlist_del_data(client);
3066 silc_idcache_purge_by_context(server->local_list->clients, client);
3069 /* Frees client data and notifies about client's signoff. */
3071 void silc_server_free_client_data(SilcServer server,
3072 SilcSocketConnection sock,
3073 SilcClientEntry client,
3075 const char *signoff)
3077 SILC_LOG_DEBUG(("Freeing client data"));
3079 /* If there is pending outgoing data for the client then purge it
3080 to the network before removing the client entry. */
3081 silc_server_packet_queue_purge(server, sock);
3084 /* Check if anyone is watching this nickname */
3085 if (server->server_type == SILC_ROUTER)
3086 silc_server_check_watcher_list(server, client, NULL,
3087 SILC_NOTIFY_TYPE_SIGNOFF);
3089 /* Send SIGNOFF notify to routers. */
3091 silc_server_send_notify_signoff(server, SILC_PRIMARY_ROUTE(server),
3092 SILC_BROADCAST(server), client->id,
3096 /* Remove client from all channels */
3098 silc_server_remove_from_channels(server, NULL, client,
3099 TRUE, (char *)signoff, TRUE, FALSE);
3101 silc_server_remove_from_channels(server, NULL, client,
3102 FALSE, NULL, FALSE, FALSE);
3104 /* Remove this client from watcher list if it is */
3105 silc_server_del_from_watcher_list(server, client);
3107 /* Update statistics */
3108 server->stat.my_clients--;
3109 server->stat.clients--;
3110 if (server->stat.cell_clients)
3111 server->stat.cell_clients--;
3112 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
3113 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
3114 silc_schedule_task_del_by_context(server->schedule, client);
3116 /* We will not delete the client entry right away. We will take it
3117 into history (for WHOWAS command) for 5 minutes, unless we're
3118 shutting down server. */
3119 if (!server->server_shutdown) {
3120 silc_schedule_task_add(server->schedule, 0,
3121 silc_server_free_client_data_timeout,
3123 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
3124 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
3125 client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
3127 client->router = NULL;
3128 client->connection = NULL;
3130 /* Delete directly since we're shutting down server */
3131 silc_idlist_del_data(client);
3132 silc_idlist_del_client(server->local_list, client);
3136 /* Frees user_data pointer from socket connection object. This also sends
3137 appropriate notify packets to the network to inform about leaving
3140 void silc_server_free_sock_user_data(SilcServer server,
3141 SilcSocketConnection sock,
3142 const char *signoff_message)
3144 switch (sock->type) {
3145 case SILC_SOCKET_TYPE_CLIENT:
3147 SilcClientEntry user_data = (SilcClientEntry)sock->user_data;
3148 silc_server_free_client_data(server, sock, user_data, TRUE,
3152 case SILC_SOCKET_TYPE_SERVER:
3153 case SILC_SOCKET_TYPE_ROUTER:
3155 SilcServerEntry user_data = (SilcServerEntry)sock->user_data;
3156 SilcServerEntry backup_router = NULL;
3158 SILC_LOG_DEBUG(("Freeing server data"));
3161 backup_router = silc_server_backup_get(server, user_data->id);
3163 if (!server->backup_router && server->server_type == SILC_ROUTER &&
3164 backup_router == server->id_entry &&
3165 sock->type != SILC_SOCKET_TYPE_ROUTER)
3166 backup_router = NULL;
3168 if (server->server_shutdown || server->backup_noswitch)
3169 backup_router = NULL;
3171 /* If this was our primary router connection then we're lost to
3172 the outside world. */
3173 if (server->router == user_data) {
3174 /* Check whether we have a backup router connection */
3175 if (!backup_router || backup_router == user_data) {
3176 silc_server_create_connections(server);
3177 server->id_entry->router = NULL;
3178 server->router = NULL;
3179 server->standalone = TRUE;
3180 server->backup_primary = FALSE;
3181 backup_router = NULL;
3183 if (server->id_entry != backup_router) {
3184 SILC_LOG_INFO(("New primary router is backup router %s",
3185 backup_router->server_name));
3186 server->id_entry->router = backup_router;
3187 server->router = backup_router;
3188 server->router_connect = time(0);
3189 server->backup_primary = TRUE;
3191 SILC_LOG_INFO(("We are now new primary router in this cell"));
3192 server->id_entry->router = NULL;
3193 server->router = NULL;
3194 server->standalone = TRUE;
3196 /* We stop here to take a breath */
3200 if (server->server_type == SILC_BACKUP_ROUTER) {
3201 server->server_type = SILC_ROUTER;
3203 /* We'll need to constantly try to reconnect to the primary
3204 router so that we'll see when it comes back online. */
3205 silc_server_backup_reconnect(server, sock->ip, sock->port,
3206 silc_server_backup_connected,
3210 /* Mark this connection as replaced */
3211 silc_server_backup_replaced_add(server, user_data->id,
3214 } else if (backup_router) {
3215 SILC_LOG_INFO(("Enabling the use of backup router %s",
3216 backup_router->server_name));
3218 /* Mark this connection as replaced */
3219 silc_server_backup_replaced_add(server, user_data->id,
3221 } else if (server->server_type == SILC_SERVER &&
3222 sock->type == SILC_SOCKET_TYPE_ROUTER) {
3223 /* Reconnect to the router (backup) */
3224 silc_server_create_connections(server);
3227 if (user_data->server_name)
3228 SILC_SERVER_SEND_OPERS(server, FALSE, TRUE, SILC_NOTIFY_TYPE_NONE,
3229 ("Server %s signoff", user_data->server_name));
3231 if (!backup_router) {
3232 /* Remove all servers that are originated from this server, and
3233 remove the clients of those servers too. */
3234 silc_server_remove_servers_by_server(server, user_data, TRUE);
3237 /* Remove the clients that this server owns as they will become
3238 invalid now too. For backup router the server is actually
3239 coming from the primary router, so mark that as the owner
3241 if (server->server_type == SILC_BACKUP_ROUTER &&
3242 sock->type == SILC_SOCKET_TYPE_SERVER)
3243 silc_server_remove_clients_by_server(server, server->router,
3247 silc_server_remove_clients_by_server(server, user_data,
3250 /* Remove channels owned by this server */
3251 if (server->server_type == SILC_SERVER)
3252 silc_server_remove_channels_by_server(server, user_data);
3254 /* Enable local server connections that may be disabled */
3255 silc_server_local_servers_toggle_enabled(server, TRUE);
3257 /* Update the client entries of this server to the new backup
3258 router. If we are the backup router we also resolve the real
3259 servers for the clients. After updating is over this also
3260 removes the clients that this server explicitly owns. */
3261 silc_server_update_clients_by_server(server, user_data,
3262 backup_router, TRUE);
3264 /* If we are router and just lost our primary router (now standlaone)
3265 we remove everything that was behind it, since we don't know
3267 if (server->server_type == SILC_ROUTER && server->standalone)
3268 /* Remove all servers that are originated from this server, and
3269 remove the clients of those servers too. */
3270 silc_server_remove_servers_by_server(server, user_data, TRUE);
3272 /* Finally remove the clients that are explicitly owned by this
3273 server. They go down with the server. */
3274 silc_server_remove_clients_by_server(server, user_data,
3277 /* Update our server cache to use the new backup router too. */
3278 silc_server_update_servers_by_server(server, user_data, backup_router);
3279 if (server->server_type == SILC_SERVER)
3280 silc_server_update_channels_by_server(server, user_data,
3283 /* Send notify about primary router going down to local operators */
3284 if (server->backup_router)
3285 SILC_SERVER_SEND_OPERS(server, FALSE, TRUE,
3286 SILC_NOTIFY_TYPE_NONE,
3287 ("%s switched to backup router %s "
3288 "(we are primary router now)",
3289 server->server_name, server->server_name));
3290 else if (server->router)
3291 SILC_SERVER_SEND_OPERS(server, FALSE, TRUE,
3292 SILC_NOTIFY_TYPE_NONE,
3293 ("%s switched to backup router %s",
3294 server->server_name,
3295 server->router->server_name));
3297 server->backup_noswitch = FALSE;
3299 /* Free the server entry */
3300 silc_server_backup_del(server, user_data);
3301 silc_server_backup_replaced_del(server, user_data);
3302 silc_idlist_del_data(user_data);
3303 if (!silc_idlist_del_server(server->local_list, user_data))
3304 silc_idlist_del_server(server->global_list, user_data);
3305 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
3306 server->stat.my_servers--;
3308 server->stat.my_routers--;
3309 server->stat.routers--;
3311 server->stat.servers--;
3312 if (server->server_type == SILC_ROUTER)
3313 server->stat.cell_servers--;
3315 if (backup_router && backup_router != server->id_entry) {
3316 /* Announce all of our stuff that was created about 5 minutes ago.
3317 The backup router knows all the other stuff already. */
3318 if (server->server_type == SILC_ROUTER)
3319 silc_server_announce_servers(server, FALSE, time(0) - 300,
3320 backup_router->connection);
3322 /* Announce our clients and channels to the router */
3323 silc_server_announce_clients(server, time(0) - 300,
3324 backup_router->connection);
3325 silc_server_announce_channels(server, time(0) - 300,
3326 backup_router->connection);
3332 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
3334 SILC_LOG_DEBUG(("Freeing unknown connection data"));
3336 silc_idlist_del_data(user_data);
3337 silc_free(user_data);
3342 /* If any protocol is active cancel its execution */
3343 if (sock->protocol && sock->protocol->protocol &&
3344 sock->protocol->protocol->type != SILC_PROTOCOL_SERVER_BACKUP) {
3345 SILC_LOG_DEBUG(("Cancelling protocol, calling final callback"));
3346 silc_protocol_cancel(sock->protocol, server->schedule);
3347 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
3348 silc_protocol_execute_final(sock->protocol, server->schedule);
3349 sock->protocol = NULL;
3352 sock->user_data = NULL;
3355 /* Removes client from all channels it has joined. This is used when client
3356 connection is disconnected. If the client on a channel is last, the
3357 channel is removed as well. This sends the SIGNOFF notify types. */
3359 void silc_server_remove_from_channels(SilcServer server,
3360 SilcSocketConnection sock,
3361 SilcClientEntry client,
3363 const char *signoff_message,
3367 SilcChannelEntry channel;
3368 SilcChannelClientEntry chl;
3369 SilcHashTableList htl;
3370 SilcBuffer clidp = NULL;
3375 SILC_LOG_DEBUG(("Removing client from joined channels"));
3377 if (notify && !client->id)
3381 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3386 /* Remove the client from all channels. The client is removed from
3387 the channels' user list. */
3388 silc_hash_table_list(client->channels, &htl);
3389 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3390 channel = chl->channel;
3392 /* Remove channel if this is last client leaving the channel, unless
3393 the channel is permanent. */
3394 if (server->server_type != SILC_SERVER &&
3395 silc_hash_table_count(channel->user_list) < 2) {
3396 silc_server_channel_delete(server, channel);
3400 silc_hash_table_del(client->channels, channel);
3401 silc_hash_table_del(channel->user_list, client);
3402 channel->user_count--;
3404 /* If there is no global users on the channel anymore mark the channel
3405 as local channel. Do not check if the removed client is local client. */
3406 if (server->server_type == SILC_SERVER && channel->global_users &&
3407 chl->client->router && !silc_server_channel_has_global(channel))
3408 channel->global_users = FALSE;
3410 memset(chl, 'A', sizeof(*chl));
3413 /* Update statistics */
3414 if (SILC_IS_LOCAL(client))
3415 server->stat.my_chanclients--;
3416 if (server->server_type == SILC_ROUTER) {
3417 server->stat.cell_chanclients--;
3418 server->stat.chanclients--;
3421 /* If there is not at least one local user on the channel then we don't
3422 need the channel entry anymore, we can remove it safely, unless the
3423 channel is permanent channel */
3424 if (server->server_type == SILC_SERVER &&
3425 !silc_server_channel_has_local(channel)) {
3426 /* Notify about leaving client if this channel has global users. */
3427 if (notify && channel->global_users)
3428 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3429 SILC_NOTIFY_TYPE_SIGNOFF,
3430 signoff_message ? 2 : 1,
3431 clidp->data, clidp->len,
3432 signoff_message, signoff_message ?
3433 strlen(signoff_message) : 0);
3435 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
3436 silc_server_channel_delete(server, channel);
3440 /* Send notify to channel about client leaving SILC and channel too */
3442 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3443 SILC_NOTIFY_TYPE_SIGNOFF,
3444 signoff_message ? 2 : 1,
3445 clidp->data, clidp->len,
3446 signoff_message, signoff_message ?
3447 strlen(signoff_message) : 0);
3449 if (killed && clidp) {
3450 /* Remove the client from channel's invite list */
3451 if (channel->invite_list &&
3452 silc_hash_table_count(channel->invite_list)) {
3454 SilcArgumentPayload iargs;
3455 ab = silc_argument_payload_encode_one(NULL, clidp->data,
3457 iargs = silc_argument_payload_parse(ab->data, ab->len, 1);
3458 silc_server_inviteban_process(server, channel->invite_list, 1, iargs);
3459 silc_buffer_free(ab);
3460 silc_argument_payload_free(iargs);
3464 /* Don't create keys if we are shutting down */
3465 if (server->server_shutdown)
3468 /* Re-generate channel key if needed */
3469 if (keygen && !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3470 if (!silc_server_create_channel_key(server, channel, 0))
3473 /* Send the channel key to the channel. The key of course is not sent
3474 to the client who was removed from the channel. */
3475 silc_server_send_channel_key(server, client->connection, channel,
3476 server->server_type == SILC_ROUTER ?
3477 FALSE : !server->standalone);
3481 silc_hash_table_list_reset(&htl);
3483 silc_buffer_free(clidp);
3486 /* Removes client from one channel. This is used for example when client
3487 calls LEAVE command to remove itself from the channel. Returns TRUE
3488 if channel still exists and FALSE if the channel is removed when
3489 last client leaves the channel. If `notify' is FALSE notify messages
3492 bool silc_server_remove_from_one_channel(SilcServer server,
3493 SilcSocketConnection sock,
3494 SilcChannelEntry channel,
3495 SilcClientEntry client,
3498 SilcChannelClientEntry chl;
3501 SILC_LOG_DEBUG(("Removing %s from channel %s",
3502 silc_id_render(client->id, SILC_ID_CLIENT),
3503 channel->channel_name));
3505 /* Get the entry to the channel, if this client is not on the channel
3507 if (!silc_hash_table_find(client->channels, channel, NULL, (void *)&chl))
3510 /* Remove channel if this is last client leaving the channel, unless
3511 the channel is permanent. */
3512 if (server->server_type != SILC_SERVER &&
3513 silc_hash_table_count(channel->user_list) < 2) {
3514 silc_server_channel_delete(server, channel);
3518 silc_hash_table_del(client->channels, channel);
3519 silc_hash_table_del(channel->user_list, client);
3520 channel->user_count--;
3522 /* If there is no global users on the channel anymore mark the channel
3523 as local channel. Do not check if the client is local client. */
3524 if (server->server_type == SILC_SERVER && channel->global_users &&
3525 chl->client->router && !silc_server_channel_has_global(channel))
3526 channel->global_users = FALSE;
3528 memset(chl, 'O', sizeof(*chl));
3531 /* Update statistics */
3532 if (SILC_IS_LOCAL(client))
3533 server->stat.my_chanclients--;
3534 if (server->server_type == SILC_ROUTER) {
3535 server->stat.cell_chanclients--;
3536 server->stat.chanclients--;
3539 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3543 /* If there is not at least one local user on the channel then we don't
3544 need the channel entry anymore, we can remove it safely, unless the
3545 channel is permanent channel */
3546 if (server->server_type == SILC_SERVER &&
3547 !silc_server_channel_has_local(channel)) {
3548 /* Notify about leaving client if this channel has global users. */
3549 if (notify && channel->global_users)
3550 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3551 SILC_NOTIFY_TYPE_LEAVE, 1,
3552 clidp->data, clidp->len);
3554 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
3555 silc_server_channel_delete(server, channel);
3556 silc_buffer_free(clidp);
3560 /* Send notify to channel about client leaving the channel */
3562 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3563 SILC_NOTIFY_TYPE_LEAVE, 1,
3564 clidp->data, clidp->len);
3566 silc_buffer_free(clidp);
3570 /* Timeout callback. This is called if connection is idle or for some
3571 other reason is not responding within some period of time. This
3572 disconnects the remote end. */
3574 SILC_TASK_CALLBACK(silc_server_timeout_remote)
3576 SilcServer server = (SilcServer)context;
3577 SilcSocketConnection sock = server->sockets[fd];
3578 SilcProtocolType protocol = 0;
3580 SILC_LOG_DEBUG(("Start"));
3585 SILC_LOG_ERROR(("No response from %s (%s), Connection timeout",
3586 sock->hostname, sock->ip));
3588 /* If we have protocol active we must assure that we call the protocol's
3589 final callback so that all the memory is freed. */
3590 if (sock->protocol && sock->protocol->protocol &&
3591 sock->protocol->protocol->type != SILC_PROTOCOL_SERVER_BACKUP) {
3592 protocol = sock->protocol->protocol->type;
3593 silc_protocol_cancel(sock->protocol, server->schedule);
3594 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
3595 silc_protocol_execute_final(sock->protocol, server->schedule);
3596 sock->protocol = NULL;
3600 silc_server_disconnect_remote(server, sock,
3602 SILC_PROTOCOL_SERVER_CONNECTION_AUTH ?
3603 SILC_STATUS_ERR_AUTH_FAILED :
3604 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED,
3605 "Connection timeout");
3607 if (sock->user_data)
3608 silc_server_free_sock_user_data(server, sock, NULL);
3611 /* Creates new channel. Sends NEW_CHANNEL packet to primary route. This
3612 function may be used only by router. In real SILC network all channels
3613 are created by routers thus this function is never used by normal
3616 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
3617 SilcServerID *router_id,
3623 SilcChannelID *channel_id;
3624 SilcChannelEntry entry;
3628 SILC_LOG_DEBUG(("Creating new channel %s", channel_name));
3631 cipher = SILC_DEFAULT_CIPHER;
3633 hmac = SILC_DEFAULT_HMAC;
3635 /* Allocate cipher */
3636 if (!silc_cipher_alloc(cipher, &key))
3640 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
3641 silc_cipher_free(key);
3645 channel_name = strdup(channel_name);
3647 /* Create the channel ID */
3648 if (!silc_id_create_channel_id(server, router_id, server->rng,
3650 silc_free(channel_name);
3651 silc_cipher_free(key);
3652 silc_hmac_free(newhmac);
3656 /* Create the channel */
3657 entry = silc_idlist_add_channel(server->local_list, channel_name,
3658 SILC_CHANNEL_MODE_NONE, channel_id,
3659 NULL, key, newhmac, 0);
3661 silc_free(channel_name);
3662 silc_cipher_free(key);
3663 silc_hmac_free(newhmac);
3664 silc_free(channel_id);
3668 entry->cipher = strdup(cipher);
3669 entry->hmac_name = strdup(hmac);
3671 /* Now create the actual key material */
3672 if (!silc_server_create_channel_key(server, entry,
3673 silc_cipher_get_key_len(key) / 8)) {
3674 silc_idlist_del_channel(server->local_list, entry);
3678 /* Notify other routers about the new channel. We send the packet
3679 to our primary route. */
3681 silc_server_send_new_channel(server, SILC_PRIMARY_ROUTE(server), TRUE,
3682 channel_name, entry->id,
3683 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
3686 /* Distribute to backup routers */
3687 if (broadcast && server->server_type == SILC_ROUTER) {
3690 SilcUInt32 name_len = strlen(channel_name);
3691 SilcUInt32 channel_id_len = silc_id_get_len(entry->id, SILC_ID_CHANNEL);
3692 cid = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3694 packet = silc_channel_payload_encode(channel_name, name_len,
3695 cid, channel_id_len, entry->mode);
3696 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
3697 packet->data, packet->len, FALSE, TRUE);
3699 silc_buffer_free(packet);
3702 server->stat.my_channels++;
3703 if (server->server_type == SILC_ROUTER) {
3704 server->stat.channels++;
3705 server->stat.cell_channels++;
3706 entry->users_resolved = TRUE;
3712 /* Same as above but creates the channel with Channel ID `channel_id. */
3715 silc_server_create_new_channel_with_id(SilcServer server,
3719 SilcChannelID *channel_id,
3722 SilcChannelEntry entry;
3726 SILC_LOG_DEBUG(("Creating new channel %s", channel_name));
3729 cipher = SILC_DEFAULT_CIPHER;
3731 hmac = SILC_DEFAULT_HMAC;
3733 /* Allocate cipher */
3734 if (!silc_cipher_alloc(cipher, &key))
3738 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
3739 silc_cipher_free(key);
3743 channel_name = strdup(channel_name);
3745 /* Create the channel */
3746 entry = silc_idlist_add_channel(server->local_list, channel_name,
3747 SILC_CHANNEL_MODE_NONE, channel_id,
3748 NULL, key, newhmac, 0);
3750 silc_cipher_free(key);
3751 silc_hmac_free(newhmac);
3752 silc_free(channel_name);
3756 /* Now create the actual key material */
3757 if (!silc_server_create_channel_key(server, entry,
3758 silc_cipher_get_key_len(key) / 8)) {
3759 silc_idlist_del_channel(server->local_list, entry);
3763 /* Notify other routers about the new channel. We send the packet
3764 to our primary route. */
3766 silc_server_send_new_channel(server, SILC_PRIMARY_ROUTE(server), TRUE,
3767 channel_name, entry->id,
3768 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
3771 /* Distribute to backup routers */
3772 if (broadcast && server->server_type == SILC_ROUTER) {
3775 SilcUInt32 name_len = strlen(channel_name);
3776 SilcUInt32 channel_id_len = silc_id_get_len(entry->id, SILC_ID_CHANNEL);
3777 cid = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3779 packet = silc_channel_payload_encode(channel_name, name_len,
3780 cid, channel_id_len, entry->mode);
3781 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
3782 packet->data, packet->len, FALSE, TRUE);
3784 silc_buffer_free(packet);
3787 server->stat.my_channels++;
3788 if (server->server_type == SILC_ROUTER) {
3789 server->stat.channels++;
3790 server->stat.cell_channels++;
3791 entry->users_resolved = TRUE;
3797 /* Channel's key re-key timeout callback. */
3799 SILC_TASK_CALLBACK(silc_server_channel_key_rekey)
3801 SilcServer server = app_context;
3802 SilcServerChannelRekey rekey = (SilcServerChannelRekey)context;
3806 /* Return now if we are shutting down */
3807 if (server->server_shutdown)
3810 if (!silc_server_create_channel_key(server, rekey->channel, rekey->key_len))
3813 silc_server_send_channel_key(server, NULL, rekey->channel, FALSE);
3816 /* Generates new channel key. This is used to create the initial channel key
3817 but also to re-generate new key for channel. If `key_len' is provided
3818 it is the bytes of the key length. */
3820 bool silc_server_create_channel_key(SilcServer server,
3821 SilcChannelEntry channel,
3825 unsigned char channel_key[32], hash[32];
3828 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3829 SILC_LOG_DEBUG(("Channel has private keys, will not generate new key"));
3833 SILC_LOG_DEBUG(("Generating channel %s key", channel->channel_name));
3835 if (!channel->channel_key)
3836 if (!silc_cipher_alloc(SILC_DEFAULT_CIPHER, &channel->channel_key)) {
3837 channel->channel_key = NULL;
3843 else if (channel->key_len)
3844 len = channel->key_len / 8;
3846 len = silc_cipher_get_key_len(channel->channel_key) / 8;
3848 /* Create channel key */
3849 for (i = 0; i < len; i++) channel_key[i] = silc_rng_get_byte(server->rng);
3852 silc_cipher_set_key(channel->channel_key, channel_key, len * 8);
3854 /* Remove old key if exists */
3856 memset(channel->key, 0, channel->key_len / 8);
3857 silc_free(channel->key);
3861 channel->key_len = len * 8;
3862 channel->key = silc_memdup(channel_key, len);
3863 memset(channel_key, 0, sizeof(channel_key));
3865 /* Generate HMAC key from the channel key data and set it */
3867 if (!silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac)) {
3868 memset(channel->key, 0, channel->key_len / 8);
3869 silc_free(channel->key);
3870 channel->channel_key = NULL;
3873 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key, len, hash);
3874 silc_hmac_set_key(channel->hmac, hash,
3875 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3876 memset(hash, 0, sizeof(hash));
3878 if (server->server_type == SILC_ROUTER) {
3879 if (!channel->rekey)
3880 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
3881 channel->rekey->channel = channel;
3882 channel->rekey->key_len = key_len;
3883 if (channel->rekey->task)
3884 silc_schedule_task_del(server->schedule, channel->rekey->task);
3886 channel->rekey->task =
3887 silc_schedule_task_add(server->schedule, 0,
3888 silc_server_channel_key_rekey,
3889 (void *)channel->rekey,
3890 server->config->channel_rekey_secs, 0,
3892 SILC_TASK_PRI_NORMAL);
3898 /* Saves the channel key found in the encoded `key_payload' buffer. This
3899 function is used when we receive Channel Key Payload and also when we're
3900 processing JOIN command reply. Returns entry to the channel. */
3902 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
3903 SilcBuffer key_payload,
3904 SilcChannelEntry channel)
3906 SilcChannelKeyPayload payload = NULL;
3907 SilcChannelID *id = NULL;
3908 unsigned char *tmp, hash[32];
3912 /* Decode channel key payload */
3913 payload = silc_channel_key_payload_parse(key_payload->data,
3916 SILC_LOG_ERROR(("Bad channel key payload received, dropped"));
3921 /* Get the channel entry */
3924 /* Get channel ID */
3925 tmp = silc_channel_key_get_id(payload, &tmp_len);
3926 id = silc_id_str2id(tmp, tmp_len, SILC_ID_CHANNEL);
3932 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
3934 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
3936 if (server->server_type == SILC_ROUTER)
3937 SILC_LOG_ERROR(("Received key for non-existent channel %s",
3938 silc_id_render(id, SILC_ID_CHANNEL)));
3944 SILC_LOG_DEBUG(("Saving new channel %s key", channel->channel_name));
3946 tmp = silc_channel_key_get_key(payload, &tmp_len);
3952 cipher = silc_channel_key_get_cipher(payload, NULL);
3958 /* Remove old key if exists */
3960 memset(channel->key, 0, channel->key_len / 8);
3961 silc_free(channel->key);
3962 silc_cipher_free(channel->channel_key);
3965 /* Create new cipher */
3966 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3967 channel->channel_key = NULL;
3972 if (channel->cipher)
3973 silc_free(channel->cipher);
3974 channel->cipher = strdup(cipher);
3977 channel->key_len = tmp_len * 8;
3978 channel->key = silc_memdup(tmp, tmp_len);
3979 silc_cipher_set_key(channel->channel_key, tmp, channel->key_len);
3981 /* Generate HMAC key from the channel key data and set it */
3983 if (!silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac)) {
3984 memset(channel->key, 0, channel->key_len / 8);
3985 silc_free(channel->key);
3986 channel->channel_key = NULL;
3989 silc_hash_make(silc_hmac_get_hash(channel->hmac), tmp, tmp_len, hash);
3990 silc_hmac_set_key(channel->hmac, hash,
3991 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3993 memset(hash, 0, sizeof(hash));
3994 memset(tmp, 0, tmp_len);
3996 if (server->server_type == SILC_ROUTER) {
3997 if (!channel->rekey)
3998 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
3999 channel->rekey->channel = channel;
4000 if (channel->rekey->task)
4001 silc_schedule_task_del(server->schedule, channel->rekey->task);
4003 channel->rekey->task =
4004 silc_schedule_task_add(server->schedule, 0,
4005 silc_server_channel_key_rekey,
4006 (void *)channel->rekey,
4007 server->config->channel_rekey_secs, 0,
4009 SILC_TASK_PRI_NORMAL);
4015 silc_channel_key_payload_free(payload);
4020 /* Heartbeat callback. This function is set as argument for the
4021 silc_socket_set_heartbeat function. The library will call this function
4022 at the set time interval. */
4024 void silc_server_perform_heartbeat(SilcSocketConnection sock,
4027 SilcServer server = hb_context;
4029 SILC_LOG_DEBUG(("Sending heartbeat to %s:%d (%s)", sock->hostname,
4030 sock->port, sock->ip));
4032 /* Send the heartbeat */
4033 silc_server_send_heartbeat(server, sock);
4036 /* Returns assembled of all servers in the given ID list. The packet's
4037 form is dictated by the New ID payload. */
4039 static void silc_server_announce_get_servers(SilcServer server,
4040 SilcServerEntry remote,
4042 SilcBuffer *servers,
4043 unsigned long creation_time)
4045 SilcIDCacheList list;
4046 SilcIDCacheEntry id_cache;
4047 SilcServerEntry entry;
4050 /* Go through all clients in the list */
4051 if (silc_idcache_get_all(id_list->servers, &list)) {
4052 if (silc_idcache_list_first(list, &id_cache)) {
4054 entry = (SilcServerEntry)id_cache->context;
4056 /* Do not announce the one we've sending our announcements and
4057 do not announce ourself. Also check the creation time if it's
4059 if ((entry == remote) || (entry == server->id_entry) ||
4060 (creation_time && entry->data.created < creation_time)) {
4061 if (!silc_idcache_list_next(list, &id_cache))
4066 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
4068 *servers = silc_buffer_realloc(*servers,
4070 (*servers)->truelen + idp->len :
4072 silc_buffer_pull_tail(*servers, ((*servers)->end - (*servers)->data));
4073 silc_buffer_put(*servers, idp->data, idp->len);
4074 silc_buffer_pull(*servers, idp->len);
4075 silc_buffer_free(idp);
4077 if (!silc_idcache_list_next(list, &id_cache))
4082 silc_idcache_list_free(list);
4087 silc_server_announce_encode_notify(SilcNotifyType notify, SilcUInt32 argc, ...)
4093 p = silc_notify_payload_encode(notify, argc, ap);
4099 /* This function is used by router to announce existing servers to our
4100 primary router when we've connected to it. If `creation_time' is non-zero
4101 then only the servers that has been created after the `creation_time'
4102 will be announced. */
4104 void silc_server_announce_servers(SilcServer server, bool global,
4105 unsigned long creation_time,
4106 SilcSocketConnection remote)
4108 SilcBuffer servers = NULL;
4110 SILC_LOG_DEBUG(("Announcing servers"));
4112 /* Get servers in local list */
4113 silc_server_announce_get_servers(server, remote->user_data,
4114 server->local_list, &servers,
4118 /* Get servers in global list */
4119 silc_server_announce_get_servers(server, remote->user_data,
4120 server->global_list, &servers,
4124 silc_buffer_push(servers, servers->data - servers->head);
4125 SILC_LOG_HEXDUMP(("servers"), servers->data, servers->len);
4127 /* Send the packet */
4128 silc_server_packet_send(server, remote,
4129 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
4130 servers->data, servers->len, TRUE);
4132 silc_buffer_free(servers);
4136 /* Returns assembled packet of all clients in the given ID list. The
4137 packet's form is dictated by the New ID Payload. */
4139 static void silc_server_announce_get_clients(SilcServer server,
4141 SilcBuffer *clients,
4143 unsigned long creation_time)
4145 SilcIDCacheList list;
4146 SilcIDCacheEntry id_cache;
4147 SilcClientEntry client;
4150 unsigned char mode[4];
4152 /* Go through all clients in the list */
4153 if (silc_idcache_get_all(id_list->clients, &list)) {
4154 if (silc_idcache_list_first(list, &id_cache)) {
4156 client = (SilcClientEntry)id_cache->context;
4158 if (creation_time && client->data.created < creation_time) {
4159 if (!silc_idcache_list_next(list, &id_cache))
4163 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED) &&
4164 !client->connection && !client->router && !SILC_IS_LOCAL(client)) {
4165 if (!silc_idcache_list_next(list, &id_cache))
4170 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4172 *clients = silc_buffer_realloc(*clients,
4174 (*clients)->truelen + idp->len :
4176 silc_buffer_pull_tail(*clients, ((*clients)->end - (*clients)->data));
4177 silc_buffer_put(*clients, idp->data, idp->len);
4178 silc_buffer_pull(*clients, idp->len);
4180 SILC_PUT32_MSB(client->mode, mode);
4182 silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_UMODE_CHANGE,
4183 2, idp->data, idp->len,
4185 *umodes = silc_buffer_realloc(*umodes,
4187 (*umodes)->truelen + tmp->len :
4189 silc_buffer_pull_tail(*umodes, ((*umodes)->end - (*umodes)->data));
4190 silc_buffer_put(*umodes, tmp->data, tmp->len);
4191 silc_buffer_pull(*umodes, tmp->len);
4192 silc_buffer_free(tmp);
4194 silc_buffer_free(idp);
4196 if (!silc_idcache_list_next(list, &id_cache))
4201 silc_idcache_list_free(list);
4205 /* This function is used to announce our existing clients to our router
4206 when we've connected to it. If `creation_time' is non-zero then only
4207 the clients that has been created after the `creation_time' will be
4210 void silc_server_announce_clients(SilcServer server,
4211 unsigned long creation_time,
4212 SilcSocketConnection remote)
4214 SilcBuffer clients = NULL;
4215 SilcBuffer umodes = NULL;
4217 SILC_LOG_DEBUG(("Announcing clients"));
4219 /* Get clients in local list */
4220 silc_server_announce_get_clients(server, server->local_list,
4221 &clients, &umodes, creation_time);
4223 /* As router we announce our global list as well */
4224 if (server->server_type == SILC_ROUTER)
4225 silc_server_announce_get_clients(server, server->global_list,
4226 &clients, &umodes, creation_time);
4229 silc_buffer_push(clients, clients->data - clients->head);
4230 SILC_LOG_HEXDUMP(("clients"), clients->data, clients->len);
4232 /* Send the packet */
4233 silc_server_packet_send(server, remote,
4234 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
4235 clients->data, clients->len, TRUE);
4237 silc_buffer_free(clients);
4241 silc_buffer_push(umodes, umodes->data - umodes->head);
4242 SILC_LOG_HEXDUMP(("umodes"), umodes->data, umodes->len);
4244 /* Send the packet */
4245 silc_server_packet_send(server, remote,
4246 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
4247 umodes->data, umodes->len, TRUE);
4249 silc_buffer_free(umodes);
4253 /* Returns channel's topic for announcing it */
4255 void silc_server_announce_get_channel_topic(SilcServer server,
4256 SilcChannelEntry channel,
4261 if (channel->topic) {
4262 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4263 *topic = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_TOPIC_SET, 2,
4264 chidp->data, chidp->len,
4266 strlen(channel->topic));
4267 silc_buffer_free(chidp);
4271 /* Returns assembled packets for channel users of the `channel'. */
4273 void silc_server_announce_get_channel_users(SilcServer server,
4274 SilcChannelEntry channel,
4275 SilcBuffer *channel_modes,
4276 SilcBuffer *channel_users,
4277 SilcBuffer *channel_users_modes)
4279 SilcChannelClientEntry chl;
4280 SilcHashTableList htl;
4281 SilcBuffer chidp, clidp, csidp;
4282 SilcBuffer tmp, fkey = NULL;
4284 unsigned char mode[4];
4287 SILC_LOG_DEBUG(("Start"));
4289 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4290 csidp = silc_id_payload_encode(server->id, SILC_ID_SERVER);
4293 SILC_PUT32_MSB(channel->mode, mode);
4294 hmac = channel->hmac ? (char *)silc_hmac_get_name(channel->hmac) : NULL;
4295 if (channel->founder_key)
4296 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
4298 silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CMODE_CHANGE,
4299 6, csidp->data, csidp->len,
4302 hmac, hmac ? strlen(hmac) : 0,
4303 channel->passphrase,
4304 channel->passphrase ?
4305 strlen(channel->passphrase) : 0,
4306 fkey ? fkey->data : NULL,
4307 fkey ? fkey->len : 0);
4310 silc_buffer_realloc(*channel_modes,
4312 (*channel_modes)->truelen + len : len));
4313 silc_buffer_pull_tail(*channel_modes,
4314 ((*channel_modes)->end -
4315 (*channel_modes)->data));
4316 silc_buffer_put(*channel_modes, tmp->data, tmp->len);
4317 silc_buffer_pull(*channel_modes, len);
4318 silc_buffer_free(tmp);
4319 silc_buffer_free(fkey);
4322 /* Now find all users on the channel */
4323 silc_hash_table_list(channel->user_list, &htl);
4324 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
4325 clidp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
4328 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_JOIN, 2,
4329 clidp->data, clidp->len,
4330 chidp->data, chidp->len);
4333 silc_buffer_realloc(*channel_users,
4335 (*channel_users)->truelen + len : len));
4336 silc_buffer_pull_tail(*channel_users,
4337 ((*channel_users)->end -
4338 (*channel_users)->data));
4340 silc_buffer_put(*channel_users, tmp->data, tmp->len);
4341 silc_buffer_pull(*channel_users, len);
4342 silc_buffer_free(tmp);
4344 /* CUMODE notify for mode change on the channel */
4345 SILC_PUT32_MSB(chl->mode, mode);
4346 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && channel->founder_key)
4347 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
4348 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CUMODE_CHANGE,
4349 4, csidp->data, csidp->len,
4351 clidp->data, clidp->len,
4352 fkey ? fkey->data : NULL,
4353 fkey ? fkey->len : 0);
4355 *channel_users_modes =
4356 silc_buffer_realloc(*channel_users_modes,
4357 (*channel_users_modes ?
4358 (*channel_users_modes)->truelen + len : len));
4359 silc_buffer_pull_tail(*channel_users_modes,
4360 ((*channel_users_modes)->end -
4361 (*channel_users_modes)->data));
4363 silc_buffer_put(*channel_users_modes, tmp->data, tmp->len);
4364 silc_buffer_pull(*channel_users_modes, len);
4365 silc_buffer_free(tmp);
4366 silc_buffer_free(fkey);
4368 silc_buffer_free(clidp);
4370 silc_hash_table_list_reset(&htl);
4371 silc_buffer_free(chidp);
4372 silc_buffer_free(csidp);
4375 /* Returns assembled packets for all channels and users on those channels
4376 from the given ID List. The packets are in the form dictated by the
4377 New Channel and New Channel User payloads. */
4379 void silc_server_announce_get_channels(SilcServer server,
4381 SilcBuffer *channels,
4382 SilcBuffer **channel_modes,
4383 SilcBuffer *channel_users,
4384 SilcBuffer **channel_users_modes,
4385 SilcUInt32 *channel_users_modes_c,
4386 SilcBuffer **channel_topics,
4387 SilcChannelID ***channel_ids,
4388 unsigned long creation_time)
4390 SilcIDCacheList list;
4391 SilcIDCacheEntry id_cache;
4392 SilcChannelEntry channel;
4395 SilcUInt16 name_len;
4397 int i = *channel_users_modes_c;
4400 SILC_LOG_DEBUG(("Start"));
4402 /* Go through all channels in the list */
4403 if (silc_idcache_get_all(id_list->channels, &list)) {
4404 if (silc_idcache_list_first(list, &id_cache)) {
4406 channel = (SilcChannelEntry)id_cache->context;
4408 if (creation_time && channel->created < creation_time)
4413 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
4414 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
4415 name_len = strlen(channel->channel_name);
4418 len = 4 + name_len + id_len + 4;
4420 silc_buffer_realloc(*channels,
4421 (*channels ? (*channels)->truelen +
4423 silc_buffer_pull_tail(*channels,
4424 ((*channels)->end - (*channels)->data));
4425 silc_buffer_format(*channels,
4426 SILC_STR_UI_SHORT(name_len),
4427 SILC_STR_UI_XNSTRING(channel->channel_name,
4429 SILC_STR_UI_SHORT(id_len),
4430 SILC_STR_UI_XNSTRING(cid, id_len),
4431 SILC_STR_UI_INT(channel->mode),
4433 silc_buffer_pull(*channels, len);
4436 if (creation_time && channel->updated < creation_time)
4442 /* Channel user modes */
4443 *channel_users_modes = silc_realloc(*channel_users_modes,
4444 sizeof(**channel_users_modes) *
4446 (*channel_users_modes)[i] = NULL;
4447 *channel_modes = silc_realloc(*channel_modes,
4448 sizeof(**channel_modes) * (i + 1));
4449 (*channel_modes)[i] = NULL;
4450 *channel_ids = silc_realloc(*channel_ids,
4451 sizeof(**channel_ids) * (i + 1));
4452 (*channel_ids)[i] = NULL;
4453 silc_server_announce_get_channel_users(server, channel,
4454 &(*channel_modes)[i],
4456 &(*channel_users_modes)[i]);
4457 (*channel_ids)[i] = channel->id;
4459 /* Channel's topic */
4460 *channel_topics = silc_realloc(*channel_topics,
4461 sizeof(**channel_topics) * (i + 1));
4462 (*channel_topics)[i] = NULL;
4463 silc_server_announce_get_channel_topic(server, channel,
4464 &(*channel_topics)[i]);
4465 (*channel_users_modes_c)++;
4472 if (!silc_idcache_list_next(list, &id_cache))
4477 silc_idcache_list_free(list);
4481 /* This function is used to announce our existing channels to our router
4482 when we've connected to it. This also announces the users on the
4483 channels to the router. If the `creation_time' is non-zero only the
4484 channels that was created after the `creation_time' are announced.
4485 Note that the channel users are still announced even if the `creation_time'
4488 void silc_server_announce_channels(SilcServer server,
4489 unsigned long creation_time,
4490 SilcSocketConnection remote)
4492 SilcBuffer channels = NULL, *channel_modes = NULL, channel_users = NULL;
4493 SilcBuffer *channel_users_modes = NULL;
4494 SilcBuffer *channel_topics = NULL;
4495 SilcUInt32 channel_users_modes_c = 0;
4496 SilcChannelID **channel_ids = NULL;
4498 SILC_LOG_DEBUG(("Announcing channels and channel users"));
4500 /* Get channels and channel users in local list */
4501 silc_server_announce_get_channels(server, server->local_list,
4502 &channels, &channel_modes,
4504 &channel_users_modes,
4505 &channel_users_modes_c,
4507 &channel_ids, creation_time);
4509 /* Get channels and channel users in global list */
4510 if (server->server_type != SILC_SERVER)
4511 silc_server_announce_get_channels(server, server->global_list,
4512 &channels, &channel_modes,
4514 &channel_users_modes,
4515 &channel_users_modes_c,
4517 &channel_ids, creation_time);
4520 silc_buffer_push(channels, channels->data - channels->head);
4521 SILC_LOG_HEXDUMP(("channels"), channels->data, channels->len);
4523 /* Send the packet */
4524 silc_server_packet_send(server, remote,
4525 SILC_PACKET_NEW_CHANNEL, SILC_PACKET_FLAG_LIST,
4526 channels->data, channels->len,
4529 silc_buffer_free(channels);
4532 if (channel_users) {
4533 silc_buffer_push(channel_users, channel_users->data - channel_users->head);
4534 SILC_LOG_HEXDUMP(("channel users"), channel_users->data,
4535 channel_users->len);
4537 /* Send the packet */
4538 silc_server_packet_send(server, remote,
4539 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
4540 channel_users->data, channel_users->len,
4543 silc_buffer_free(channel_users);
4546 if (channel_modes) {
4549 for (i = 0; i < channel_users_modes_c; i++) {
4550 if (!channel_modes[i])
4552 silc_buffer_push(channel_modes[i],
4553 channel_modes[i]->data -
4554 channel_modes[i]->head);
4555 SILC_LOG_HEXDUMP(("channel modes"), channel_modes[i]->data,
4556 channel_modes[i]->len);
4557 silc_server_packet_send_dest(server, remote,
4558 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
4559 channel_ids[i], SILC_ID_CHANNEL,
4560 channel_modes[i]->data,
4561 channel_modes[i]->len,
4563 silc_buffer_free(channel_modes[i]);
4565 silc_free(channel_modes);
4568 if (channel_users_modes) {
4571 for (i = 0; i < channel_users_modes_c; i++) {
4572 if (!channel_users_modes[i])
4574 silc_buffer_push(channel_users_modes[i],
4575 channel_users_modes[i]->data -
4576 channel_users_modes[i]->head);
4577 SILC_LOG_HEXDUMP(("channel users modes"), channel_users_modes[i]->data,
4578 channel_users_modes[i]->len);
4579 silc_server_packet_send_dest(server, remote,
4580 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
4581 channel_ids[i], SILC_ID_CHANNEL,
4582 channel_users_modes[i]->data,
4583 channel_users_modes[i]->len,
4585 silc_buffer_free(channel_users_modes[i]);
4587 silc_free(channel_users_modes);
4590 if (channel_topics) {
4593 for (i = 0; i < channel_users_modes_c; i++) {
4594 if (!channel_topics[i])
4597 silc_buffer_push(channel_topics[i],
4598 channel_topics[i]->data -
4599 channel_topics[i]->head);
4600 SILC_LOG_HEXDUMP(("channel topic"), channel_topics[i]->data,
4601 channel_topics[i]->len);
4602 silc_server_packet_send_dest(server, remote,
4603 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
4604 channel_ids[i], SILC_ID_CHANNEL,
4605 channel_topics[i]->data,
4606 channel_topics[i]->len,
4608 silc_buffer_free(channel_topics[i]);
4610 silc_free(channel_topics);
4613 silc_free(channel_ids);
4616 /* Assembles user list and users mode list from the `channel'. */
4618 bool silc_server_get_users_on_channel(SilcServer server,
4619 SilcChannelEntry channel,
4620 SilcBuffer *user_list,
4621 SilcBuffer *mode_list,
4622 SilcUInt32 *user_count)
4624 SilcChannelClientEntry chl;
4625 SilcHashTableList htl;
4626 SilcBuffer client_id_list;
4627 SilcBuffer client_mode_list;
4629 SilcUInt32 list_count = 0, len = 0;
4631 if (!silc_hash_table_count(channel->user_list))
4634 silc_hash_table_list(channel->user_list, &htl);
4635 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
4636 len += (silc_id_get_len(chl->client->id, SILC_ID_CLIENT) + 4);
4637 silc_hash_table_list_reset(&htl);
4639 client_id_list = silc_buffer_alloc(len);
4641 silc_buffer_alloc(4 * silc_hash_table_count(channel->user_list));
4642 silc_buffer_pull_tail(client_id_list, SILC_BUFFER_END(client_id_list));
4643 silc_buffer_pull_tail(client_mode_list, SILC_BUFFER_END(client_mode_list));
4645 silc_hash_table_list(channel->user_list, &htl);
4646 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
4648 idp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
4649 silc_buffer_put(client_id_list, idp->data, idp->len);
4650 silc_buffer_pull(client_id_list, idp->len);
4651 silc_buffer_free(idp);
4653 /* Client's mode on channel */
4654 SILC_PUT32_MSB(chl->mode, client_mode_list->data);
4655 silc_buffer_pull(client_mode_list, 4);
4659 silc_hash_table_list_reset(&htl);
4660 silc_buffer_push(client_id_list,
4661 client_id_list->data - client_id_list->head);
4662 silc_buffer_push(client_mode_list,
4663 client_mode_list->data - client_mode_list->head);
4665 *user_list = client_id_list;
4666 *mode_list = client_mode_list;
4667 *user_count = list_count;
4671 /* Saves users and their modes to the `channel'. */
4673 void silc_server_save_users_on_channel(SilcServer server,
4674 SilcSocketConnection sock,
4675 SilcChannelEntry channel,
4676 SilcClientID *noadd,
4677 SilcBuffer user_list,
4678 SilcBuffer mode_list,
4679 SilcUInt32 user_count)
4684 SilcClientID *client_id;
4685 SilcClientEntry client;
4686 SilcIDCacheEntry cache;
4687 SilcChannelClientEntry chl;
4689 SILC_LOG_DEBUG(("Saving %d users on %s channel", user_count,
4690 channel->channel_name));
4692 for (i = 0; i < user_count; i++) {
4694 SILC_GET16_MSB(idp_len, user_list->data + 2);
4696 client_id = silc_id_payload_parse_id(user_list->data, idp_len, NULL);
4697 silc_buffer_pull(user_list, idp_len);
4702 SILC_GET32_MSB(mode, mode_list->data);
4703 silc_buffer_pull(mode_list, 4);
4705 if (noadd && SILC_ID_CLIENT_COMPARE(client_id, noadd)) {
4706 silc_free(client_id);
4712 /* Check if we have this client cached already. */
4713 client = silc_idlist_find_client_by_id(server->local_list, client_id,
4714 server->server_type, &cache);
4716 client = silc_idlist_find_client_by_id(server->global_list,
4717 client_id, server->server_type,
4720 /* If router did not find such Client ID in its lists then this must
4721 be bogus client or some router in the net is buggy. */
4722 if (server->server_type != SILC_SERVER) {
4723 silc_free(client_id);
4727 /* We don't have that client anywhere, add it. The client is added
4728 to global list since server didn't have it in the lists so it must be
4730 client = silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
4731 silc_id_dup(client_id, SILC_ID_CLIENT),
4732 sock->user_data, NULL, 0);
4734 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
4735 silc_free(client_id);
4739 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
4744 silc_free(client_id);
4746 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
4747 SILC_LOG_ERROR(("Attempting to add unregistered client to channel ",
4748 "%s", channel->channel_name));
4752 if (!silc_server_client_on_channel(client, channel, &chl)) {
4753 /* Client was not on the channel, add it. */
4754 chl = silc_calloc(1, sizeof(*chl));
4755 chl->client = client;
4757 chl->channel = channel;
4758 silc_hash_table_add(channel->user_list, chl->client, chl);
4759 silc_hash_table_add(client->channels, chl->channel, chl);
4760 channel->user_count++;
4768 /* Saves channels and channels user modes to the `client'. Removes
4769 the client from those channels that are not sent in the list but
4772 void silc_server_save_user_channels(SilcServer server,
4773 SilcSocketConnection sock,
4774 SilcClientEntry client,
4775 SilcBuffer channels,
4776 SilcBuffer channels_user_modes)
4779 SilcUInt32 *chumodes;
4780 SilcChannelPayload entry;
4781 SilcChannelEntry channel;
4782 SilcChannelID *channel_id;
4783 SilcChannelClientEntry chl;
4784 SilcHashTable ht = NULL;
4785 SilcHashTableList htl;
4789 if (!channels || !channels_user_modes ||
4790 !(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
4793 ch = silc_channel_payload_parse_list(channels->data, channels->len);
4794 if (ch && silc_get_mode_list(channels_user_modes, silc_dlist_count(ch),
4796 ht = silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL,
4797 NULL, NULL, NULL, TRUE);
4798 silc_dlist_start(ch);
4799 while ((entry = silc_dlist_get(ch)) != SILC_LIST_END) {
4800 /* Check if we have this channel, and add it if we don't have it.
4801 Also add the client on the channel unless it is there already. */
4802 channel_id = silc_channel_get_id_parse(entry);
4803 channel = silc_idlist_find_channel_by_id(server->local_list,
4806 channel = silc_idlist_find_channel_by_id(server->global_list,
4809 if (server->server_type != SILC_SERVER) {
4810 silc_free(channel_id);
4815 /* We don't have that channel anywhere, add it. */
4816 name = silc_channel_get_name(entry, NULL);
4817 channel = silc_idlist_add_channel(server->global_list, strdup(name), 0,
4818 channel_id, server->router,
4821 silc_free(channel_id);
4828 channel->mode = silc_channel_get_mode(entry);
4830 /* Add the client on the channel */
4831 if (!silc_server_client_on_channel(client, channel, &chl)) {
4832 chl = silc_calloc(1, sizeof(*chl));
4833 chl->client = client;
4834 chl->mode = chumodes[i++];
4835 chl->channel = channel;
4836 silc_hash_table_add(channel->user_list, chl->client, chl);
4837 silc_hash_table_add(client->channels, chl->channel, chl);
4838 channel->user_count++;
4841 chl->mode = chumodes[i++];
4844 silc_hash_table_add(ht, channel, channel);
4845 silc_free(channel_id);
4847 silc_channel_payload_list_free(ch);
4848 silc_free(chumodes);
4852 /* Go through the list again and remove client from channels that
4853 are no part of the list. */
4855 silc_hash_table_list(client->channels, &htl);
4856 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
4857 if (!silc_hash_table_find(ht, chl->channel, NULL, NULL)) {
4858 silc_hash_table_del(chl->channel->user_list, chl->client);
4859 silc_hash_table_del(chl->client->channels, chl->channel);
4863 silc_hash_table_list_reset(&htl);
4864 silc_hash_table_free(ht);
4866 silc_hash_table_list(client->channels, &htl);
4867 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
4868 silc_hash_table_del(chl->channel->user_list, chl->client);
4869 silc_hash_table_del(chl->client->channels, chl->channel);
4872 silc_hash_table_list_reset(&htl);
4876 /* Lookups route to the client indicated by the `id_data'. The connection
4877 object and internal data object is returned. Returns NULL if route
4878 could not be found to the client. If the `client_id' is specified then
4879 it is used and the `id_data' is ignored. */
4881 SilcSocketConnection
4882 silc_server_get_client_route(SilcServer server,
4883 unsigned char *id_data,
4885 SilcClientID *client_id,
4886 SilcIDListData *idata,
4887 SilcClientEntry *client_entry)
4890 SilcClientEntry client;
4892 SILC_LOG_DEBUG(("Start"));
4895 *client_entry = NULL;
4897 /* Decode destination Client ID */
4899 id = silc_id_str2id(id_data, id_len, SILC_ID_CLIENT);
4901 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
4905 id = silc_id_dup(client_id, SILC_ID_CLIENT);
4908 /* If the destination belongs to our server we don't have to route
4909 the packet anywhere but to send it to the local destination. */
4910 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
4914 /* If we are router and the client has router then the client is in
4915 our cell but not directly connected to us. */
4916 if (server->server_type == SILC_ROUTER && client->router) {
4917 /* We are of course in this case the client's router thus the route
4918 to the client is the server who owns the client. So, we will send
4919 the packet to that server. */
4921 *idata = (SilcIDListData)client->router;
4922 return client->router->connection;
4925 /* Seems that client really is directly connected to us */
4927 *idata = (SilcIDListData)client;
4929 *client_entry = client;
4930 return client->connection;
4933 /* Destination belongs to someone not in this server. If we are normal
4934 server our action is to send the packet to our router. */
4935 if (server->server_type != SILC_ROUTER && !server->standalone) {
4938 *idata = (SilcIDListData)server->router;
4939 return SILC_PRIMARY_ROUTE(server);
4942 /* We are router and we will perform route lookup for the destination
4943 and send the packet to fastest route. */
4944 if (server->server_type == SILC_ROUTER && !server->standalone) {
4945 /* Check first that the ID is valid */
4946 client = silc_idlist_find_client_by_id(server->global_list, id,
4949 SilcSocketConnection dst_sock;
4951 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
4955 *idata = (SilcIDListData)dst_sock->user_data;
4964 /* Encodes and returns channel list of channels the `client' has joined.
4965 Secret channels are not put to the list. */
4967 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
4968 SilcClientEntry client,
4971 SilcBuffer *user_mode_list)
4973 SilcBuffer buffer = NULL;
4974 SilcChannelEntry channel;
4975 SilcChannelClientEntry chl;
4976 SilcHashTableList htl;
4979 SilcUInt16 name_len;
4983 *user_mode_list = NULL;
4985 silc_hash_table_list(client->channels, &htl);
4986 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
4987 channel = chl->channel;
4989 if (channel->mode & SILC_CHANNEL_MODE_SECRET && !get_secret)
4991 if (channel->mode & SILC_CHANNEL_MODE_PRIVATE && !get_private)
4994 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
4995 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
4996 name_len = strlen(channel->channel_name);
4998 len = 4 + name_len + id_len + 4;
4999 buffer = silc_buffer_realloc(buffer,
5000 (buffer ? buffer->truelen + len : len));
5001 silc_buffer_pull_tail(buffer, (buffer->end - buffer->data));
5002 silc_buffer_format(buffer,
5003 SILC_STR_UI_SHORT(name_len),
5004 SILC_STR_UI_XNSTRING(channel->channel_name,
5006 SILC_STR_UI_SHORT(id_len),
5007 SILC_STR_UI_XNSTRING(cid, id_len),
5008 SILC_STR_UI_INT(chl->channel->mode),
5010 silc_buffer_pull(buffer, len);
5013 if (user_mode_list) {
5014 *user_mode_list = silc_buffer_realloc(*user_mode_list,
5016 (*user_mode_list)->truelen + 4 :
5018 silc_buffer_pull_tail(*user_mode_list, ((*user_mode_list)->end -
5019 (*user_mode_list)->data));
5020 SILC_PUT32_MSB(chl->mode, (*user_mode_list)->data);
5021 silc_buffer_pull(*user_mode_list, 4);
5024 silc_hash_table_list_reset(&htl);
5027 silc_buffer_push(buffer, buffer->data - buffer->head);
5028 if (user_mode_list && *user_mode_list)
5029 silc_buffer_push(*user_mode_list, ((*user_mode_list)->data -
5030 (*user_mode_list)->head));
5035 /* A timeout callback for the re-key. We will be the initiator of the
5038 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_callback)
5040 SilcServer server = app_context;
5041 SilcSocketConnection sock = (SilcSocketConnection)context;
5042 SilcIDListData idata = (SilcIDListData)sock->user_data;
5043 SilcProtocol protocol;
5044 SilcServerRekeyInternalContext *proto_ctx;
5046 /* If rekey protocol is active already wait for it to finish */
5047 if (sock->protocol && sock->protocol->protocol &&
5048 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)
5051 /* Allocate internal protocol context. This is sent as context
5053 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
5054 proto_ctx->server = (void *)server;
5055 proto_ctx->sock = sock;
5056 proto_ctx->responder = FALSE;
5057 proto_ctx->pfs = idata->rekey->pfs;
5059 /* Perform rekey protocol. Will call the final callback after the
5060 protocol is over. */
5061 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
5062 &protocol, proto_ctx, silc_server_rekey_final);
5063 sock->protocol = protocol;
5065 /* Run the protocol */
5066 silc_protocol_execute(protocol, server->schedule, 0, 0);
5069 /* The final callback for the REKEY protocol. This will actually take the
5070 new key material into use. */
5072 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final)
5074 SilcProtocol protocol = (SilcProtocol)context;
5075 SilcServerRekeyInternalContext *ctx =
5076 (SilcServerRekeyInternalContext *)protocol->context;
5077 SilcServer server = (SilcServer)ctx->server;
5078 SilcSocketConnection sock = ctx->sock;
5079 SilcIDListData idata = (SilcIDListData)sock->user_data;
5081 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
5082 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
5083 /* Error occured during protocol */
5084 SILC_LOG_ERROR(("Error occurred during rekey protocol with "
5085 "%s (%s)", sock->hostname, sock->ip));
5086 silc_protocol_cancel(protocol, server->schedule);
5087 silc_protocol_free(protocol);
5088 sock->protocol = NULL;
5090 silc_packet_context_free(ctx->packet);
5092 silc_ske_free(ctx->ske);
5094 silc_server_disconnect_remote(server, sock,
5095 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
5098 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
5099 silc_server_create_connections(server);
5103 SILC_LOG_DEBUG(("Rekey protocol completed"));
5105 /* Purge the outgoing data queue to assure that all rekey packets really
5106 go to the network before we quit the protocol. */
5107 silc_server_packet_queue_purge(server, sock);
5109 /* Re-register re-key timeout */
5110 if (ctx->responder == FALSE)
5111 silc_schedule_task_add(server->schedule, sock->sock,
5112 silc_server_rekey_callback,
5113 sock, idata->rekey->timeout, 0,
5114 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
5117 silc_protocol_free(protocol);
5118 sock->protocol = NULL;
5120 silc_packet_context_free(ctx->packet);
5122 silc_ske_free(ctx->ske);
5126 /* Task callback used to retrieve network statistical information from
5127 router server once in a while. */
5129 SILC_TASK_CALLBACK(silc_server_get_stats)
5131 SilcServer server = (SilcServer)context;
5132 SilcBuffer idp, packet;
5134 if (!server->standalone) {
5135 SILC_LOG_DEBUG(("Retrieving stats from router"));
5136 idp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
5137 packet = silc_command_payload_encode_va(SILC_COMMAND_STATS,
5138 ++server->cmd_ident, 1,
5139 1, idp->data, idp->len);
5140 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
5141 SILC_PACKET_COMMAND, 0, packet->data,
5142 packet->len, FALSE);
5143 silc_buffer_free(packet);
5144 silc_buffer_free(idp);
5147 silc_schedule_task_add(server->schedule, 0, silc_server_get_stats,
5148 server, 120, 0, SILC_TASK_TIMEOUT,