5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * This is the actual SILC server than handles everything relating to
22 * servicing the SILC connections. This is also a SILC router as a router
23 * is also normal server.
27 #include "serverincludes.h"
28 #include "server_internal.h"
30 /* Static prototypes */
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);
34 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
35 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
36 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
37 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
38 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
39 SILC_TASK_CALLBACK(silc_server_packet_process);
40 SILC_TASK_CALLBACK(silc_server_packet_parse_real);
41 SILC_TASK_CALLBACK(silc_server_close_connection_final);
42 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout);
43 SILC_TASK_CALLBACK(silc_server_timeout_remote);
44 SILC_TASK_CALLBACK(silc_server_channel_key_rekey);
45 SILC_TASK_CALLBACK(silc_server_failure_callback);
46 SILC_TASK_CALLBACK(silc_server_rekey_callback);
47 SILC_TASK_CALLBACK(silc_server_get_stats);
49 /* Allocates a new SILC server object. This has to be done before the server
50 can be used. After allocation one must call silc_server_init to initialize
51 the server. The new allocated server object is returned to the new_server
54 int silc_server_alloc(SilcServer *new_server)
58 SILC_LOG_DEBUG(("Allocating new server object"));
60 server = silc_calloc(1, sizeof(*server));
61 server->server_type = SILC_SERVER;
62 server->standalone = TRUE;
63 server->local_list = silc_calloc(1, sizeof(*server->local_list));
64 server->global_list = silc_calloc(1, sizeof(*server->global_list));
65 server->pending_commands = silc_dlist_init();
67 server->sim = silc_dlist_init();
75 /* Free's the SILC server object. This is called at the very end before
78 void silc_server_free(SilcServer server)
84 while ((sim = silc_dlist_get(server->sim)) != SILC_LIST_END) {
85 silc_dlist_del(server->sim, sim);
88 silc_dlist_uninit(server->sim);
91 silc_server_config_unref(&server->config_ref);
93 silc_rng_free(server->rng);
95 silc_pkcs_free(server->pkcs);
96 if (server->public_key)
97 silc_pkcs_public_key_free(server->public_key);
98 if (server->private_key)
99 silc_pkcs_private_key_free(server->private_key);
100 if (server->pending_commands)
101 silc_dlist_uninit(server->pending_commands);
102 if (server->id_entry)
103 silc_idlist_del_server(server->local_list, server->id_entry);
105 silc_idcache_free(server->local_list->clients);
106 silc_idcache_free(server->local_list->servers);
107 silc_idcache_free(server->local_list->channels);
108 silc_idcache_free(server->global_list->clients);
109 silc_idcache_free(server->global_list->servers);
110 silc_idcache_free(server->global_list->channels);
111 silc_hash_table_free(server->watcher_list);
113 silc_free(server->sockets);
118 /* Creates a new server listener. */
120 static bool silc_server_listen(SilcServer server, const char *server_ip,
121 SilcUInt16 port, int *sock)
123 *sock = silc_net_create_server(port, server_ip);
125 SILC_LOG_ERROR(("Could not create server listener: %s on %hu",
132 /* Adds a secondary listener. */
133 bool silc_server_init_secondary(SilcServer server)
135 int sock=0, sock_list[server->config->param.connections_max];
136 SilcSocketConnection newsocket = NULL;
137 SilcServerConfigServerInfoInterface *interface;
139 for (interface = server->config->server_info->secondary; interface;
140 interface = interface->next, sock++) {
142 if (!silc_server_listen(server,
143 interface->server_ip, interface->port, &sock_list[sock]))
146 /* Set socket to non-blocking mode */
147 silc_net_set_socket_nonblock(sock_list[sock]);
149 /* Add ourselves also to the socket table. The entry allocated above
150 is sent as argument for fast referencing in the future. */
151 silc_socket_alloc(sock_list[sock],
152 SILC_SOCKET_TYPE_SERVER, NULL, &newsocket);
153 server->sockets[sock_list[sock]] = newsocket;
155 /* Perform name and address lookups to resolve the listenning address
157 if (!silc_net_check_local_by_sock(sock_list[sock], &newsocket->hostname,
159 if ((server->config->require_reverse_lookup && !newsocket->hostname) ||
161 SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
162 newsocket->hostname ? newsocket->hostname :
163 newsocket->ip ? newsocket->ip : ""));
164 server->stat.conn_failures++;
167 if (!newsocket->hostname)
168 newsocket->hostname = strdup(newsocket->ip);
170 newsocket->port = silc_net_get_local_port(sock);
172 newsocket->user_data = (void *)server->id_entry;
173 silc_schedule_task_add(server->schedule, sock_list[sock],
174 silc_server_accept_new_connection,
175 (void *)server, 0, 0,
177 SILC_TASK_PRI_NORMAL);
185 do silc_net_close_server(sock_list[sock--]); while (sock >= 0);
190 /* Initializes the entire SILC server. This is called always before running
191 the server. This is called only once at the initialization of the program.
192 This binds the server to its listenning port. After this function returns
193 one should call silc_server_run to start the server. This returns TRUE
194 when everything is ok to run the server. Configuration file must be
195 read and parsed before calling this. */
197 bool silc_server_init(SilcServer server)
201 SilcServerEntry id_entry;
202 SilcIDListPurge purge;
203 SilcSocketConnection newsocket = NULL;
205 SILC_LOG_DEBUG(("Initializing server"));
207 server->starttime = time(NULL);
209 /* Take config object for us */
210 silc_server_config_ref(&server->config_ref, server->config,
213 /* Steal public and private key from the config object */
214 server->public_key = server->config->server_info->public_key;
215 server->private_key = server->config->server_info->private_key;
216 server->config->server_info->public_key = NULL;
217 server->config->server_info->private_key = NULL;
219 /* Register all configured ciphers, PKCS and hash functions. */
220 if (!silc_server_config_register_ciphers(server))
221 silc_cipher_register_default();
222 if (!silc_server_config_register_pkcs(server))
223 silc_pkcs_register_default();
224 if (!silc_server_config_register_hashfuncs(server))
225 silc_hash_register_default();
226 if (!silc_server_config_register_hmacs(server))
227 silc_hmac_register_default();
229 /* Initialize random number generator for the server. */
230 server->rng = silc_rng_alloc();
231 silc_rng_init(server->rng);
232 silc_rng_global_init(server->rng);
234 /* Initialize hash functions for server to use */
235 silc_hash_alloc("md5", &server->md5hash);
236 silc_hash_alloc("sha1", &server->sha1hash);
238 /* Allocate PKCS context for local public and private keys */
239 if (!silc_pkcs_alloc(server->public_key->name, &server->pkcs))
241 silc_pkcs_public_key_set(server->pkcs, server->public_key);
242 silc_pkcs_private_key_set(server->pkcs, server->private_key);
244 /* Initialize the scheduler */
245 server->schedule = silc_schedule_init(server->config->param.connections_max);
246 if (!server->schedule)
249 /* First, register log files configuration for error output */
250 silc_server_config_setlogfiles(server);
252 /* Initialize ID caches */
253 server->local_list->clients =
254 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
255 server->local_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
256 server->local_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
258 /* These are allocated for normal server as well as these hold some
259 global information that the server has fetched from its router. For
260 router these are used as they are supposed to be used on router. */
261 server->global_list->clients =
262 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
263 server->global_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
264 server->global_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
266 /* Init watcher list */
267 server->watcher_list =
268 silc_hash_table_alloc(1, silc_hash_client_id_hash, NULL,
269 silc_hash_data_compare, (void *)CLIENTID_HASH_LEN,
271 if (!server->watcher_list)
274 /* Create a listening server */
275 if (!silc_server_listen(server,
276 server->config->server_info->primary == NULL ? NULL :
277 server->config->server_info->primary->server_ip,
278 server->config->server_info->primary == NULL ? 0 :
279 server->config->server_info->primary->port,
283 /* Set socket to non-blocking mode */
284 silc_net_set_socket_nonblock(sock);
287 /* Allocate the entire socket list that is used in server. Eventually
288 all connections will have entry in this table (it is a table of
289 pointers to the actual object that is allocated individually
291 server->sockets = silc_calloc(server->config->param.connections_max,
292 sizeof(*server->sockets));
293 if (!server->sockets)
296 /* Add ourselves also to the socket table. The entry allocated above
297 is sent as argument for fast referencing in the future. */
298 silc_socket_alloc(sock, SILC_SOCKET_TYPE_SERVER, NULL, &newsocket);
299 server->sockets[sock] = newsocket;
301 /* Perform name and address lookups to resolve the listenning address
303 if (!silc_net_check_local_by_sock(sock, &newsocket->hostname,
305 if ((server->config->require_reverse_lookup && !newsocket->hostname) ||
307 SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
308 newsocket->hostname ? newsocket->hostname :
309 newsocket->ip ? newsocket->ip : ""));
310 server->stat.conn_failures++;
313 if (!newsocket->hostname)
314 newsocket->hostname = strdup(newsocket->ip);
316 newsocket->port = silc_net_get_local_port(sock);
318 /* Create a Server ID for the server. */
319 silc_id_create_server_id(newsocket->ip, newsocket->port, server->rng, &id);
324 server->id_string = silc_id_id2str(id, SILC_ID_SERVER);
325 server->id_string_len = silc_id_get_len(id, SILC_ID_SERVER);
326 server->id_type = SILC_ID_SERVER;
327 server->server_name = server->config->server_info->server_name;
328 server->config->server_info->server_name = NULL;
330 /* Add ourselves to the server list. We don't have a router yet
331 beacuse we haven't established a route yet. It will be done later.
332 For now, NULL is sent as router. This allocates new entry to
335 silc_idlist_add_server(server->local_list, strdup(server->server_name),
336 server->server_type, server->id, NULL, NULL);
338 SILC_LOG_ERROR(("Could not add ourselves to cache"));
341 id_entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
343 /* Put the allocated socket pointer also to the entry allocated above
344 for fast back-referencing to the socket list. */
345 newsocket->user_data = (void *)id_entry;
346 id_entry->connection = (void *)newsocket;
347 server->id_entry = id_entry;
349 /* Register protocols */
350 silc_server_protocols_register();
352 /* Add the first task to the scheduler. This is task that is executed by
353 timeout. It expires as soon as the caller calls silc_server_run. This
354 task performs authentication protocol and key exchange with our
356 silc_schedule_task_add(server->schedule, 0,
357 silc_server_connect_to_router,
358 (void *)server, 0, 1,
360 SILC_TASK_PRI_NORMAL);
362 /* Add listener task to the scheduler. This task receives new connections
363 to the server. This task remains on the queue until the end of the
365 silc_schedule_task_add(server->schedule, sock,
366 silc_server_accept_new_connection,
367 (void *)server, 0, 0,
369 SILC_TASK_PRI_NORMAL);
371 if (silc_server_init_secondary(server) == FALSE)
374 server->listenning = TRUE;
376 /* If server connections has been configured then we must be router as
377 normal server cannot have server connections, only router connections. */
378 if (server->config->servers) {
379 SilcServerConfigServer *ptr = server->config->servers;
381 server->server_type = SILC_ROUTER;
383 if (ptr->backup_router) {
384 server->server_type = SILC_BACKUP_ROUTER;
385 server->backup_router = TRUE;
386 server->id_entry->server_type = SILC_BACKUP_ROUTER;
393 /* Register the ID Cache purge task. This periodically purges the ID cache
394 and removes the expired cache entries. */
396 /* Clients local list */
397 purge = silc_calloc(1, sizeof(*purge));
398 purge->cache = server->local_list->clients;
399 purge->schedule = server->schedule;
400 purge->timeout = 600;
401 silc_schedule_task_add(purge->schedule, 0,
403 (void *)purge, purge->timeout, 0,
404 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
406 /* Clients global list */
407 purge = silc_calloc(1, sizeof(*purge));
408 purge->cache = server->global_list->clients;
409 purge->schedule = server->schedule;
410 purge->timeout = 300;
411 silc_schedule_task_add(purge->schedule, 0,
413 (void *)purge, purge->timeout, 0,
414 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
416 /* If we are normal server we'll retrieve network statisticial information
417 once in a while from the router. */
418 if (server->server_type == SILC_SERVER)
419 silc_schedule_task_add(purge->schedule, 0, silc_server_get_stats,
420 server, 10, 0, SILC_TASK_TIMEOUT,
423 if (server->server_type == SILC_ROUTER)
424 server->stat.routers++;
426 SILC_LOG_DEBUG(("Server initialized"));
428 /* We are done here, return succesfully */
432 silc_server_config_unref(&server->config_ref);
433 silc_net_close_server(sock);
437 /* This function basically reads the config file again and switches the config
438 object pointed by the server object. After that, we have to fix various
439 things such as the server_name and the listening ports.
440 Keep in mind that we no longer have the root privileges at this point. */
442 bool silc_server_rehash(SilcServer server)
444 SilcServerConfig newconfig;
446 SILC_LOG_INFO(("Rehashing server"));
448 /* Reset the logging system */
449 silc_log_quick = TRUE;
450 silc_log_flush_all();
452 /* Start the main rehash phase (read again the config file) */
453 newconfig = silc_server_config_alloc(server->config_file);
455 SILC_LOG_ERROR(("Rehash FAILED."));
459 /* Reinit scheduler if necessary */
460 if (newconfig->param.connections_max > server->config->param.connections_max)
461 if (!silc_schedule_reinit(server->schedule,
462 newconfig->param.connections_max))
465 /* Fix the server_name field */
466 if (strcmp(server->server_name, newconfig->server_info->server_name)) {
467 silc_free(server->server_name);
468 server->server_name = newconfig->server_info->server_name;
469 newconfig->server_info->server_name = NULL;
471 /* Update the idcache list with a fresh pointer */
472 silc_free(server->id_entry->server_name);
473 server->id_entry->server_name = strdup(server->server_name);
474 if (!silc_idcache_del_by_context(server->local_list->servers,
477 if (!silc_idcache_add(server->local_list->servers,
478 server->id_entry->server_name,
479 server->id_entry->id, server->id_entry, 0, NULL))
484 silc_server_config_setlogfiles(server);
486 /* Change new key pair if necessary */
487 if (newconfig->server_info->public_key &&
488 !silc_pkcs_public_key_compare(server->public_key,
489 newconfig->server_info->public_key)) {
490 silc_pkcs_public_key_free(server->public_key);
491 silc_pkcs_private_key_free(server->private_key);
492 server->public_key = newconfig->server_info->public_key;
493 server->private_key = newconfig->server_info->private_key;
494 newconfig->server_info->public_key = NULL;
495 newconfig->server_info->private_key = NULL;
497 /* Allocate PKCS context for local public and private keys */
498 silc_pkcs_free(server->pkcs);
499 if (!silc_pkcs_alloc(server->public_key->name, &server->pkcs))
501 silc_pkcs_public_key_set(server->pkcs, server->public_key);
502 silc_pkcs_private_key_set(server->pkcs, server->private_key);
505 /* Go through all configured routers after rehash */
506 silc_schedule_task_add(server->schedule, 0,
507 silc_server_connect_to_router,
508 (void *)server, 0, 1,
510 SILC_TASK_PRI_NORMAL);
512 /* Check whether our router status has changed */
513 if (newconfig->servers) {
514 SilcServerConfigServer *ptr = newconfig->servers;
516 server->server_type = SILC_ROUTER;
518 if (ptr->backup_router) {
519 server->server_type = SILC_BACKUP_ROUTER;
520 server->backup_router = TRUE;
521 server->id_entry->server_type = SILC_BACKUP_ROUTER;
528 /* Our old config is gone now. We'll unreference our reference made in
529 silc_server_init and then destroy it since we are destroying it
530 underneath the application (layer which called silc_server_init). */
531 silc_server_config_unref(&server->config_ref);
532 silc_server_config_destroy(server->config);
534 /* Take new config context */
535 server->config = newconfig;
536 silc_server_config_ref(&server->config_ref, server->config, server->config);
538 SILC_LOG_DEBUG(("Server rehashed"));
543 /* The heart of the server. This runs the scheduler thus runs the server.
544 When this returns the server has been stopped and the program will
547 void silc_server_run(SilcServer server)
549 SILC_LOG_INFO(("SILC Server started"));
551 /* Start the scheduler, the heart of the SILC server. When this returns
552 the program will be terminated. */
553 silc_schedule(server->schedule);
556 /* Stops the SILC server. This function is used to shutdown the server.
557 This is usually called after the scheduler has returned. After stopping
558 the server one should call silc_server_free. */
560 void silc_server_stop(SilcServer server)
562 SILC_LOG_DEBUG(("Stopping server"));
564 if (server->schedule) {
565 silc_schedule_stop(server->schedule);
566 silc_schedule_uninit(server->schedule);
567 server->schedule = NULL;
570 silc_server_protocols_unregister();
572 SILC_LOG_DEBUG(("Server stopped"));
575 /* Function that is called when the network connection to a router has
576 been established. This will continue with the key exchange protocol
577 with the remote router. */
579 void silc_server_start_key_exchange(SilcServer server,
580 SilcServerConnection sconn,
583 SilcSocketConnection newsocket;
584 SilcProtocol protocol;
585 SilcServerKEInternalContext *proto_ctx;
586 SilcServerConfigRouter *conn =
587 (SilcServerConfigRouter *) sconn->conn.ref_ptr;
590 /* Cancel any possible retry timeouts */
591 silc_schedule_task_del_by_callback(server->schedule,
592 silc_server_connect_to_router_retry);
594 /* Set socket options */
595 silc_net_set_socket_nonblock(sock);
596 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
598 /* Create socket connection for the connection. Even though we
599 know that we are connecting to a router we will mark the socket
600 to be unknown connection until we have executed authentication
602 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
603 server->sockets[sock] = newsocket;
604 newsocket->hostname = strdup(sconn->remote_host);
605 newsocket->ip = strdup(sconn->remote_host);
606 newsocket->port = sconn->remote_port;
607 sconn->sock = newsocket;
609 /* Allocate internal protocol context. This is sent as context
611 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
612 proto_ctx->server = (void *)server;
613 proto_ctx->context = (void *)sconn;
614 proto_ctx->sock = newsocket;
615 proto_ctx->rng = server->rng;
616 proto_ctx->responder = FALSE;
618 /* Set Key Exchange flags from configuration, but fall back to global
620 SILC_GET_SKE_FLAGS(conn, proto_ctx);
621 if (server->config->param.key_exchange_pfs)
622 proto_ctx->flags |= SILC_SKE_SP_FLAG_PFS;
624 /* Perform key exchange protocol. silc_server_connect_to_router_second
625 will be called after the protocol is finished. */
626 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
627 &protocol, proto_ctx,
628 silc_server_connect_to_router_second);
629 newsocket->protocol = protocol;
631 /* Register a timeout task that will be executed if the protocol
632 is not executed within set limit. */
633 proto_ctx->timeout_task =
634 silc_schedule_task_add(server->schedule, sock,
635 silc_server_timeout_remote,
636 server, server->config->key_exchange_timeout, 0,
640 /* Register the connection for network input and output. This sets
641 that scheduler will listen for incoming packets for this connection
642 and sets that outgoing packets may be sent to this connection as
643 well. However, this doesn't set the scheduler for outgoing traffic,
644 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
645 later when outgoing data is available. */
646 context = (void *)server;
647 SILC_REGISTER_CONNECTION_FOR_IO(sock);
649 /* Run the protocol */
650 silc_protocol_execute(protocol, server->schedule, 0, 0);
653 /* Timeout callback that will be called to retry connecting to remote
654 router. This is used by both normal and router server. This will wait
655 before retrying the connecting. The timeout is generated by exponential
656 backoff algorithm. */
658 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry)
660 SilcServerConnection sconn = (SilcServerConnection)context;
661 SilcServer server = sconn->server;
662 SilcServerConfigRouter *conn = sconn->conn.ref_ptr;
663 SilcServerConfigConnParams *param =
664 (conn->param ? conn->param : &server->config->param);
666 SILC_LOG_INFO(("Retrying connecting to a router"));
668 /* Calculate next timeout */
669 if (sconn->retry_count >= 1) {
670 sconn->retry_timeout = sconn->retry_timeout * SILC_SERVER_RETRY_MULTIPLIER;
671 if (sconn->retry_timeout > param->reconnect_interval_max)
672 sconn->retry_timeout = param->reconnect_interval_max;
674 sconn->retry_timeout = param->reconnect_interval;
676 sconn->retry_count++;
677 sconn->retry_timeout = sconn->retry_timeout +
678 silc_rng_get_rn32(server->rng) % SILC_SERVER_RETRY_RANDOMIZER;
680 /* If we've reached max retry count, give up. */
681 if ((sconn->retry_count > param->reconnect_count) &&
682 !param->reconnect_keep_trying) {
683 SILC_LOG_ERROR(("Could not connect to router, giving up"));
684 silc_server_config_unref(&sconn->conn);
685 silc_free(sconn->remote_host);
686 silc_free(sconn->backup_replace_ip);
691 /* We will lookup a fresh pointer later */
692 silc_server_config_unref(&sconn->conn);
694 /* Wait one before retrying */
695 silc_schedule_task_add(server->schedule, 0, silc_server_connect_router,
696 context, sconn->retry_timeout, 0,
697 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
700 /* Generic routine to use connect to a router. */
702 SILC_TASK_CALLBACK(silc_server_connect_router)
704 SilcServerConnection sconn = (SilcServerConnection)context;
705 SilcServer server = sconn->server;
706 SilcServerConfigRouter *rconn;
709 SILC_LOG_INFO(("Connecting to the %s %s on port %d",
710 (sconn->backup ? "backup router" : "router"),
711 sconn->remote_host, sconn->remote_port));
713 server->router_connect = time(NULL);
714 rconn = silc_server_config_find_router_conn(server, sconn->remote_host,
717 SILC_LOG_INFO(("Unconfigured %s connection %s:%d, cannot connect",
718 (sconn->backup ? "backup router" : "router"),
719 sconn->remote_host, sconn->remote_port));
720 silc_free(sconn->remote_host);
721 silc_free(sconn->backup_replace_ip);
725 silc_server_config_ref(&sconn->conn, server->config, (void *)rconn);
727 /* Connect to remote host */
728 sock = silc_net_create_connection(
729 (!server->config->server_info->primary ? NULL :
730 server->config->server_info->primary->server_ip),
731 sconn->remote_port, sconn->remote_host);
733 SILC_LOG_ERROR(("Could not connect to router %s:%d",
734 sconn->remote_host, sconn->remote_port));
735 if (!sconn->no_reconnect)
736 silc_schedule_task_add(server->schedule, 0,
737 silc_server_connect_to_router_retry,
738 context, 0, 1, SILC_TASK_TIMEOUT,
739 SILC_TASK_PRI_NORMAL);
741 silc_server_config_unref(&sconn->conn);
745 /* Continue with key exchange protocol */
746 silc_server_start_key_exchange(server, sconn, sock);
749 /* This function connects to our primary router or if we are a router this
750 establishes all our primary routes. This is called at the start of the
751 server to do authentication and key exchange with our router - called
754 SILC_TASK_CALLBACK(silc_server_connect_to_router)
756 SilcServer server = (SilcServer)context;
757 SilcServerConnection sconn;
758 SilcServerConfigRouter *ptr;
760 SILC_LOG_DEBUG(("Connecting to router(s)"));
762 if (server->server_type == SILC_SERVER) {
763 SILC_LOG_DEBUG(("We are normal server"));
764 } else if (server->server_type == SILC_ROUTER) {
765 SILC_LOG_DEBUG(("We are router"));
767 SILC_LOG_DEBUG(("We are backup router/normal server"));
770 if (!server->config->routers) {
771 /* There wasn't a configured router, we will continue but we don't
772 have a connection to outside world. We will be standalone server. */
773 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
774 server->standalone = TRUE;
778 /* Cancel any possible retry timeouts */
779 silc_schedule_task_del_by_callback(server->schedule,
780 silc_server_connect_router);
781 silc_schedule_task_del_by_callback(server->schedule,
782 silc_server_connect_to_router_retry);
784 /* Create the connections to all our routes */
785 for (ptr = server->config->routers; ptr; ptr = ptr->next) {
787 SILC_LOG_DEBUG(("%s connection [%s] %s:%d",
788 ptr->backup_router ? "Backup router" : "Router",
789 ptr->initiator ? "Initiator" : "Responder",
790 ptr->host, ptr->port));
792 if (ptr->initiator) {
793 /* Check whether we are connected to this host already */
794 if (silc_server_num_sockets_by_remote(server,
795 silc_net_is_ip(ptr->host) ?
797 silc_net_is_ip(ptr->host) ?
798 NULL : ptr->host, ptr->port,
799 SILC_SOCKET_TYPE_ROUTER)) {
800 SILC_LOG_DEBUG(("We are already connected to this router"));
804 /* Allocate connection object for hold connection specific stuff. */
805 sconn = silc_calloc(1, sizeof(*sconn));
806 sconn->server = server;
807 sconn->remote_host = strdup(ptr->host);
808 sconn->remote_port = ptr->port;
809 sconn->backup = ptr->backup_router;
811 sconn->backup_replace_ip = strdup(ptr->backup_replace_ip);
812 sconn->backup_replace_port = ptr->backup_replace_port;
815 if (!server->router_conn && !sconn->backup)
816 server->router_conn = sconn;
818 silc_schedule_task_add(server->schedule, 0,
819 silc_server_connect_router,
820 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
821 SILC_TASK_PRI_NORMAL);
826 /* Second part of connecting to router(s). Key exchange protocol has been
827 executed and now we will execute authentication protocol. */
829 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
831 SilcProtocol protocol = (SilcProtocol)context;
832 SilcServerKEInternalContext *ctx =
833 (SilcServerKEInternalContext *)protocol->context;
834 SilcServer server = (SilcServer)ctx->server;
835 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
836 SilcSocketConnection sock = ctx->sock;
837 SilcServerConnAuthInternalContext *proto_ctx;
838 SilcServerConfigRouter *conn = NULL;
840 SILC_LOG_DEBUG(("Start"));
842 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
843 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
844 /* Error occured during protocol */
845 silc_protocol_free(protocol);
846 sock->protocol = NULL;
847 silc_ske_free_key_material(ctx->keymat);
849 silc_packet_context_free(ctx->packet);
851 silc_ske_free(ctx->ske);
852 silc_free(ctx->dest_id);
854 silc_server_config_unref(&sconn->conn);
855 silc_free(sconn->remote_host);
856 silc_free(sconn->backup_replace_ip);
858 silc_schedule_task_del_by_callback(server->schedule,
859 silc_server_failure_callback);
860 silc_server_disconnect_remote(server, sock,
861 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
865 /* We now have the key material as the result of the key exchange
866 protocol. Take the key material into use. Free the raw key material
867 as soon as we've set them into use. */
868 if (!silc_server_protocol_ke_set_keys(server, ctx->ske,
869 ctx->sock, ctx->keymat,
870 ctx->ske->prop->cipher,
871 ctx->ske->prop->pkcs,
872 ctx->ske->prop->hash,
873 ctx->ske->prop->hmac,
874 ctx->ske->prop->group,
876 silc_protocol_free(protocol);
877 sock->protocol = NULL;
878 silc_ske_free_key_material(ctx->keymat);
880 silc_packet_context_free(ctx->packet);
882 silc_ske_free(ctx->ske);
883 silc_free(ctx->dest_id);
885 silc_server_config_unref(&sconn->conn);
886 silc_free(sconn->remote_host);
887 silc_free(sconn->backup_replace_ip);
889 silc_schedule_task_del_by_callback(server->schedule,
890 silc_server_failure_callback);
891 silc_server_disconnect_remote(server, sock,
892 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
895 silc_ske_free_key_material(ctx->keymat);
897 /* Allocate internal context for the authentication protocol. This
898 is sent as context for the protocol. */
899 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
900 proto_ctx->server = (void *)server;
901 proto_ctx->context = (void *)sconn;
902 proto_ctx->sock = sock;
903 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
904 proto_ctx->dest_id_type = ctx->dest_id_type;
905 proto_ctx->dest_id = ctx->dest_id;
907 /* Resolve the authentication method used in this connection. Check if
908 we find a match from user configured connections */
909 if (!sconn->conn.ref_ptr)
910 conn = silc_server_config_find_router_conn(server, sock->hostname,
913 conn = sconn->conn.ref_ptr;
916 /* Match found. Use the configured authentication method. Take only
917 the passphrase, since for public key auth we automatically use
918 our local key pair. */
919 if (conn->passphrase) {
920 if (conn->publickeys && !server->config->prefer_passphrase_auth) {
921 proto_ctx->auth_meth = SILC_AUTH_PUBLIC_KEY;
923 proto_ctx->auth_data = strdup(conn->passphrase);
924 proto_ctx->auth_data_len = strlen(conn->passphrase);
925 proto_ctx->auth_meth = SILC_AUTH_PASSWORD;
927 } else if (conn->publickeys) {
928 proto_ctx->auth_meth = SILC_AUTH_PUBLIC_KEY;
930 proto_ctx->auth_meth = SILC_AUTH_NONE;
933 SILC_LOG_ERROR(("Could not find connection data for %s (%s) on port",
934 sock->hostname, sock->ip, sock->port));
935 silc_protocol_free(protocol);
936 sock->protocol = NULL;
938 silc_packet_context_free(ctx->packet);
940 silc_ske_free(ctx->ske);
941 silc_free(ctx->dest_id);
943 silc_server_config_unref(&sconn->conn);
944 silc_free(sconn->remote_host);
945 silc_free(sconn->backup_replace_ip);
947 silc_schedule_task_del_by_callback(server->schedule,
948 silc_server_failure_callback);
949 silc_server_disconnect_remote(server, sock,
950 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
954 /* Free old protocol as it is finished now */
955 silc_protocol_free(protocol);
957 silc_packet_context_free(ctx->packet);
959 sock->protocol = NULL;
961 /* Allocate the authentication protocol. This is allocated here
962 but we won't start it yet. We will be receiving party of this
963 protocol thus we will wait that connecting party will make
965 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
966 &sock->protocol, proto_ctx,
967 silc_server_connect_to_router_final);
969 /* Register timeout task. If the protocol is not executed inside
970 this timelimit the connection will be terminated. */
971 proto_ctx->timeout_task =
972 silc_schedule_task_add(server->schedule, sock->sock,
973 silc_server_timeout_remote,
975 server->config->conn_auth_timeout, 0,
979 /* Run the protocol */
980 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
983 /* Finalizes the connection to router. Registers a server task to the
984 queue so that we can accept new connections. */
986 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
988 SilcProtocol protocol = (SilcProtocol)context;
989 SilcServerConnAuthInternalContext *ctx =
990 (SilcServerConnAuthInternalContext *)protocol->context;
991 SilcServer server = (SilcServer)ctx->server;
992 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
993 SilcSocketConnection sock = ctx->sock;
994 SilcServerEntry id_entry;
996 SilcServerHBContext hb_context;
997 unsigned char *id_string;
999 SilcIDListData idata;
1000 SilcServerConfigRouter *conn = NULL;
1001 SilcServerConfigConnParams *param = NULL;
1003 SILC_LOG_DEBUG(("Start"));
1005 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1006 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1007 /* Error occured during protocol */
1008 silc_free(ctx->dest_id);
1009 silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1014 /* Add a task to the queue. This task receives new connections to the
1015 server. This task remains on the queue until the end of the program. */
1016 if (!server->listenning && !sconn->backup) {
1017 silc_schedule_task_add(server->schedule, server->sock,
1018 silc_server_accept_new_connection,
1019 (void *)server, 0, 0,
1021 SILC_TASK_PRI_NORMAL);
1022 server->listenning = TRUE;
1025 /* Send NEW_SERVER packet to the router. We will become registered
1026 to the SILC network after sending this packet. */
1027 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
1028 id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1029 packet = silc_buffer_alloc(2 + 2 + id_len + strlen(server->server_name));
1030 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1031 silc_buffer_format(packet,
1032 SILC_STR_UI_SHORT(id_len),
1033 SILC_STR_UI_XNSTRING(id_string, id_len),
1034 SILC_STR_UI_SHORT(strlen(server->server_name)),
1035 SILC_STR_UI_XNSTRING(server->server_name,
1036 strlen(server->server_name)),
1039 /* Send the packet */
1040 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
1041 packet->data, packet->len, TRUE);
1042 silc_buffer_free(packet);
1043 silc_free(id_string);
1045 SILC_LOG_INFO(("Connected to router %s", sock->hostname));
1047 /* Check that we do not have this ID already */
1048 id_entry = silc_idlist_find_server_by_id(server->local_list,
1049 ctx->dest_id, TRUE, NULL);
1051 silc_idcache_del_by_context(server->local_list->servers, id_entry);
1053 id_entry = silc_idlist_find_server_by_id(server->global_list,
1054 ctx->dest_id, TRUE, NULL);
1056 silc_idcache_del_by_context(server->global_list->servers, id_entry);
1059 SILC_LOG_DEBUG(("New server id(%s)",
1060 silc_id_render(ctx->dest_id, SILC_ID_SERVER)));
1062 /* Add the connected router to global server list */
1063 id_entry = silc_idlist_add_server(server->global_list,
1064 strdup(sock->hostname),
1065 SILC_ROUTER, ctx->dest_id, NULL, sock);
1067 silc_free(ctx->dest_id);
1068 SILC_LOG_ERROR(("Cannot add new server entry to cache"));
1069 silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1074 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1075 silc_free(sock->user_data);
1076 sock->user_data = (void *)id_entry;
1077 sock->type = SILC_SOCKET_TYPE_ROUTER;
1078 idata = (SilcIDListData)sock->user_data;
1079 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1081 conn = sconn->conn.ref_ptr;
1082 param = &server->config->param;
1083 if (conn && conn->param)
1084 param = conn->param;
1086 /* Perform keepalive. The `hb_context' will be freed automatically
1087 when finally calling the silc_socket_free function. */
1088 hb_context = silc_calloc(1, sizeof(*hb_context));
1089 hb_context->server = server;
1090 silc_socket_set_heartbeat(sock, param->keepalive_secs, hb_context,
1091 silc_server_perform_heartbeat,
1094 /* Register re-key timeout */
1095 idata->rekey->timeout = param->key_exchange_rekey;
1096 idata->rekey->context = (void *)server;
1097 silc_schedule_task_add(server->schedule, sock->sock,
1098 silc_server_rekey_callback,
1099 (void *)sock, idata->rekey->timeout, 0,
1100 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1102 if (!sconn->backup) {
1103 /* Mark this router our primary router if we're still standalone */
1104 if (server->standalone) {
1105 server->id_entry->router = id_entry;
1106 server->router = id_entry;
1107 server->standalone = FALSE;
1109 /* If we are router then announce our possible servers. */
1110 if (server->server_type == SILC_ROUTER)
1111 silc_server_announce_servers(server, FALSE, 0,
1112 server->router->connection);
1114 /* Announce our clients and channels to the router */
1115 silc_server_announce_clients(server, 0, server->router->connection);
1116 silc_server_announce_channels(server, 0, server->router->connection);
1118 #ifdef BACKUP_SINGLE_ROUTER
1119 /* If we are backup router then this primary router is whom we are
1121 if (server->server_type == SILC_BACKUP_ROUTER)
1122 silc_server_backup_add(server, server->id_entry, sock->ip, 0, TRUE);
1123 #endif /* BACKUP_SINGLE_ROUTER */
1126 /* Add this server to be our backup router */
1127 silc_server_backup_add(server, id_entry, sconn->backup_replace_ip,
1128 sconn->backup_replace_port, FALSE);
1131 sock->protocol = NULL;
1133 /* Call the completion callback to indicate that we've connected to
1135 if (sconn->callback)
1136 (*sconn->callback)(server, id_entry, sconn->callback_context);
1139 /* Free the temporary connection data context */
1141 silc_server_config_unref(&sconn->conn);
1142 silc_free(sconn->remote_host);
1143 silc_free(sconn->backup_replace_ip);
1146 if (sconn == server->router_conn)
1147 server->router_conn = NULL;
1149 /* Free the protocol object */
1150 if (sock->protocol == protocol)
1151 sock->protocol = NULL;
1152 silc_protocol_free(protocol);
1154 silc_packet_context_free(ctx->packet);
1156 silc_ske_free(ctx->ske);
1157 if (ctx->auth_meth == SILC_AUTH_PASSWORD)
1158 silc_free(ctx->auth_data);
1162 /* Host lookup callback that is called after the incoming connection's
1163 IP and FQDN lookup is performed. This will actually check the acceptance
1164 of the incoming connection and will register the key exchange protocol
1165 for this connection. */
1168 silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
1171 SilcServerKEInternalContext *proto_ctx =
1172 (SilcServerKEInternalContext *)context;
1173 SilcServer server = (SilcServer)proto_ctx->server;
1174 SilcServerConfigClient *cconfig = NULL;
1175 SilcServerConfigServer *sconfig = NULL;
1176 SilcServerConfigRouter *rconfig = NULL;
1177 SilcServerConfigDeny *deny;
1180 context = (void *)server;
1182 SILC_LOG_DEBUG(("Start"));
1184 /* Check whether we could resolve both IP and FQDN. */
1185 if (!sock->ip || (!strcmp(sock->ip, sock->hostname) &&
1186 server->config->require_reverse_lookup)) {
1187 SILC_LOG_ERROR(("IP/DNS lookup failed %s",
1188 sock->hostname ? sock->hostname :
1189 sock->ip ? sock->ip : ""));
1190 server->stat.conn_failures++;
1191 silc_server_disconnect_remote(server, sock,
1192 SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1193 "Unknown host or IP");
1194 silc_free(proto_ctx);
1198 /* Register the connection for network input and output. This sets
1199 that scheduler will listen for incoming packets for this connection
1200 and sets that outgoing packets may be sent to this connection as well.
1201 However, this doesn't set the scheduler for outgoing traffic, it
1202 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
1203 later when outgoing data is available. */
1204 SILC_REGISTER_CONNECTION_FOR_IO(sock->sock);
1206 SILC_LOG_INFO(("Incoming connection %s (%s)", sock->hostname,
1209 /* Listenning port */
1210 port = server->sockets[(SilcUInt32)proto_ctx->context]->port;
1212 /* Check whether this connection is denied to connect to us. */
1213 deny = silc_server_config_find_denied(server, sock->ip);
1215 deny = silc_server_config_find_denied(server, sock->hostname);
1217 /* The connection is denied */
1218 SILC_LOG_INFO(("Connection %s (%s) is denied",
1219 sock->hostname, sock->ip));
1220 silc_server_disconnect_remote(server, sock,
1221 SILC_STATUS_ERR_BANNED_FROM_SERVER,
1223 server->stat.conn_failures++;
1224 silc_free(proto_ctx);
1228 /* Check whether we have configured this sort of connection at all. We
1229 have to check all configurations since we don't know what type of
1230 connection this is. */
1231 if (!(cconfig = silc_server_config_find_client(server, sock->ip)))
1232 cconfig = silc_server_config_find_client(server, sock->hostname);
1233 if (!(sconfig = silc_server_config_find_server_conn(server, sock->ip)))
1234 sconfig = silc_server_config_find_server_conn(server, sock->hostname);
1235 if (server->server_type == SILC_ROUTER) {
1236 if (!(rconfig = silc_server_config_find_router_conn(server,
1237 sock->ip, sock->port)))
1238 rconfig = silc_server_config_find_router_conn(server, sock->hostname,
1241 if (!cconfig && !sconfig && !rconfig) {
1242 SILC_LOG_INFO(("Connection %s (%s) is not allowed", sock->hostname,
1244 silc_server_disconnect_remote(server, sock,
1245 SILC_STATUS_ERR_BANNED_FROM_SERVER, NULL);
1246 server->stat.conn_failures++;
1247 silc_free(proto_ctx);
1251 /* The connection is allowed */
1253 /* Set internal context for key exchange protocol. This is
1254 sent as context for the protocol. */
1255 proto_ctx->sock = sock;
1256 proto_ctx->rng = server->rng;
1257 proto_ctx->responder = TRUE;
1258 silc_server_config_ref(&proto_ctx->cconfig, server->config, cconfig);
1259 silc_server_config_ref(&proto_ctx->sconfig, server->config, sconfig);
1260 silc_server_config_ref(&proto_ctx->rconfig, server->config, rconfig);
1262 /* Take flags for key exchange. Since we do not know what type of connection
1263 this is, we go through all found configurations and use the global ones
1264 as well. This will result always into strictest key exchange flags. */
1265 SILC_GET_SKE_FLAGS(cconfig, proto_ctx);
1266 SILC_GET_SKE_FLAGS(sconfig, proto_ctx);
1267 SILC_GET_SKE_FLAGS(rconfig, proto_ctx);
1268 if (server->config->param.key_exchange_pfs)
1269 proto_ctx->flags |= SILC_SKE_SP_FLAG_PFS;
1271 /* Prepare the connection for key exchange protocol. We allocate the
1272 protocol but will not start it yet. The connector will be the
1273 initiator of the protocol thus we will wait for initiation from
1274 there before we start the protocol. */
1275 server->stat.auth_attempts++;
1276 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1277 &sock->protocol, proto_ctx,
1278 silc_server_accept_new_connection_second);
1280 /* Register a timeout task that will be executed if the connector
1281 will not start the key exchange protocol within specified timeout
1282 and the connection will be closed. */
1283 proto_ctx->timeout_task =
1284 silc_schedule_task_add(server->schedule, sock->sock,
1285 silc_server_timeout_remote,
1287 server->config->key_exchange_timeout, 0,
1292 /* Accepts new connections to the server. Accepting new connections are
1293 done in three parts to make it async. */
1295 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
1297 SilcServer server = (SilcServer)context;
1298 SilcSocketConnection newsocket;
1299 SilcServerKEInternalContext *proto_ctx;
1302 SILC_LOG_DEBUG(("Accepting new connection"));
1304 server->stat.conn_attempts++;
1306 sock = silc_net_accept_connection(fd);
1308 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
1309 server->stat.conn_failures++;
1313 /* Check for maximum allowed connections */
1314 if (sock > server->config->param.connections_max) {
1315 SILC_LOG_ERROR(("Refusing connection, server is full"));
1316 server->stat.conn_failures++;
1317 silc_net_close_connection(sock);
1321 /* Set socket options */
1322 silc_net_set_socket_nonblock(sock);
1323 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
1325 /* We don't create a ID yet, since we don't know what type of connection
1326 this is yet. But, we do add the connection to the socket table. */
1327 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
1328 server->sockets[sock] = newsocket;
1330 /* Perform asynchronous host lookup. This will lookup the IP and the
1331 FQDN of the remote connection. After the lookup is done the connection
1332 is accepted further. */
1333 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1334 proto_ctx->server = server;
1335 proto_ctx->context = (void *)fd;
1336 silc_socket_host_lookup(newsocket, TRUE,
1337 silc_server_accept_new_connection_lookup,
1338 (void *)proto_ctx, server->schedule);
1341 /* Second part of accepting new connection. Key exchange protocol has been
1342 performed and now it is time to do little connection authentication
1343 protocol to figure out whether this connection is client or server
1344 and whether it has right to access this server (especially server
1345 connections needs to be authenticated). */
1347 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
1349 SilcProtocol protocol = (SilcProtocol)context;
1350 SilcServerKEInternalContext *ctx =
1351 (SilcServerKEInternalContext *)protocol->context;
1352 SilcServer server = (SilcServer)ctx->server;
1353 SilcSocketConnection sock = ctx->sock;
1354 SilcServerConnAuthInternalContext *proto_ctx;
1356 SILC_LOG_DEBUG(("Start"));
1358 if ((protocol->state == SILC_PROTOCOL_STATE_ERROR) ||
1359 (protocol->state == SILC_PROTOCOL_STATE_FAILURE)) {
1360 /* Error occured during protocol */
1361 silc_protocol_free(protocol);
1362 sock->protocol = NULL;
1363 silc_ske_free_key_material(ctx->keymat);
1365 silc_packet_context_free(ctx->packet);
1367 silc_ske_free(ctx->ske);
1368 silc_free(ctx->dest_id);
1369 silc_server_config_unref(&ctx->cconfig);
1370 silc_server_config_unref(&ctx->sconfig);
1371 silc_server_config_unref(&ctx->rconfig);
1373 silc_schedule_task_del_by_callback(server->schedule,
1374 silc_server_failure_callback);
1375 silc_server_disconnect_remote(server, sock,
1376 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED,
1378 server->stat.auth_failures++;
1382 /* We now have the key material as the result of the key exchange
1383 protocol. Take the key material into use. Free the raw key material
1384 as soon as we've set them into use. */
1385 if (!silc_server_protocol_ke_set_keys(server, ctx->ske,
1386 ctx->sock, ctx->keymat,
1387 ctx->ske->prop->cipher,
1388 ctx->ske->prop->pkcs,
1389 ctx->ske->prop->hash,
1390 ctx->ske->prop->hmac,
1391 ctx->ske->prop->group,
1393 silc_protocol_free(protocol);
1394 sock->protocol = NULL;
1395 silc_ske_free_key_material(ctx->keymat);
1397 silc_packet_context_free(ctx->packet);
1399 silc_ske_free(ctx->ske);
1400 silc_free(ctx->dest_id);
1401 silc_server_config_unref(&ctx->cconfig);
1402 silc_server_config_unref(&ctx->sconfig);
1403 silc_server_config_unref(&ctx->rconfig);
1405 silc_schedule_task_del_by_callback(server->schedule,
1406 silc_server_failure_callback);
1407 silc_server_disconnect_remote(server, sock,
1408 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
1409 server->stat.auth_failures++;
1412 silc_ske_free_key_material(ctx->keymat);
1414 /* Allocate internal context for the authentication protocol. This
1415 is sent as context for the protocol. */
1416 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1417 proto_ctx->server = (void *)server;
1418 proto_ctx->sock = sock;
1419 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
1420 proto_ctx->responder = TRUE;
1421 proto_ctx->dest_id_type = ctx->dest_id_type;
1422 proto_ctx->dest_id = ctx->dest_id;
1423 proto_ctx->cconfig = ctx->cconfig;
1424 proto_ctx->sconfig = ctx->sconfig;
1425 proto_ctx->rconfig = ctx->rconfig;
1427 /* Free old protocol as it is finished now */
1428 silc_protocol_free(protocol);
1430 silc_packet_context_free(ctx->packet);
1432 sock->protocol = NULL;
1434 /* Allocate the authentication protocol. This is allocated here
1435 but we won't start it yet. We will be receiving party of this
1436 protocol thus we will wait that connecting party will make
1437 their first move. */
1438 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1439 &sock->protocol, proto_ctx,
1440 silc_server_accept_new_connection_final);
1442 /* Register timeout task. If the protocol is not executed inside
1443 this timelimit the connection will be terminated. */
1444 proto_ctx->timeout_task =
1445 silc_schedule_task_add(server->schedule, sock->sock,
1446 silc_server_timeout_remote,
1448 server->config->conn_auth_timeout, 0,
1453 /* Final part of accepting new connection. The connection has now
1454 been authenticated and keys has been exchanged. We also know whether
1455 this is client or server connection. */
1457 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
1459 SilcProtocol protocol = (SilcProtocol)context;
1460 SilcServerConnAuthInternalContext *ctx =
1461 (SilcServerConnAuthInternalContext *)protocol->context;
1462 SilcServer server = (SilcServer)ctx->server;
1463 SilcSocketConnection sock = ctx->sock;
1464 SilcServerHBContext hb_context;
1465 SilcUnknownEntry entry = (SilcUnknownEntry)sock->user_data;
1467 SilcUInt32 hearbeat_timeout = server->config->param.keepalive_secs;
1469 SILC_LOG_DEBUG(("Start"));
1471 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1472 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1473 /* Error occured during protocol */
1474 silc_protocol_free(protocol);
1475 sock->protocol = NULL;
1477 silc_packet_context_free(ctx->packet);
1479 silc_ske_free(ctx->ske);
1480 silc_free(ctx->dest_id);
1481 silc_server_config_unref(&ctx->cconfig);
1482 silc_server_config_unref(&ctx->sconfig);
1483 silc_server_config_unref(&ctx->rconfig);
1485 silc_schedule_task_del_by_callback(server->schedule,
1486 silc_server_failure_callback);
1487 silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1489 server->stat.auth_failures++;
1493 entry->data.last_receive = time(NULL);
1495 switch (ctx->conn_type) {
1496 case SILC_SOCKET_TYPE_CLIENT:
1498 SilcClientEntry client;
1499 SilcServerConfigClient *conn = ctx->cconfig.ref_ptr;
1501 /* Verify whether this connection is after all allowed to connect */
1502 if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1503 &server->config->param,
1504 conn->param, ctx->ske)) {
1505 server->stat.auth_failures++;
1509 SILC_LOG_DEBUG(("Remote host is client"));
1510 SILC_LOG_INFO(("Connection %s (%s) is client", sock->hostname,
1513 /* Add the client to the client ID cache. The nickname and Client ID
1514 and other information is created after we have received NEW_CLIENT
1515 packet from client. */
1516 client = silc_idlist_add_client(server->local_list,
1517 NULL, NULL, NULL, NULL, NULL, sock, 0);
1519 SILC_LOG_ERROR(("Could not add new client to cache"));
1520 silc_free(sock->user_data);
1521 silc_server_disconnect_remote(server, sock,
1522 SILC_STATUS_ERR_AUTH_FAILED, NULL);
1523 server->stat.auth_failures++;
1528 server->stat.my_clients++;
1529 server->stat.clients++;
1530 server->stat.cell_clients++;
1532 /* Get connection parameters */
1534 if (conn->param->keepalive_secs)
1535 hearbeat_timeout = conn->param->keepalive_secs;
1538 id_entry = (void *)client;
1541 case SILC_SOCKET_TYPE_SERVER:
1542 case SILC_SOCKET_TYPE_ROUTER:
1544 SilcServerEntry new_server;
1545 bool initiator = FALSE;
1546 bool backup_local = FALSE;
1547 bool backup_router = FALSE;
1548 char *backup_replace_ip = NULL;
1549 SilcUInt16 backup_replace_port = 0;
1550 SilcServerConfigServer *sconn = ctx->sconfig.ref_ptr;
1551 SilcServerConfigRouter *rconn = ctx->rconfig.ref_ptr;
1553 if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1554 /* Verify whether this connection is after all allowed to connect */
1555 if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1556 &server->config->param,
1557 rconn ? rconn->param : NULL,
1559 server->stat.auth_failures++;
1565 if (rconn->param->keepalive_secs)
1566 hearbeat_timeout = rconn->param->keepalive_secs;
1569 initiator = rconn->initiator;
1570 backup_local = rconn->backup_local;
1571 backup_router = rconn->backup_router;
1572 backup_replace_ip = rconn->backup_replace_ip;
1573 backup_replace_port = rconn->backup_replace_port;
1577 if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER) {
1578 /* Verify whether this connection is after all allowed to connect */
1579 if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1580 &server->config->param,
1581 sconn ? sconn->param : NULL,
1583 server->stat.auth_failures++;
1588 if (sconn->param->keepalive_secs)
1589 hearbeat_timeout = sconn->param->keepalive_secs;
1592 backup_router = sconn->backup_router;
1596 SILC_LOG_DEBUG(("Remote host is %s",
1597 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1598 "server" : (backup_router ?
1599 "backup router" : "router")));
1600 SILC_LOG_INFO(("Connection %s (%s) is %s", sock->hostname,
1601 sock->ip, ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1602 "server" : (backup_router ?
1603 "backup router" : "router")));
1605 /* Add the server into server cache. The server name and Server ID
1606 is updated after we have received NEW_SERVER packet from the
1607 server. We mark ourselves as router for this server if we really
1610 silc_idlist_add_server((ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1611 server->local_list : (backup_router ?
1612 server->local_list :
1613 server->global_list)),
1615 (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1616 SILC_SERVER : SILC_ROUTER),
1618 (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1619 server->id_entry : (backup_router ?
1620 server->id_entry : NULL)),
1623 SILC_LOG_ERROR(("Could not add new server to cache"));
1624 silc_free(sock->user_data);
1625 silc_server_disconnect_remote(server, sock,
1626 SILC_STATUS_ERR_AUTH_FAILED, NULL);
1627 server->stat.auth_failures++;
1632 if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER) {
1633 server->stat.my_servers++;
1635 server->stat.my_routers++;
1636 server->stat.routers++;
1638 server->stat.servers++;
1640 id_entry = (void *)new_server;
1642 /* If the incoming connection is router and marked as backup router
1643 then add it to be one of our backups */
1644 if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER && backup_router) {
1645 silc_server_backup_add(server, new_server, backup_replace_ip,
1646 backup_replace_port, backup_local);
1648 /* Change it back to SERVER type since that's what it really is. */
1650 ctx->conn_type = SILC_SOCKET_TYPE_SERVER;
1652 new_server->server_type = SILC_BACKUP_ROUTER;
1655 /* Check whether this connection is to be our primary router connection
1656 if we do not already have the primary route. */
1657 if (!backup_router &&
1658 server->standalone && ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1659 if (silc_server_config_is_primary_route(server) && !initiator)
1662 SILC_LOG_DEBUG(("We are not standalone server anymore"));
1663 server->standalone = FALSE;
1664 if (!server->id_entry->router) {
1665 server->id_entry->router = id_entry;
1666 server->router = id_entry;
1677 sock->type = ctx->conn_type;
1679 /* Add the common data structure to the ID entry. */
1680 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1682 /* Add to sockets internal pointer for fast referencing */
1683 silc_free(sock->user_data);
1684 sock->user_data = id_entry;
1686 /* Connection has been fully established now. Everything is ok. */
1687 SILC_LOG_DEBUG(("New connection authenticated"));
1689 /* Perform keepalive. The `hb_context' will be freed automatically
1690 when finally calling the silc_socket_free function. */
1691 hb_context = silc_calloc(1, sizeof(*hb_context));
1692 hb_context->server = server;
1693 silc_socket_set_heartbeat(sock, hearbeat_timeout, hb_context,
1694 silc_server_perform_heartbeat,
1698 silc_schedule_task_del_by_callback(server->schedule,
1699 silc_server_failure_callback);
1700 silc_protocol_free(protocol);
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 sock->protocol = NULL;
1713 /* This function is used to read packets from network and send packets to
1714 network. This is usually a generic task. */
1716 SILC_TASK_CALLBACK(silc_server_packet_process)
1718 SilcServer server = (SilcServer)context;
1719 SilcSocketConnection sock = server->sockets[fd];
1720 SilcIDListData idata;
1721 SilcCipher cipher = NULL;
1722 SilcHmac hmac = NULL;
1723 SilcUInt32 sequence = 0;
1729 SILC_LOG_DEBUG(("Processing packet"));
1731 /* Packet sending */
1733 if (type == SILC_TASK_WRITE) {
1734 /* Do not send data to disconnected connection */
1735 if (SILC_IS_DISCONNECTED(sock))
1738 server->stat.packets_sent++;
1740 /* Send the packet */
1741 ret = silc_packet_send(sock, TRUE);
1743 /* If returned -2 could not write to connection now, will do
1749 SILC_LOG_ERROR(("Error sending packet to connection "
1750 "%s:%d [%s]", sock->hostname, sock->port,
1751 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1752 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1753 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1758 /* The packet has been sent and now it is time to set the connection
1759 back to only for input. When there is again some outgoing data
1760 available for this connection it will be set for output as well.
1761 This call clears the output setting and sets it only for input. */
1762 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, fd);
1763 SILC_UNSET_OUTBUF_PENDING(sock);
1765 silc_buffer_clear(sock->outbuf);
1769 /* Packet receiving */
1771 /* Read some data from connection */
1772 ret = silc_packet_receive(sock);
1776 SILC_LOG_ERROR(("Error receiving packet from connection "
1777 "%s:%d [%s] %s", sock->hostname, sock->port,
1778 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1779 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1780 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1781 "Router"), strerror(errno)));
1787 SILC_LOG_DEBUG(("Read EOF"));
1789 /* If connection is disconnecting already we will finally
1790 close the connection */
1791 if (SILC_IS_DISCONNECTING(sock)) {
1792 if (sock->user_data)
1793 silc_server_free_sock_user_data(server, sock, NULL);
1794 silc_server_close_connection(server, sock);
1798 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1799 SILC_SET_DISCONNECTING(sock);
1801 if (sock->user_data) {
1803 if (silc_socket_get_error(sock, tmp, sizeof(tmp) - 1))
1804 silc_server_free_sock_user_data(server, sock, tmp);
1806 silc_server_free_sock_user_data(server, sock, NULL);
1807 } else if (server->router_conn && server->router_conn->sock == sock &&
1808 !server->router && server->standalone)
1809 silc_schedule_task_add(server->schedule, 0,
1810 silc_server_connect_to_router,
1813 SILC_TASK_PRI_NORMAL);
1815 silc_server_close_connection(server, sock);
1819 /* If connection is disconnecting or disconnected we will ignore
1821 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1822 SILC_LOG_DEBUG(("Ignoring read data from disconnected connection"));
1826 server->stat.packets_received++;
1828 /* Get keys and stuff from ID entry */
1829 idata = (SilcIDListData)sock->user_data;
1831 cipher = idata->receive_key;
1832 hmac = idata->hmac_receive;
1833 sequence = idata->psn_receive;
1836 /* Process the packet. This will call the parser that will then
1837 decrypt and parse the packet. */
1838 ret = silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1839 TRUE : FALSE, cipher, hmac, sequence,
1840 silc_server_packet_parse, server);
1842 /* If this socket connection is not authenticated yet and the packet
1843 processing failed we will drop the connection since it can be
1844 a malicious flooder. */
1845 if (sock->type == SILC_SOCKET_TYPE_UNKNOWN && ret == FALSE &&
1846 (!sock->protocol || sock->protocol->protocol->type ==
1847 SILC_PROTOCOL_SERVER_KEY_EXCHANGE)) {
1848 SILC_LOG_DEBUG(("Bad data sent from unknown connection %d", sock->sock));
1849 SILC_SET_DISCONNECTING(sock);
1851 if (sock->user_data)
1852 silc_server_free_sock_user_data(server, sock, NULL);
1853 silc_server_close_connection(server, sock);
1857 /* Parses whole packet, received earlier. */
1859 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1861 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1862 SilcServer server = (SilcServer)parse_ctx->context;
1863 SilcSocketConnection sock = parse_ctx->sock;
1864 SilcPacketContext *packet = parse_ctx->packet;
1865 SilcIDListData idata = (SilcIDListData)sock->user_data;
1868 SILC_LOG_DEBUG(("Start"));
1870 /* Parse the packet */
1871 if (parse_ctx->normal)
1872 ret = silc_packet_parse(packet, idata ? idata->receive_key : NULL);
1874 ret = silc_packet_parse_special(packet, idata ? idata->receive_key : NULL);
1876 /* If entry is disabled ignore what we got. */
1877 if (ret != SILC_PACKET_RESUME_ROUTER &&
1878 idata && idata->status & SILC_IDLIST_STATUS_DISABLED) {
1879 SILC_LOG_DEBUG(("Connection is disabled"));
1883 if (ret == SILC_PACKET_NONE)
1886 /* Check that the the current client ID is same as in the client's packet. */
1887 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
1888 SilcClientEntry client = (SilcClientEntry)sock->user_data;
1889 if (client && client->id) {
1890 void *id = silc_id_str2id(packet->src_id, packet->src_id_len,
1891 packet->src_id_type);
1892 if (!id || !SILC_ID_CLIENT_COMPARE(client->id, id)) {
1900 if (server->server_type == SILC_ROUTER) {
1901 /* Route the packet if it is not destined to us. Other ID types but
1902 server are handled separately after processing them. */
1903 if (!(packet->flags & SILC_PACKET_FLAG_BROADCAST) &&
1904 packet->dst_id_type == SILC_ID_SERVER &&
1905 sock->type != SILC_SOCKET_TYPE_CLIENT &&
1906 memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1908 /* Route the packet to fastest route for the destination ID */
1909 void *id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1910 packet->dst_id_type);
1913 silc_server_packet_route(server,
1914 silc_server_route_get(server, id,
1915 packet->dst_id_type),
1922 /* Parse the incoming packet type */
1923 silc_server_packet_parse_type(server, sock, packet);
1925 if (server->server_type == SILC_ROUTER) {
1926 /* Broadcast packet if it is marked as broadcast packet and it is
1927 originated from router and we are router. */
1928 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1929 packet->flags & SILC_PACKET_FLAG_BROADCAST &&
1930 !server->standalone) {
1931 /* Broadcast to our primary route */
1932 silc_server_packet_broadcast(server, server->router->connection, packet);
1934 /* If we have backup routers then we need to feed all broadcast
1935 data to those servers. */
1936 silc_server_backup_broadcast(server, sock, packet);
1941 silc_packet_context_free(packet);
1942 silc_free(parse_ctx);
1945 /* Parser callback called by silc_packet_receive_process. This merely
1946 registers timeout that will handle the actual parsing when appropriate. */
1948 bool silc_server_packet_parse(SilcPacketParserContext *parser_context,
1951 SilcServer server = (SilcServer)context;
1952 SilcSocketConnection sock = parser_context->sock;
1953 SilcIDListData idata = (SilcIDListData)sock->user_data;
1956 idata->psn_receive = parser_context->packet->sequence + 1;
1958 /* If protocol for this connection is key exchange or rekey then we'll
1959 process all packets synchronously, since there might be packets in
1960 queue that we are not able to decrypt without first processing the
1961 packets before them. */
1962 if ((parser_context->packet->type == SILC_PACKET_REKEY ||
1963 parser_context->packet->type == SILC_PACKET_REKEY_DONE) ||
1964 (sock->protocol && sock->protocol->protocol &&
1965 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1966 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY))) {
1967 silc_server_packet_parse_real(server->schedule, 0, sock->sock,
1970 /* Reprocess data since we'll return FALSE here. This is because
1971 the idata->receive_key might have become valid in the last packet
1972 and we want to call this processor with valid cipher. */
1974 silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1975 TRUE : FALSE, idata->receive_key,
1976 idata->hmac_receive, idata->psn_receive,
1977 silc_server_packet_parse, server);
1979 silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1980 TRUE : FALSE, NULL, NULL, 0,
1981 silc_server_packet_parse, server);
1985 switch (sock->type) {
1986 case SILC_SOCKET_TYPE_UNKNOWN:
1987 case SILC_SOCKET_TYPE_CLIENT:
1988 /* Parse the packet with timeout */
1989 silc_schedule_task_add(server->schedule, sock->sock,
1990 silc_server_packet_parse_real,
1991 (void *)parser_context, 0, 100000,
1993 SILC_TASK_PRI_NORMAL);
1995 case SILC_SOCKET_TYPE_SERVER:
1996 case SILC_SOCKET_TYPE_ROUTER:
1997 /* Packets from servers are parsed immediately */
1998 silc_server_packet_parse_real(server->schedule, 0, sock->sock,
2008 /* Parses the packet type and calls what ever routines the packet type
2009 requires. This is done for all incoming packets. */
2011 void silc_server_packet_parse_type(SilcServer server,
2012 SilcSocketConnection sock,
2013 SilcPacketContext *packet)
2015 SilcPacketType type = packet->type;
2016 SilcIDListData idata = (SilcIDListData)sock->user_data;
2018 SILC_LOG_DEBUG(("Parsing packet type %d", type));
2020 /* Parse the packet type */
2022 case SILC_PACKET_DISCONNECT:
2025 char *message = NULL;
2027 SILC_LOG_DEBUG(("Disconnect packet"));
2029 if (packet->flags & SILC_PACKET_FLAG_LIST)
2031 if (packet->buffer->len < 1)
2034 status = (SilcStatus)packet->buffer->data[0];
2035 if (packet->buffer->len > 1 &&
2036 silc_utf8_valid(packet->buffer->data + 1, packet->buffer->len - 1))
2037 message = silc_memdup(packet->buffer->data, packet->buffer->len);
2039 SILC_LOG_ERROR(("Disconnected by %s (%s): %s (%d) %s",
2040 sock->ip, sock->hostname,
2041 silc_get_status_message(status), status,
2042 message ? message : ""));
2047 case SILC_PACKET_SUCCESS:
2049 * Success received for something. For now we can have only
2050 * one protocol for connection executing at once hence this
2051 * success message is for whatever protocol is executing currently.
2053 SILC_LOG_DEBUG(("Success packet"));
2054 if (packet->flags & SILC_PACKET_FLAG_LIST)
2057 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2060 case SILC_PACKET_FAILURE:
2062 * Failure received for something. For now we can have only
2063 * one protocol for connection executing at once hence this
2064 * failure message is for whatever protocol is executing currently.
2066 SILC_LOG_DEBUG(("Failure packet"));
2067 if (packet->flags & SILC_PACKET_FLAG_LIST)
2069 if (sock->protocol) {
2070 SilcServerFailureContext f;
2071 f = silc_calloc(1, sizeof(*f));
2075 /* We will wait 5 seconds to process this failure packet */
2076 silc_schedule_task_add(server->schedule, sock->sock,
2077 silc_server_failure_callback, (void *)f, 5, 0,
2078 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
2082 case SILC_PACKET_REJECT:
2083 SILC_LOG_DEBUG(("Reject packet"));
2084 if (packet->flags & SILC_PACKET_FLAG_LIST)
2089 case SILC_PACKET_NOTIFY:
2091 * Received notify packet. Server can receive notify packets from
2092 * router. Server then relays the notify messages to clients if needed.
2094 SILC_LOG_DEBUG(("Notify packet"));
2095 if (packet->flags & SILC_PACKET_FLAG_LIST)
2096 silc_server_notify_list(server, sock, packet);
2098 silc_server_notify(server, sock, packet);
2104 case SILC_PACKET_CHANNEL_MESSAGE:
2106 * Received channel message. Channel messages are special packets
2107 * (although probably most common ones) thus they are handled
2110 SILC_LOG_DEBUG(("Channel Message packet"));
2111 if (packet->flags & SILC_PACKET_FLAG_LIST)
2113 idata->last_receive = time(NULL);
2114 silc_server_channel_message(server, sock, packet);
2117 case SILC_PACKET_CHANNEL_KEY:
2119 * Received key for channel. As channels are created by the router
2120 * the keys are as well. We will distribute the key to all of our
2121 * locally connected clients on the particular channel. Router
2122 * never receives this channel and thus is ignored.
2124 SILC_LOG_DEBUG(("Channel Key packet"));
2125 if (packet->flags & SILC_PACKET_FLAG_LIST)
2127 silc_server_channel_key(server, sock, packet);
2133 case SILC_PACKET_COMMAND:
2135 * Recived command. Processes the command request and allocates the
2136 * command context and calls the command.
2138 SILC_LOG_DEBUG(("Command packet"));
2139 if (packet->flags & SILC_PACKET_FLAG_LIST)
2141 silc_server_command_process(server, sock, packet);
2144 case SILC_PACKET_COMMAND_REPLY:
2146 * Received command reply packet. Received command reply to command. It
2147 * may be reply to command sent by us or reply to command sent by client
2148 * that we've routed further.
2150 SILC_LOG_DEBUG(("Command Reply packet"));
2151 if (packet->flags & SILC_PACKET_FLAG_LIST)
2153 silc_server_command_reply(server, sock, packet);
2157 * Private Message packets
2159 case SILC_PACKET_PRIVATE_MESSAGE:
2161 * Received private message packet. The packet is coming from either
2164 SILC_LOG_DEBUG(("Private Message packet"));
2165 if (packet->flags & SILC_PACKET_FLAG_LIST)
2167 idata->last_receive = time(NULL);
2168 silc_server_private_message(server, sock, packet);
2171 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
2173 * Private message key packet.
2175 if (packet->flags & SILC_PACKET_FLAG_LIST)
2177 silc_server_private_message_key(server, sock, packet);
2181 * Key Exchange protocol packets
2183 case SILC_PACKET_KEY_EXCHANGE:
2184 SILC_LOG_DEBUG(("KE packet"));
2185 if (packet->flags & SILC_PACKET_FLAG_LIST)
2188 if (sock->protocol && sock->protocol->protocol &&
2189 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
2190 SilcServerKEInternalContext *proto_ctx =
2191 (SilcServerKEInternalContext *)sock->protocol->context;
2193 proto_ctx->packet = silc_packet_context_dup(packet);
2195 /* Let the protocol handle the packet */
2196 silc_protocol_execute(sock->protocol, server->schedule, 0, 100000);
2198 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
2199 "protocol active, packet dropped."));
2203 case SILC_PACKET_KEY_EXCHANGE_1:
2204 SILC_LOG_DEBUG(("KE 1 packet"));
2205 if (packet->flags & SILC_PACKET_FLAG_LIST)
2208 if (sock->protocol && sock->protocol->protocol &&
2209 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
2210 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
2212 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2213 SilcServerRekeyInternalContext *proto_ctx =
2214 (SilcServerRekeyInternalContext *)sock->protocol->context;
2216 if (proto_ctx->packet)
2217 silc_packet_context_free(proto_ctx->packet);
2219 proto_ctx->packet = silc_packet_context_dup(packet);
2221 /* Let the protocol handle the packet */
2222 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2224 SilcServerKEInternalContext *proto_ctx =
2225 (SilcServerKEInternalContext *)sock->protocol->context;
2227 if (proto_ctx->packet)
2228 silc_packet_context_free(proto_ctx->packet);
2230 proto_ctx->packet = silc_packet_context_dup(packet);
2231 proto_ctx->dest_id_type = packet->src_id_type;
2232 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2233 packet->src_id_type);
2234 if (!proto_ctx->dest_id)
2237 /* Let the protocol handle the packet */
2238 silc_protocol_execute(sock->protocol, server->schedule,
2242 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
2243 "protocol active, packet dropped."));
2247 case SILC_PACKET_KEY_EXCHANGE_2:
2248 SILC_LOG_DEBUG(("KE 2 packet"));
2249 if (packet->flags & SILC_PACKET_FLAG_LIST)
2252 if (sock->protocol && sock->protocol->protocol &&
2253 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
2254 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
2256 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2257 SilcServerRekeyInternalContext *proto_ctx =
2258 (SilcServerRekeyInternalContext *)sock->protocol->context;
2260 if (proto_ctx->packet)
2261 silc_packet_context_free(proto_ctx->packet);
2263 proto_ctx->packet = silc_packet_context_dup(packet);
2265 /* Let the protocol handle the packet */
2266 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2268 SilcServerKEInternalContext *proto_ctx =
2269 (SilcServerKEInternalContext *)sock->protocol->context;
2271 if (proto_ctx->packet)
2272 silc_packet_context_free(proto_ctx->packet);
2274 proto_ctx->packet = silc_packet_context_dup(packet);
2275 proto_ctx->dest_id_type = packet->src_id_type;
2276 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2277 packet->src_id_type);
2278 if (!proto_ctx->dest_id)
2281 /* Let the protocol handle the packet */
2282 silc_protocol_execute(sock->protocol, server->schedule,
2286 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
2287 "protocol active, packet dropped."));
2291 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
2293 * Connection authentication request packet. When we receive this packet
2294 * we will send to the other end information about our mandatory
2295 * authentication method for the connection. This packet maybe received
2298 SILC_LOG_DEBUG(("Connection authentication request packet"));
2299 if (packet->flags & SILC_PACKET_FLAG_LIST)
2301 silc_server_connection_auth_request(server, sock, packet);
2305 * Connection Authentication protocol packets
2307 case SILC_PACKET_CONNECTION_AUTH:
2308 /* Start of the authentication protocol. We receive here the
2309 authentication data and will verify it. */
2310 SILC_LOG_DEBUG(("Connection auth packet"));
2311 if (packet->flags & SILC_PACKET_FLAG_LIST)
2314 if (sock->protocol && sock->protocol->protocol->type
2315 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
2317 SilcServerConnAuthInternalContext *proto_ctx =
2318 (SilcServerConnAuthInternalContext *)sock->protocol->context;
2320 proto_ctx->packet = silc_packet_context_dup(packet);
2322 /* Let the protocol handle the packet */
2323 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2325 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
2326 "protocol active, packet dropped."));
2330 case SILC_PACKET_NEW_ID:
2332 * Received New ID packet. This includes some new ID that has been
2333 * created. It may be for client, server or channel. This is the way
2334 * to distribute information about new registered entities in the
2337 SILC_LOG_DEBUG(("New ID packet"));
2338 if (packet->flags & SILC_PACKET_FLAG_LIST)
2339 silc_server_new_id_list(server, sock, packet);
2341 silc_server_new_id(server, sock, packet);
2344 case SILC_PACKET_NEW_CLIENT:
2346 * Received new client packet. This includes client information that
2347 * we will use to create initial client ID. After creating new
2348 * ID we will send it to the client.
2350 SILC_LOG_DEBUG(("New Client packet"));
2351 if (packet->flags & SILC_PACKET_FLAG_LIST)
2353 silc_server_new_client(server, sock, packet);
2356 case SILC_PACKET_NEW_SERVER:
2358 * Received new server packet. This includes Server ID and some other
2359 * information that we may save. This is received after server has
2362 SILC_LOG_DEBUG(("New Server packet"));
2363 if (packet->flags & SILC_PACKET_FLAG_LIST)
2365 silc_server_new_server(server, sock, packet);
2368 case SILC_PACKET_NEW_CHANNEL:
2370 * Received new channel packet. Information about new channel in the
2371 * network are distributed using this packet.
2373 SILC_LOG_DEBUG(("New Channel packet"));
2374 if (packet->flags & SILC_PACKET_FLAG_LIST)
2375 silc_server_new_channel_list(server, sock, packet);
2377 silc_server_new_channel(server, sock, packet);
2380 case SILC_PACKET_HEARTBEAT:
2382 * Received heartbeat.
2384 SILC_LOG_DEBUG(("Heartbeat packet"));
2385 if (packet->flags & SILC_PACKET_FLAG_LIST)
2389 case SILC_PACKET_KEY_AGREEMENT:
2391 * Received heartbeat.
2393 SILC_LOG_DEBUG(("Key agreement packet"));
2394 if (packet->flags & SILC_PACKET_FLAG_LIST)
2396 silc_server_key_agreement(server, sock, packet);
2399 case SILC_PACKET_REKEY:
2401 * Received re-key packet. The sender wants to regenerate the session
2404 SILC_LOG_DEBUG(("Re-key packet"));
2405 if (packet->flags & SILC_PACKET_FLAG_LIST)
2407 silc_server_rekey(server, sock, packet);
2410 case SILC_PACKET_REKEY_DONE:
2412 * The re-key is done.
2414 SILC_LOG_DEBUG(("Re-key done packet"));
2415 if (packet->flags & SILC_PACKET_FLAG_LIST)
2418 if (sock->protocol && sock->protocol->protocol &&
2419 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2421 SilcServerRekeyInternalContext *proto_ctx =
2422 (SilcServerRekeyInternalContext *)sock->protocol->context;
2424 if (proto_ctx->packet)
2425 silc_packet_context_free(proto_ctx->packet);
2427 proto_ctx->packet = silc_packet_context_dup(packet);
2429 /* Let the protocol handle the packet */
2430 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2432 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
2433 "protocol active, packet dropped."));
2437 case SILC_PACKET_FTP:
2439 SILC_LOG_DEBUG(("FTP packet"));
2440 if (packet->flags & SILC_PACKET_FLAG_LIST)
2442 silc_server_ftp(server, sock, packet);
2445 case SILC_PACKET_RESUME_CLIENT:
2447 SILC_LOG_DEBUG(("Resume Client packet"));
2448 if (packet->flags & SILC_PACKET_FLAG_LIST)
2450 silc_server_resume_client(server, sock, packet);
2453 case SILC_PACKET_RESUME_ROUTER:
2454 /* Resume router packet received. This packet is received for backup
2455 router resuming protocol. */
2456 SILC_LOG_DEBUG(("Resume router packet"));
2457 if (packet->flags & SILC_PACKET_FLAG_LIST)
2459 silc_server_backup_resume_router(server, sock, packet);
2463 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
2469 /* Creates connection to a remote router. */
2471 void silc_server_create_connection(SilcServer server,
2472 const char *remote_host, SilcUInt32 port)
2474 SilcServerConnection sconn;
2476 /* Allocate connection object for hold connection specific stuff. */
2477 sconn = silc_calloc(1, sizeof(*sconn));
2478 sconn->server = server;
2479 sconn->remote_host = strdup(remote_host);
2480 sconn->remote_port = port;
2481 sconn->no_reconnect = TRUE;
2483 silc_schedule_task_add(server->schedule, 0,
2484 silc_server_connect_router,
2485 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
2486 SILC_TASK_PRI_NORMAL);
2489 SILC_TASK_CALLBACK(silc_server_close_connection_final)
2491 silc_socket_free((SilcSocketConnection)context);
2494 /* Closes connection to socket connection */
2496 void silc_server_close_connection(SilcServer server,
2497 SilcSocketConnection sock)
2499 if (!server->sockets[sock->sock])
2502 SILC_LOG_INFO(("Closing connection %s:%d [%s]", sock->hostname,
2504 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2505 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2506 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2509 /* We won't listen for this connection anymore */
2510 silc_schedule_unset_listen_fd(server->schedule, sock->sock);
2512 /* Unregister all tasks */
2513 silc_schedule_task_del_by_fd(server->schedule, sock->sock);
2515 /* Close the actual connection */
2516 silc_net_close_connection(sock->sock);
2517 server->sockets[sock->sock] = NULL;
2519 /* If sock->user_data is NULL then we'll check for active protocols
2520 here since the silc_server_free_sock_user_data has not been called
2521 for this connection. */
2522 if (!sock->user_data) {
2523 /* If any protocol is active cancel its execution. It will call
2524 the final callback which will finalize the disconnection. */
2525 if (sock->protocol) {
2526 silc_protocol_cancel(sock->protocol, server->schedule);
2527 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2528 silc_protocol_execute_final(sock->protocol, server->schedule);
2529 sock->protocol = NULL;
2534 silc_schedule_task_add(server->schedule, 0,
2535 silc_server_close_connection_final,
2536 (void *)sock, 0, 1, SILC_TASK_TIMEOUT,
2537 SILC_TASK_PRI_NORMAL);
2540 /* Sends disconnect message to remote connection and disconnects the
2543 void silc_server_disconnect_remote(SilcServer server,
2544 SilcSocketConnection sock,
2545 SilcStatus status, ...)
2548 unsigned char buf[512];
2556 memset(buf, 0, sizeof(buf));
2557 va_start(ap, status);
2558 cp = va_arg(ap, char *);
2560 vsnprintf(buf, sizeof(buf) - 1, cp, ap);
2565 SILC_LOG_DEBUG(("Disconnecting remote host"));
2567 /* Notify remote end that the conversation is over. The notify message
2568 is tried to be sent immediately. */
2572 len += silc_utf8_encoded_len(buf, strlen(buf), SILC_STRING_ASCII);
2574 buffer = silc_buffer_alloc_size(len);
2578 buffer->data[0] = status;
2580 silc_utf8_encode(buf, strlen(buf), SILC_STRING_ASCII, buffer->data + 1,
2582 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2583 buffer->data, buffer->len, TRUE);
2584 silc_buffer_free(buffer);
2587 silc_server_packet_queue_purge(server, sock);
2589 /* Mark the connection to be disconnected */
2590 SILC_SET_DISCONNECTED(sock);
2591 silc_server_close_connection(server, sock);
2596 SilcClientEntry client;
2597 } *FreeClientInternal;
2599 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout)
2601 FreeClientInternal i = (FreeClientInternal)context;
2603 silc_idlist_del_data(i->client);
2604 silc_idcache_purge_by_context(i->server->local_list->clients, i->client);
2608 /* Frees client data and notifies about client's signoff. */
2610 void silc_server_free_client_data(SilcServer server,
2611 SilcSocketConnection sock,
2612 SilcClientEntry client,
2614 const char *signoff)
2616 FreeClientInternal i = silc_calloc(1, sizeof(*i));
2618 /* If there is pending outgoing data for the client then purge it
2619 to the network before removing the client entry. */
2620 silc_server_packet_queue_purge(server, sock);
2623 /* Check if anyone is watching this nickname */
2624 if (server->server_type == SILC_ROUTER)
2625 silc_server_check_watcher_list(server, client, NULL,
2626 SILC_NOTIFY_TYPE_SIGNOFF);
2628 /* Send SIGNOFF notify to routers. */
2629 if (notify && !server->standalone && server->router)
2630 silc_server_send_notify_signoff(server, server->router->connection,
2631 server->server_type == SILC_SERVER ?
2632 FALSE : TRUE, client->id, signoff);
2634 /* Remove client from all channels */
2636 silc_server_remove_from_channels(server, NULL, client,
2637 TRUE, (char *)signoff, TRUE);
2639 silc_server_remove_from_channels(server, NULL, client,
2640 FALSE, NULL, FALSE);
2642 /* Remove this client from watcher list if it is */
2643 silc_server_del_from_watcher_list(server, client);
2646 /* Update statistics */
2647 server->stat.my_clients--;
2648 server->stat.clients--;
2649 if (server->stat.cell_clients)
2650 server->stat.cell_clients--;
2651 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
2652 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
2653 silc_schedule_task_del_by_context(server->schedule, client);
2655 /* We will not delete the client entry right away. We will take it
2656 into history (for WHOWAS command) for 5 minutes */
2659 silc_schedule_task_add(server->schedule, 0,
2660 silc_server_free_client_data_timeout,
2662 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2663 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
2665 client->router = NULL;
2666 client->connection = NULL;
2669 /* Frees user_data pointer from socket connection object. This also sends
2670 appropriate notify packets to the network to inform about leaving
2673 void silc_server_free_sock_user_data(SilcServer server,
2674 SilcSocketConnection sock,
2675 const char *signoff_message)
2677 SILC_LOG_DEBUG(("Start"));
2679 switch (sock->type) {
2680 case SILC_SOCKET_TYPE_CLIENT:
2682 SilcClientEntry user_data = (SilcClientEntry)sock->user_data;
2683 silc_server_free_client_data(server, sock, user_data, TRUE,
2687 case SILC_SOCKET_TYPE_SERVER:
2688 case SILC_SOCKET_TYPE_ROUTER:
2690 SilcServerEntry user_data = (SilcServerEntry)sock->user_data;
2691 SilcServerEntry backup_router = NULL;
2694 backup_router = silc_server_backup_get(server, user_data->id);
2696 /* If this was our primary router connection then we're lost to
2697 the outside world. */
2698 if (server->router == user_data) {
2699 /* Check whether we have a backup router connection */
2700 if (!backup_router || backup_router == user_data) {
2701 silc_schedule_task_add(server->schedule, 0,
2702 silc_server_connect_to_router,
2705 SILC_TASK_PRI_NORMAL);
2707 server->id_entry->router = NULL;
2708 server->router = NULL;
2709 server->standalone = TRUE;
2710 backup_router = NULL;
2712 SILC_LOG_INFO(("New primary router is backup router %s",
2713 backup_router->server_name));
2714 SILC_LOG_DEBUG(("New primary router is backup router %s",
2715 backup_router->server_name));
2716 #ifdef BACKUP_SINGLE_ROUTER
2717 if (server->id_entry != backup_router) {
2718 #endif /* BACKUP_SINGLE_ROUTER */
2719 server->id_entry->router = backup_router;
2720 server->router = backup_router;
2721 server->router_connect = time(0);
2722 server->backup_primary = TRUE;
2723 #ifdef BACKUP_SINGLE_ROUTER
2725 server->id_entry->router = NULL;
2726 server->router = NULL;
2727 server->standalone = TRUE;
2729 #endif /* BACKUP_SINGLE_ROUTER */
2731 if (server->server_type == SILC_BACKUP_ROUTER) {
2732 server->server_type = SILC_ROUTER;
2734 /* We'll need to constantly try to reconnect to the primary
2735 router so that we'll see when it comes back online. */
2736 silc_server_backup_reconnect(server, sock->ip, sock->port,
2737 silc_server_backup_connected,
2741 /* Mark this connection as replaced */
2742 silc_server_backup_replaced_add(server, user_data->id,
2745 } else if (backup_router) {
2746 SILC_LOG_INFO(("Enabling the use of backup router %s",
2747 backup_router->server_name));
2748 SILC_LOG_DEBUG(("Enabling the use of backup router %s",
2749 backup_router->server_name));
2751 /* Mark this connection as replaced */
2752 silc_server_backup_replaced_add(server, user_data->id,
2756 if (!backup_router) {
2757 /* Free all client entries that this server owns as they will
2758 become invalid now as well. */
2760 silc_server_remove_clients_by_server(server, user_data, TRUE);
2761 if (server->server_type == SILC_SERVER)
2762 silc_server_remove_channels_by_server(server, user_data);
2764 /* Update the client entries of this server to the new backup
2765 router. This also removes the clients that *really* was owned
2766 by the primary router and went down with the router. */
2767 silc_server_update_clients_by_server(server, user_data, backup_router,
2769 silc_server_update_servers_by_server(server, user_data, backup_router);
2770 if (server->server_type == SILC_SERVER)
2771 silc_server_update_channels_by_server(server, user_data,
2775 /* Free the server entry */
2776 silc_server_backup_del(server, user_data);
2777 silc_server_backup_replaced_del(server, user_data);
2778 silc_idlist_del_data(user_data);
2779 if (!silc_idlist_del_server(server->local_list, user_data))
2780 silc_idlist_del_server(server->global_list, user_data);
2781 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2782 server->stat.my_servers--;
2784 server->stat.my_routers--;
2785 server->stat.routers--;
2787 server->stat.servers--;
2788 if (server->server_type == SILC_ROUTER)
2789 server->stat.cell_servers--;
2791 if (backup_router) {
2792 /* Announce all of our stuff that was created about 5 minutes ago.
2793 The backup router knows all the other stuff already. */
2794 if (server->server_type == SILC_ROUTER)
2795 silc_server_announce_servers(server, FALSE, time(0) - 300,
2796 backup_router->connection);
2798 /* Announce our clients and channels to the router */
2799 silc_server_announce_clients(server, time(0) - 300,
2800 backup_router->connection);
2801 silc_server_announce_channels(server, time(0) - 300,
2802 backup_router->connection);
2808 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2810 silc_idlist_del_data(user_data);
2811 silc_free(user_data);
2816 /* If any protocol is active cancel its execution */
2817 if (sock->protocol) {
2818 silc_protocol_cancel(sock->protocol, server->schedule);
2819 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2820 silc_protocol_execute_final(sock->protocol, server->schedule);
2821 sock->protocol = NULL;
2824 sock->user_data = NULL;
2827 /* Removes client from all channels it has joined. This is used when client
2828 connection is disconnected. If the client on a channel is last, the
2829 channel is removed as well. This sends the SIGNOFF notify types. */
2831 void silc_server_remove_from_channels(SilcServer server,
2832 SilcSocketConnection sock,
2833 SilcClientEntry client,
2835 const char *signoff_message,
2838 SilcChannelEntry channel;
2839 SilcChannelClientEntry chl;
2840 SilcHashTableList htl;
2843 SILC_LOG_DEBUG(("Start"));
2845 if (!client || !client->id)
2848 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2852 /* Remove the client from all channels. The client is removed from
2853 the channels' user list. */
2854 silc_hash_table_list(client->channels, &htl);
2855 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
2856 channel = chl->channel;
2858 /* Remove channel if this is last client leaving the channel, unless
2859 the channel is permanent. */
2860 if (server->server_type == SILC_ROUTER &&
2861 silc_hash_table_count(channel->user_list) < 2) {
2862 silc_server_channel_delete(server, channel);
2866 silc_hash_table_del(client->channels, channel);
2867 silc_hash_table_del(channel->user_list, chl->client);
2868 channel->user_count--;
2870 /* If there is no global users on the channel anymore mark the channel
2871 as local channel. Do not check if the removed client is local client. */
2872 if (server->server_type != SILC_ROUTER && channel->global_users &&
2873 chl->client->router && !silc_server_channel_has_global(channel))
2874 channel->global_users = FALSE;
2878 /* Update statistics */
2879 if (client->connection)
2880 server->stat.my_chanclients--;
2881 if (server->server_type == SILC_ROUTER) {
2882 server->stat.cell_chanclients--;
2883 server->stat.chanclients--;
2886 /* If there is not at least one local user on the channel then we don't
2887 need the channel entry anymore, we can remove it safely, unless the
2888 channel is permanent channel */
2889 if (server->server_type != SILC_ROUTER &&
2890 !silc_server_channel_has_local(channel)) {
2891 /* Notify about leaving client if this channel has global users. */
2892 if (notify && channel->global_users)
2893 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2894 SILC_NOTIFY_TYPE_SIGNOFF,
2895 signoff_message ? 2 : 1,
2896 clidp->data, clidp->len,
2897 signoff_message, signoff_message ?
2898 strlen(signoff_message) : 0);
2900 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2901 silc_server_channel_delete(server, channel);
2905 /* Send notify to channel about client leaving SILC and channel too */
2907 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2908 SILC_NOTIFY_TYPE_SIGNOFF,
2909 signoff_message ? 2 : 1,
2910 clidp->data, clidp->len,
2911 signoff_message, signoff_message ?
2912 strlen(signoff_message) : 0);
2914 /* Re-generate channel key if needed */
2915 if (keygen && !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2916 if (!silc_server_create_channel_key(server, channel, 0))
2919 /* Send the channel key to the channel. The key of course is not sent
2920 to the client who was removed from the channel. */
2921 silc_server_send_channel_key(server, client->connection, channel,
2922 server->server_type == SILC_ROUTER ?
2923 FALSE : !server->standalone);
2927 silc_hash_table_list_reset(&htl);
2928 silc_buffer_free(clidp);
2931 /* Removes client from one channel. This is used for example when client
2932 calls LEAVE command to remove itself from the channel. Returns TRUE
2933 if channel still exists and FALSE if the channel is removed when
2934 last client leaves the channel. If `notify' is FALSE notify messages
2937 bool silc_server_remove_from_one_channel(SilcServer server,
2938 SilcSocketConnection sock,
2939 SilcChannelEntry channel,
2940 SilcClientEntry client,
2943 SilcChannelClientEntry chl;
2946 SILC_LOG_DEBUG(("Removing %s from channel %s",
2947 silc_id_render(client->id, SILC_ID_CLIENT),
2948 channel->channel_name));
2950 /* Get the entry to the channel, if this client is not on the channel
2952 if (!silc_hash_table_find(client->channels, channel, NULL, (void *)&chl))
2955 /* Remove channel if this is last client leaving the channel, unless
2956 the channel is permanent. */
2957 if (server->server_type == SILC_ROUTER &&
2958 silc_hash_table_count(channel->user_list) < 2) {
2959 silc_server_channel_delete(server, channel);
2963 silc_hash_table_del(client->channels, chl->channel);
2964 silc_hash_table_del(channel->user_list, chl->client);
2965 channel->user_count--;
2967 /* If there is no global users on the channel anymore mark the channel
2968 as local channel. Do not check if the client is local client. */
2969 if (server->server_type != SILC_ROUTER && channel->global_users &&
2970 chl->client->router && !silc_server_channel_has_global(channel))
2971 channel->global_users = FALSE;
2975 /* Update statistics */
2976 if (client->connection)
2977 server->stat.my_chanclients--;
2978 if (server->server_type == SILC_ROUTER) {
2979 server->stat.cell_chanclients--;
2980 server->stat.chanclients--;
2983 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2987 /* If there is not at least one local user on the channel then we don't
2988 need the channel entry anymore, we can remove it safely, unless the
2989 channel is permanent channel */
2990 if (server->server_type != SILC_ROUTER &&
2991 !silc_server_channel_has_local(channel)) {
2992 /* Notify about leaving client if this channel has global users. */
2993 if (notify && channel->global_users)
2994 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2995 SILC_NOTIFY_TYPE_LEAVE, 1,
2996 clidp->data, clidp->len);
2998 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2999 silc_server_channel_delete(server, channel);
3000 silc_buffer_free(clidp);
3004 /* Send notify to channel about client leaving the channel */
3006 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3007 SILC_NOTIFY_TYPE_LEAVE, 1,
3008 clidp->data, clidp->len);
3010 silc_buffer_free(clidp);
3014 /* Timeout callback. This is called if connection is idle or for some
3015 other reason is not responding within some period of time. This
3016 disconnects the remote end. */
3018 SILC_TASK_CALLBACK(silc_server_timeout_remote)
3020 SilcServer server = (SilcServer)context;
3021 SilcSocketConnection sock = server->sockets[fd];
3022 SilcProtocolType protocol = 0;
3024 SILC_LOG_DEBUG(("Start"));
3029 SILC_LOG_ERROR(("No response from %s (%s), Connection timeout",
3030 sock->hostname, sock->ip));
3032 /* If we have protocol active we must assure that we call the protocol's
3033 final callback so that all the memory is freed. */
3034 if (sock->protocol) {
3035 protocol = sock->protocol->protocol->type;
3036 silc_protocol_cancel(sock->protocol, server->schedule);
3037 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
3038 silc_protocol_execute_final(sock->protocol, server->schedule);
3039 sock->protocol = NULL;
3043 if (sock->user_data)
3044 silc_server_free_sock_user_data(server, sock, NULL);
3046 silc_server_disconnect_remote(server, sock,
3048 SILC_PROTOCOL_SERVER_CONNECTION_AUTH ?
3049 SILC_STATUS_ERR_AUTH_FAILED :
3050 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED,
3051 "Connection timeout");
3054 /* Creates new channel. Sends NEW_CHANNEL packet to primary route. This
3055 function may be used only by router. In real SILC network all channels
3056 are created by routers thus this function is never used by normal
3059 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
3060 SilcServerID *router_id,
3066 SilcChannelID *channel_id;
3067 SilcChannelEntry entry;
3071 SILC_LOG_DEBUG(("Creating new channel"));
3074 cipher = SILC_DEFAULT_CIPHER;
3076 hmac = SILC_DEFAULT_HMAC;
3078 /* Allocate cipher */
3079 if (!silc_cipher_alloc(cipher, &key))
3083 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
3084 silc_cipher_free(key);
3088 channel_name = strdup(channel_name);
3090 /* Create the channel ID */
3091 if (!silc_id_create_channel_id(server, router_id, server->rng,
3093 silc_free(channel_name);
3094 silc_cipher_free(key);
3095 silc_hmac_free(newhmac);
3099 /* Create the channel */
3100 entry = silc_idlist_add_channel(server->local_list, channel_name,
3101 SILC_CHANNEL_MODE_NONE, channel_id,
3102 NULL, key, newhmac, 0);
3104 silc_free(channel_name);
3105 silc_cipher_free(key);
3106 silc_hmac_free(newhmac);
3107 silc_free(channel_id);
3111 entry->cipher = strdup(cipher);
3112 entry->hmac_name = strdup(hmac);
3114 /* Now create the actual key material */
3115 if (!silc_server_create_channel_key(server, entry,
3116 silc_cipher_get_key_len(key) / 8)) {
3117 silc_idlist_del_channel(server->local_list, entry);
3121 /* Notify other routers about the new channel. We send the packet
3122 to our primary route. */
3123 if (broadcast && server->standalone == FALSE)
3124 silc_server_send_new_channel(server, server->router->connection, TRUE,
3125 channel_name, entry->id,
3126 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
3129 /* Distribute to backup routers */
3130 if (broadcast && server->server_type == SILC_ROUTER) {
3133 SilcUInt32 name_len = strlen(channel_name);
3134 SilcUInt32 channel_id_len = silc_id_get_len(entry->id, SILC_ID_CHANNEL);
3135 cid = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3137 packet = silc_channel_payload_encode(channel_name, name_len,
3138 cid, channel_id_len, entry->mode);
3139 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
3140 packet->data, packet->len, FALSE, TRUE);
3142 silc_buffer_free(packet);
3145 server->stat.my_channels++;
3146 if (server->server_type == SILC_ROUTER) {
3147 server->stat.channels++;
3148 server->stat.cell_channels++;
3149 entry->users_resolved = TRUE;
3155 /* Same as above but creates the channel with Channel ID `channel_id. */
3158 silc_server_create_new_channel_with_id(SilcServer server,
3162 SilcChannelID *channel_id,
3165 SilcChannelEntry entry;
3169 SILC_LOG_DEBUG(("Creating new channel"));
3172 cipher = SILC_DEFAULT_CIPHER;
3174 hmac = SILC_DEFAULT_HMAC;
3176 /* Allocate cipher */
3177 if (!silc_cipher_alloc(cipher, &key))
3181 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
3182 silc_cipher_free(key);
3186 channel_name = strdup(channel_name);
3188 /* Create the channel */
3189 entry = silc_idlist_add_channel(server->local_list, channel_name,
3190 SILC_CHANNEL_MODE_NONE, channel_id,
3191 NULL, key, newhmac, 0);
3193 silc_cipher_free(key);
3194 silc_hmac_free(newhmac);
3195 silc_free(channel_name);
3199 /* Now create the actual key material */
3200 if (!silc_server_create_channel_key(server, entry,
3201 silc_cipher_get_key_len(key) / 8)) {
3202 silc_idlist_del_channel(server->local_list, entry);
3206 /* Notify other routers about the new channel. We send the packet
3207 to our primary route. */
3208 if (broadcast && server->standalone == FALSE)
3209 silc_server_send_new_channel(server, server->router->connection, TRUE,
3210 channel_name, entry->id,
3211 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
3214 /* Distribute to backup routers */
3215 if (broadcast && server->server_type == SILC_ROUTER) {
3218 SilcUInt32 name_len = strlen(channel_name);
3219 SilcUInt32 channel_id_len = silc_id_get_len(entry->id, SILC_ID_CHANNEL);
3220 cid = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3222 packet = silc_channel_payload_encode(channel_name, name_len,
3223 cid, channel_id_len, entry->mode);
3224 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
3225 packet->data, packet->len, FALSE, TRUE);
3227 silc_buffer_free(packet);
3230 server->stat.my_channels++;
3231 if (server->server_type == SILC_ROUTER) {
3232 server->stat.channels++;
3233 server->stat.cell_channels++;
3234 entry->users_resolved = TRUE;
3240 /* Channel's key re-key timeout callback. */
3242 SILC_TASK_CALLBACK(silc_server_channel_key_rekey)
3244 SilcServerChannelRekey rekey = (SilcServerChannelRekey)context;
3245 SilcServer server = (SilcServer)rekey->context;
3249 if (!silc_server_create_channel_key(server, rekey->channel, rekey->key_len))
3252 silc_server_send_channel_key(server, NULL, rekey->channel, FALSE);
3255 /* Generates new channel key. This is used to create the initial channel key
3256 but also to re-generate new key for channel. If `key_len' is provided
3257 it is the bytes of the key length. */
3259 bool silc_server_create_channel_key(SilcServer server,
3260 SilcChannelEntry channel,
3264 unsigned char channel_key[32], hash[32];
3267 SILC_LOG_DEBUG(("Generating channel key"));
3269 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3270 SILC_LOG_DEBUG(("Channel has private keys, will not generate new key"));
3274 if (!channel->channel_key)
3275 if (!silc_cipher_alloc(SILC_DEFAULT_CIPHER, &channel->channel_key)) {
3276 channel->channel_key = NULL;
3282 else if (channel->key_len)
3283 len = channel->key_len / 8;
3285 len = silc_cipher_get_key_len(channel->channel_key) / 8;
3287 /* Create channel key */
3288 for (i = 0; i < len; i++) channel_key[i] = silc_rng_get_byte(server->rng);
3291 silc_cipher_set_key(channel->channel_key, channel_key, len * 8);
3293 /* Remove old key if exists */
3295 memset(channel->key, 0, channel->key_len / 8);
3296 silc_free(channel->key);
3300 channel->key_len = len * 8;
3301 channel->key = silc_memdup(channel_key, len);
3302 memset(channel_key, 0, sizeof(channel_key));
3304 /* Generate HMAC key from the channel key data and set it */
3306 silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
3307 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key, len, hash);
3308 silc_hmac_set_key(channel->hmac, hash,
3309 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3310 memset(hash, 0, sizeof(hash));
3312 if (server->server_type == SILC_ROUTER) {
3313 if (!channel->rekey)
3314 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
3315 channel->rekey->context = (void *)server;
3316 channel->rekey->channel = channel;
3317 channel->rekey->key_len = key_len;
3318 if (channel->rekey->task)
3319 silc_schedule_task_del(server->schedule, channel->rekey->task);
3321 channel->rekey->task =
3322 silc_schedule_task_add(server->schedule, 0,
3323 silc_server_channel_key_rekey,
3324 (void *)channel->rekey,
3325 server->config->channel_rekey_secs, 0,
3327 SILC_TASK_PRI_NORMAL);
3333 /* Saves the channel key found in the encoded `key_payload' buffer. This
3334 function is used when we receive Channel Key Payload and also when we're
3335 processing JOIN command reply. Returns entry to the channel. */
3337 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
3338 SilcBuffer key_payload,
3339 SilcChannelEntry channel)
3341 SilcChannelKeyPayload payload = NULL;
3342 SilcChannelID *id = NULL;
3343 unsigned char *tmp, hash[32];
3347 SILC_LOG_DEBUG(("Start"));
3349 /* Decode channel key payload */
3350 payload = silc_channel_key_payload_parse(key_payload->data,
3353 SILC_LOG_ERROR(("Bad channel key payload received, dropped"));
3358 /* Get the channel entry */
3361 /* Get channel ID */
3362 tmp = silc_channel_key_get_id(payload, &tmp_len);
3363 id = silc_id_str2id(tmp, tmp_len, SILC_ID_CHANNEL);
3369 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
3371 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
3373 SILC_LOG_ERROR(("Received key for non-existent channel %s",
3374 silc_id_render(id, SILC_ID_CHANNEL)));
3380 tmp = silc_channel_key_get_key(payload, &tmp_len);
3386 cipher = silc_channel_key_get_cipher(payload, NULL);
3392 /* Remove old key if exists */
3394 memset(channel->key, 0, channel->key_len / 8);
3395 silc_free(channel->key);
3396 silc_cipher_free(channel->channel_key);
3399 /* Create new cipher */
3400 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3401 channel->channel_key = NULL;
3406 if (channel->cipher)
3407 silc_free(channel->cipher);
3408 channel->cipher = strdup(cipher);
3411 channel->key_len = tmp_len * 8;
3412 channel->key = silc_memdup(tmp, tmp_len);
3413 silc_cipher_set_key(channel->channel_key, tmp, channel->key_len);
3415 /* Generate HMAC key from the channel key data and set it */
3417 silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
3418 silc_hash_make(silc_hmac_get_hash(channel->hmac), tmp, tmp_len, hash);
3419 silc_hmac_set_key(channel->hmac, hash,
3420 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3422 memset(hash, 0, sizeof(hash));
3423 memset(tmp, 0, tmp_len);
3425 if (server->server_type == SILC_ROUTER) {
3426 if (!channel->rekey)
3427 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
3428 channel->rekey->context = (void *)server;
3429 channel->rekey->channel = channel;
3430 if (channel->rekey->task)
3431 silc_schedule_task_del(server->schedule, channel->rekey->task);
3433 channel->rekey->task =
3434 silc_schedule_task_add(server->schedule, 0,
3435 silc_server_channel_key_rekey,
3436 (void *)channel->rekey,
3437 server->config->channel_rekey_secs, 0,
3439 SILC_TASK_PRI_NORMAL);
3445 silc_channel_key_payload_free(payload);
3450 /* Heartbeat callback. This function is set as argument for the
3451 silc_socket_set_heartbeat function. The library will call this function
3452 at the set time interval. */
3454 void silc_server_perform_heartbeat(SilcSocketConnection sock,
3457 SilcServerHBContext hb = (SilcServerHBContext)hb_context;
3459 SILC_LOG_DEBUG(("Sending heartbeat to %s (%s)", sock->hostname, sock->ip));
3461 /* Send the heartbeat */
3462 silc_server_send_heartbeat(hb->server, sock);
3465 /* Returns assembled of all servers in the given ID list. The packet's
3466 form is dictated by the New ID payload. */
3468 static void silc_server_announce_get_servers(SilcServer server,
3469 SilcServerEntry remote,
3471 SilcBuffer *servers,
3472 unsigned long creation_time)
3474 SilcIDCacheList list;
3475 SilcIDCacheEntry id_cache;
3476 SilcServerEntry entry;
3479 /* Go through all clients in the list */
3480 if (silc_idcache_get_all(id_list->servers, &list)) {
3481 if (silc_idcache_list_first(list, &id_cache)) {
3483 entry = (SilcServerEntry)id_cache->context;
3485 /* Do not announce the one we've sending our announcements and
3486 do not announce ourself. Also check the creation time if it's
3488 if ((entry == remote) || (entry == server->id_entry) ||
3489 (creation_time && entry->data.created < creation_time)) {
3490 if (!silc_idcache_list_next(list, &id_cache))
3495 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
3497 *servers = silc_buffer_realloc(*servers,
3499 (*servers)->truelen + idp->len :
3501 silc_buffer_pull_tail(*servers, ((*servers)->end - (*servers)->data));
3502 silc_buffer_put(*servers, idp->data, idp->len);
3503 silc_buffer_pull(*servers, idp->len);
3504 silc_buffer_free(idp);
3506 if (!silc_idcache_list_next(list, &id_cache))
3511 silc_idcache_list_free(list);
3516 silc_server_announce_encode_notify(SilcNotifyType notify, SilcUInt32 argc, ...)
3522 p = silc_notify_payload_encode(notify, argc, ap);
3528 /* This function is used by router to announce existing servers to our
3529 primary router when we've connected to it. If `creation_time' is non-zero
3530 then only the servers that has been created after the `creation_time'
3531 will be announced. */
3533 void silc_server_announce_servers(SilcServer server, bool global,
3534 unsigned long creation_time,
3535 SilcSocketConnection remote)
3537 SilcBuffer servers = NULL;
3539 SILC_LOG_DEBUG(("Announcing servers"));
3541 /* Get servers in local list */
3542 silc_server_announce_get_servers(server, remote->user_data,
3543 server->local_list, &servers,
3547 /* Get servers in global list */
3548 silc_server_announce_get_servers(server, remote->user_data,
3549 server->global_list, &servers,
3553 silc_buffer_push(servers, servers->data - servers->head);
3554 SILC_LOG_HEXDUMP(("servers"), servers->data, servers->len);
3556 /* Send the packet */
3557 silc_server_packet_send(server, remote,
3558 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3559 servers->data, servers->len, TRUE);
3561 silc_buffer_free(servers);
3565 /* Returns assembled packet of all clients in the given ID list. The
3566 packet's form is dictated by the New ID Payload. */
3568 static void silc_server_announce_get_clients(SilcServer server,
3570 SilcBuffer *clients,
3572 unsigned long creation_time)
3574 SilcIDCacheList list;
3575 SilcIDCacheEntry id_cache;
3576 SilcClientEntry client;
3579 unsigned char mode[4];
3581 /* Go through all clients in the list */
3582 if (silc_idcache_get_all(id_list->clients, &list)) {
3583 if (silc_idcache_list_first(list, &id_cache)) {
3585 client = (SilcClientEntry)id_cache->context;
3587 if (creation_time && client->data.created < creation_time) {
3588 if (!silc_idcache_list_next(list, &id_cache))
3593 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3595 *clients = silc_buffer_realloc(*clients,
3597 (*clients)->truelen + idp->len :
3599 silc_buffer_pull_tail(*clients, ((*clients)->end - (*clients)->data));
3600 silc_buffer_put(*clients, idp->data, idp->len);
3601 silc_buffer_pull(*clients, idp->len);
3603 SILC_PUT32_MSB(client->mode, mode);
3604 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_UMODE_CHANGE,
3605 2, idp->data, idp->len,
3607 *umodes = silc_buffer_realloc(*umodes,
3609 (*umodes)->truelen + tmp->len :
3611 silc_buffer_pull_tail(*umodes, ((*umodes)->end - (*umodes)->data));
3612 silc_buffer_put(*umodes, tmp->data, tmp->len);
3613 silc_buffer_pull(*umodes, tmp->len);
3614 silc_buffer_free(tmp);
3616 silc_buffer_free(idp);
3618 if (!silc_idcache_list_next(list, &id_cache))
3623 silc_idcache_list_free(list);
3627 /* This function is used to announce our existing clients to our router
3628 when we've connected to it. If `creation_time' is non-zero then only
3629 the clients that has been created after the `creation_time' will be
3632 void silc_server_announce_clients(SilcServer server,
3633 unsigned long creation_time,
3634 SilcSocketConnection remote)
3636 SilcBuffer clients = NULL;
3637 SilcBuffer umodes = NULL;
3639 SILC_LOG_DEBUG(("Announcing clients"));
3641 /* Get clients in local list */
3642 silc_server_announce_get_clients(server, server->local_list,
3643 &clients, &umodes, creation_time);
3645 /* As router we announce our global list as well */
3646 if (server->server_type == SILC_ROUTER)
3647 silc_server_announce_get_clients(server, server->global_list,
3648 &clients, &umodes, creation_time);
3651 silc_buffer_push(clients, clients->data - clients->head);
3652 SILC_LOG_HEXDUMP(("clients"), clients->data, clients->len);
3654 /* Send the packet */
3655 silc_server_packet_send(server, remote,
3656 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3657 clients->data, clients->len, TRUE);
3659 silc_buffer_free(clients);
3663 silc_buffer_push(umodes, umodes->data - umodes->head);
3664 SILC_LOG_HEXDUMP(("umodes"), umodes->data, umodes->len);
3666 /* Send the packet */
3667 silc_server_packet_send(server, remote,
3668 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3669 umodes->data, umodes->len, TRUE);
3671 silc_buffer_free(umodes);
3675 /* Returns channel's topic for announcing it */
3677 void silc_server_announce_get_channel_topic(SilcServer server,
3678 SilcChannelEntry channel,
3683 if (channel->topic) {
3684 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3685 *topic = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_TOPIC_SET, 2,
3686 chidp->data, chidp->len,
3688 strlen(channel->topic));
3689 silc_buffer_free(chidp);
3693 /* Returns assembled packets for channel users of the `channel'. */
3695 void silc_server_announce_get_channel_users(SilcServer server,
3696 SilcChannelEntry channel,
3697 SilcBuffer *channel_modes,
3698 SilcBuffer *channel_users,
3699 SilcBuffer *channel_users_modes)
3701 SilcChannelClientEntry chl;
3702 SilcHashTableList htl;
3703 SilcBuffer chidp, clidp, csidp;
3706 unsigned char mode[4], *fkey = NULL;
3707 SilcUInt32 fkey_len = 0;
3710 SILC_LOG_DEBUG(("Start"));
3712 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3713 csidp = silc_id_payload_encode(server->id, SILC_ID_SERVER);
3716 SILC_PUT32_MSB(channel->mode, mode);
3717 hmac = channel->hmac ? (char *)silc_hmac_get_name(channel->hmac) : NULL;
3718 if (channel->founder_key)
3719 fkey = silc_pkcs_public_key_encode(channel->founder_key, &fkey_len);
3721 silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CMODE_CHANGE,
3722 6, csidp->data, csidp->len,
3725 hmac, hmac ? strlen(hmac) : 0,
3726 channel->passphrase,
3727 channel->passphrase ?
3728 strlen(channel->passphrase) : 0,
3732 silc_buffer_realloc(*channel_modes,
3734 (*channel_modes)->truelen + len : len));
3735 silc_buffer_pull_tail(*channel_modes,
3736 ((*channel_modes)->end -
3737 (*channel_modes)->data));
3738 silc_buffer_put(*channel_modes, tmp->data, tmp->len);
3739 silc_buffer_pull(*channel_modes, len);
3740 silc_buffer_free(tmp);
3745 /* Now find all users on the channel */
3746 silc_hash_table_list(channel->user_list, &htl);
3747 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3748 clidp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3751 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_JOIN, 2,
3752 clidp->data, clidp->len,
3753 chidp->data, chidp->len);
3756 silc_buffer_realloc(*channel_users,
3758 (*channel_users)->truelen + len : len));
3759 silc_buffer_pull_tail(*channel_users,
3760 ((*channel_users)->end -
3761 (*channel_users)->data));
3763 silc_buffer_put(*channel_users, tmp->data, tmp->len);
3764 silc_buffer_pull(*channel_users, len);
3765 silc_buffer_free(tmp);
3767 /* CUMODE notify for mode change on the channel */
3768 SILC_PUT32_MSB(chl->mode, mode);
3769 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && channel->founder_key)
3770 fkey = silc_pkcs_public_key_encode(channel->founder_key, &fkey_len);
3771 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CUMODE_CHANGE,
3772 4, csidp->data, csidp->len,
3774 clidp->data, clidp->len,
3777 *channel_users_modes =
3778 silc_buffer_realloc(*channel_users_modes,
3779 (*channel_users_modes ?
3780 (*channel_users_modes)->truelen + len : len));
3781 silc_buffer_pull_tail(*channel_users_modes,
3782 ((*channel_users_modes)->end -
3783 (*channel_users_modes)->data));
3785 silc_buffer_put(*channel_users_modes, tmp->data, tmp->len);
3786 silc_buffer_pull(*channel_users_modes, len);
3787 silc_buffer_free(tmp);
3791 silc_buffer_free(clidp);
3793 silc_hash_table_list_reset(&htl);
3794 silc_buffer_free(chidp);
3795 silc_buffer_free(csidp);
3798 /* Returns assembled packets for all channels and users on those channels
3799 from the given ID List. The packets are in the form dictated by the
3800 New Channel and New Channel User payloads. */
3802 void silc_server_announce_get_channels(SilcServer server,
3804 SilcBuffer *channels,
3805 SilcBuffer **channel_modes,
3806 SilcBuffer *channel_users,
3807 SilcBuffer **channel_users_modes,
3808 SilcUInt32 *channel_users_modes_c,
3809 SilcBuffer **channel_topics,
3810 SilcChannelID ***channel_ids,
3811 unsigned long creation_time)
3813 SilcIDCacheList list;
3814 SilcIDCacheEntry id_cache;
3815 SilcChannelEntry channel;
3818 SilcUInt16 name_len;
3820 int i = *channel_users_modes_c;
3823 SILC_LOG_DEBUG(("Start"));
3825 /* Go through all channels in the list */
3826 if (silc_idcache_get_all(id_list->channels, &list)) {
3827 if (silc_idcache_list_first(list, &id_cache)) {
3829 channel = (SilcChannelEntry)id_cache->context;
3831 if (creation_time && channel->created < creation_time)
3836 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3837 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3838 name_len = strlen(channel->channel_name);
3841 len = 4 + name_len + id_len + 4;
3843 silc_buffer_realloc(*channels,
3844 (*channels ? (*channels)->truelen +
3846 silc_buffer_pull_tail(*channels,
3847 ((*channels)->end - (*channels)->data));
3848 silc_buffer_format(*channels,
3849 SILC_STR_UI_SHORT(name_len),
3850 SILC_STR_UI_XNSTRING(channel->channel_name,
3852 SILC_STR_UI_SHORT(id_len),
3853 SILC_STR_UI_XNSTRING(cid, id_len),
3854 SILC_STR_UI_INT(channel->mode),
3856 silc_buffer_pull(*channels, len);
3859 /* Channel user modes */
3860 *channel_users_modes = silc_realloc(*channel_users_modes,
3861 sizeof(**channel_users_modes) *
3863 (*channel_users_modes)[i] = NULL;
3864 *channel_modes = silc_realloc(*channel_modes,
3865 sizeof(**channel_modes) * (i + 1));
3866 (*channel_modes)[i] = NULL;
3867 *channel_ids = silc_realloc(*channel_ids,
3868 sizeof(**channel_ids) * (i + 1));
3869 (*channel_ids)[i] = NULL;
3870 silc_server_announce_get_channel_users(server, channel,
3871 &(*channel_modes)[i],
3873 &(*channel_users_modes)[i]);
3874 (*channel_ids)[i] = channel->id;
3876 /* Channel's topic */
3877 *channel_topics = silc_realloc(*channel_topics,
3878 sizeof(**channel_topics) * (i + 1));
3879 (*channel_topics)[i] = NULL;
3880 silc_server_announce_get_channel_topic(server, channel,
3881 &(*channel_topics)[i]);
3884 if (!silc_idcache_list_next(list, &id_cache))
3888 *channel_users_modes_c += i;
3891 silc_idcache_list_free(list);
3895 /* This function is used to announce our existing channels to our router
3896 when we've connected to it. This also announces the users on the
3897 channels to the router. If the `creation_time' is non-zero only the
3898 channels that was created after the `creation_time' are announced.
3899 Note that the channel users are still announced even if the `creation_time'
3902 void silc_server_announce_channels(SilcServer server,
3903 unsigned long creation_time,
3904 SilcSocketConnection remote)
3906 SilcBuffer channels = NULL, *channel_modes = NULL, channel_users = NULL;
3907 SilcBuffer *channel_users_modes = NULL;
3908 SilcBuffer *channel_topics = NULL;
3909 SilcUInt32 channel_users_modes_c = 0;
3910 SilcChannelID **channel_ids = NULL;
3912 SILC_LOG_DEBUG(("Announcing channels and channel users"));
3914 /* Get channels and channel users in local list */
3915 silc_server_announce_get_channels(server, server->local_list,
3916 &channels, &channel_modes,
3918 &channel_users_modes,
3919 &channel_users_modes_c,
3921 &channel_ids, creation_time);
3923 /* Get channels and channel users in global list */
3924 if (server->server_type != SILC_SERVER)
3925 silc_server_announce_get_channels(server, server->global_list,
3926 &channels, &channel_modes,
3928 &channel_users_modes,
3929 &channel_users_modes_c,
3931 &channel_ids, creation_time);
3934 silc_buffer_push(channels, channels->data - channels->head);
3935 SILC_LOG_HEXDUMP(("channels"), channels->data, channels->len);
3937 /* Send the packet */
3938 silc_server_packet_send(server, remote,
3939 SILC_PACKET_NEW_CHANNEL, SILC_PACKET_FLAG_LIST,
3940 channels->data, channels->len,
3943 silc_buffer_free(channels);
3946 if (channel_modes) {
3949 for (i = 0; i < channel_users_modes_c; i++) {
3950 if (!channel_modes[i])
3952 silc_buffer_push(channel_modes[i],
3953 channel_modes[i]->data -
3954 channel_modes[i]->head);
3955 SILC_LOG_HEXDUMP(("channel modes"), channel_modes[i]->data,
3956 channel_modes[i]->len);
3957 silc_server_packet_send_dest(server, remote,
3958 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3959 channel_ids[i], SILC_ID_CHANNEL,
3960 channel_modes[i]->data,
3961 channel_modes[i]->len,
3963 silc_buffer_free(channel_modes[i]);
3965 silc_free(channel_modes);
3968 if (channel_users) {
3969 silc_buffer_push(channel_users, channel_users->data - channel_users->head);
3970 SILC_LOG_HEXDUMP(("channel users"), channel_users->data,
3971 channel_users->len);
3973 /* Send the packet */
3974 silc_server_packet_send(server, remote,
3975 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3976 channel_users->data, channel_users->len,
3979 silc_buffer_free(channel_users);
3982 if (channel_users_modes) {
3985 for (i = 0; i < channel_users_modes_c; i++) {
3986 if (!channel_users_modes[i])
3988 silc_buffer_push(channel_users_modes[i],
3989 channel_users_modes[i]->data -
3990 channel_users_modes[i]->head);
3991 SILC_LOG_HEXDUMP(("channel users modes"), channel_users_modes[i]->data,
3992 channel_users_modes[i]->len);
3993 silc_server_packet_send_dest(server, remote,
3994 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3995 channel_ids[i], SILC_ID_CHANNEL,
3996 channel_users_modes[i]->data,
3997 channel_users_modes[i]->len,
3999 silc_buffer_free(channel_users_modes[i]);
4001 silc_free(channel_users_modes);
4004 if (channel_topics) {
4007 for (i = 0; i < channel_users_modes_c; i++) {
4008 if (!channel_topics[i])
4011 silc_buffer_push(channel_topics[i],
4012 channel_topics[i]->data -
4013 channel_topics[i]->head);
4014 SILC_LOG_HEXDUMP(("channel topic"), channel_topics[i]->data,
4015 channel_topics[i]->len);
4016 silc_server_packet_send_dest(server, remote,
4017 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
4018 channel_ids[i], SILC_ID_CHANNEL,
4019 channel_topics[i]->data,
4020 channel_topics[i]->len,
4022 silc_buffer_free(channel_topics[i]);
4024 silc_free(channel_topics);
4027 silc_free(channel_ids);
4030 /* Failure timeout callback. If this is called then we will immediately
4031 process the received failure. We always process the failure with timeout
4032 since we do not want to blindly trust to received failure packets.
4033 This won't be called (the timeout is cancelled) if the failure was
4034 bogus (it is bogus if remote does not close the connection after sending
4037 SILC_TASK_CALLBACK(silc_server_failure_callback)
4039 SilcServerFailureContext f = (SilcServerFailureContext)context;
4041 if (f->sock->protocol) {
4042 f->sock->protocol->state = SILC_PROTOCOL_STATE_FAILURE;
4043 silc_protocol_execute(f->sock->protocol, f->server->schedule, 0, 0);
4049 /* Assembles user list and users mode list from the `channel'. */
4051 bool silc_server_get_users_on_channel(SilcServer server,
4052 SilcChannelEntry channel,
4053 SilcBuffer *user_list,
4054 SilcBuffer *mode_list,
4055 SilcUInt32 *user_count)
4057 SilcChannelClientEntry chl;
4058 SilcHashTableList htl;
4059 SilcBuffer client_id_list;
4060 SilcBuffer client_mode_list;
4062 SilcUInt32 list_count = 0, len = 0;
4064 if (!silc_hash_table_count(channel->user_list))
4067 silc_hash_table_list(channel->user_list, &htl);
4068 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
4069 len += (silc_id_get_len(chl->client->id, SILC_ID_CLIENT) + 4);
4070 silc_hash_table_list_reset(&htl);
4072 client_id_list = silc_buffer_alloc(len);
4074 silc_buffer_alloc(4 * silc_hash_table_count(channel->user_list));
4075 silc_buffer_pull_tail(client_id_list, SILC_BUFFER_END(client_id_list));
4076 silc_buffer_pull_tail(client_mode_list, SILC_BUFFER_END(client_mode_list));
4078 silc_hash_table_list(channel->user_list, &htl);
4079 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
4081 idp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
4082 silc_buffer_put(client_id_list, idp->data, idp->len);
4083 silc_buffer_pull(client_id_list, idp->len);
4084 silc_buffer_free(idp);
4086 /* Client's mode on channel */
4087 SILC_PUT32_MSB(chl->mode, client_mode_list->data);
4088 silc_buffer_pull(client_mode_list, 4);
4092 silc_hash_table_list_reset(&htl);
4093 silc_buffer_push(client_id_list,
4094 client_id_list->data - client_id_list->head);
4095 silc_buffer_push(client_mode_list,
4096 client_mode_list->data - client_mode_list->head);
4098 *user_list = client_id_list;
4099 *mode_list = client_mode_list;
4100 *user_count = list_count;
4104 /* Saves users and their modes to the `channel'. */
4106 void silc_server_save_users_on_channel(SilcServer server,
4107 SilcSocketConnection sock,
4108 SilcChannelEntry channel,
4109 SilcClientID *noadd,
4110 SilcBuffer user_list,
4111 SilcBuffer mode_list,
4112 SilcUInt32 user_count)
4117 SilcClientID *client_id;
4118 SilcClientEntry client;
4119 SilcIDCacheEntry cache;
4120 SilcChannelClientEntry chl;
4123 SILC_LOG_DEBUG(("Start"));
4125 for (i = 0; i < user_count; i++) {
4127 SILC_GET16_MSB(idp_len, user_list->data + 2);
4129 client_id = silc_id_payload_parse_id(user_list->data, idp_len, NULL);
4130 silc_buffer_pull(user_list, idp_len);
4135 SILC_GET32_MSB(mode, mode_list->data);
4136 silc_buffer_pull(mode_list, 4);
4138 if (noadd && SILC_ID_CLIENT_COMPARE(client_id, noadd)) {
4139 silc_free(client_id);
4145 /* Check if we have this client cached already. */
4146 client = silc_idlist_find_client_by_id(server->local_list, client_id,
4147 server->server_type, &cache);
4149 client = silc_idlist_find_client_by_id(server->global_list,
4150 client_id, server->server_type,
4155 /* If router did not find such Client ID in its lists then this must
4156 be bogus client or some router in the net is buggy. */
4157 if (server->server_type == SILC_ROUTER) {
4158 silc_free(client_id);
4162 /* We don't have that client anywhere, add it. The client is added
4163 to global list since server didn't have it in the lists so it must be
4165 client = silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
4166 silc_id_dup(client_id, SILC_ID_CLIENT),
4167 sock->user_data, NULL, 0);
4169 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
4170 silc_free(client_id);
4174 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
4176 /* Found, if it is from global list we'll assure that we won't
4177 expire it now that the entry is on channel. */
4182 silc_free(client_id);
4184 if (!silc_server_client_on_channel(client, channel, &chl)) {
4185 /* Client was not on the channel, add it. */
4186 chl = silc_calloc(1, sizeof(*chl));
4187 chl->client = client;
4189 chl->channel = channel;
4190 silc_hash_table_add(channel->user_list, chl->client, chl);
4191 silc_hash_table_add(client->channels, chl->channel, chl);
4192 channel->user_count++;
4200 /* Saves channels and channels user modes to the `client'. Removes
4201 the client from those channels that are not sent in the list but
4204 void silc_server_save_user_channels(SilcServer server,
4205 SilcSocketConnection sock,
4206 SilcClientEntry client,
4207 SilcBuffer channels,
4208 SilcBuffer channels_user_modes)
4211 SilcUInt32 *chumodes;
4212 SilcChannelPayload entry;
4213 SilcChannelEntry channel;
4214 SilcChannelID *channel_id;
4215 SilcChannelClientEntry chl;
4216 SilcHashTable ht = NULL;
4217 SilcHashTableList htl;
4221 if (!channels ||!channels_user_modes)
4224 ch = silc_channel_payload_parse_list(channels->data, channels->len);
4225 if (ch && silc_get_mode_list(channels_user_modes, silc_dlist_count(ch),
4227 ht = silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL,
4228 NULL, NULL, NULL, TRUE);
4229 silc_dlist_start(ch);
4230 while ((entry = silc_dlist_get(ch)) != SILC_LIST_END) {
4231 /* Check if we have this channel, and add it if we don't have it.
4232 Also add the client on the channel unless it is there already. */
4233 channel_id = silc_channel_get_id_parse(entry);
4234 channel = silc_idlist_find_channel_by_id(server->local_list,
4237 channel = silc_idlist_find_channel_by_id(server->global_list,
4240 if (server->server_type != SILC_SERVER) {
4241 silc_free(channel_id);
4246 /* We don't have that channel anywhere, add it. */
4247 name = silc_channel_get_name(entry, NULL);
4248 channel = silc_idlist_add_channel(server->global_list, strdup(name), 0,
4249 channel_id, server->router,
4252 silc_free(channel_id);
4259 channel->mode = silc_channel_get_mode(entry);
4261 /* Add the client on the channel */
4262 if (!silc_server_client_on_channel(client, channel, &chl)) {
4263 chl = silc_calloc(1, sizeof(*chl));
4264 chl->client = client;
4265 chl->mode = chumodes[i++];
4266 chl->channel = channel;
4267 silc_hash_table_add(channel->user_list, chl->client, chl);
4268 silc_hash_table_add(client->channels, chl->channel, chl);
4269 channel->user_count++;
4272 chl->mode = chumodes[i++];
4275 silc_hash_table_add(ht, channel, channel);
4276 silc_free(channel_id);
4278 silc_channel_payload_list_free(ch);
4279 silc_free(chumodes);
4283 /* Go through the list again and remove client from channels that
4284 are no part of the list. */
4286 silc_hash_table_list(client->channels, &htl);
4287 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
4288 if (!silc_hash_table_find(ht, chl->channel, NULL, NULL)) {
4289 silc_hash_table_del(chl->channel->user_list, chl->client);
4290 silc_hash_table_del(chl->client->channels, chl->channel);
4294 silc_hash_table_list_reset(&htl);
4295 silc_hash_table_free(ht);
4297 silc_hash_table_list(client->channels, &htl);
4298 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
4299 silc_hash_table_del(chl->channel->user_list, chl->client);
4300 silc_hash_table_del(chl->client->channels, chl->channel);
4303 silc_hash_table_list_reset(&htl);
4307 /* Lookups route to the client indicated by the `id_data'. The connection
4308 object and internal data object is returned. Returns NULL if route
4309 could not be found to the client. If the `client_id' is specified then
4310 it is used and the `id_data' is ignored. */
4312 SilcSocketConnection
4313 silc_server_get_client_route(SilcServer server,
4314 unsigned char *id_data,
4316 SilcClientID *client_id,
4317 SilcIDListData *idata,
4318 SilcClientEntry *client_entry)
4321 SilcClientEntry client;
4323 SILC_LOG_DEBUG(("Start"));
4326 *client_entry = NULL;
4328 /* Decode destination Client ID */
4330 id = silc_id_str2id(id_data, id_len, SILC_ID_CLIENT);
4332 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
4336 id = silc_id_dup(client_id, SILC_ID_CLIENT);
4339 /* If the destination belongs to our server we don't have to route
4340 the packet anywhere but to send it to the local destination. */
4341 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
4345 /* If we are router and the client has router then the client is in
4346 our cell but not directly connected to us. */
4347 if (server->server_type == SILC_ROUTER && client->router) {
4348 /* We are of course in this case the client's router thus the route
4349 to the client is the server who owns the client. So, we will send
4350 the packet to that server. */
4352 *idata = (SilcIDListData)client->router;
4353 return client->router->connection;
4356 /* Seems that client really is directly connected to us */
4358 *idata = (SilcIDListData)client;
4360 *client_entry = client;
4361 return client->connection;
4364 /* Destination belongs to someone not in this server. If we are normal
4365 server our action is to send the packet to our router. */
4366 if (server->server_type != SILC_ROUTER && !server->standalone) {
4369 *idata = (SilcIDListData)server->router;
4370 return server->router->connection;
4373 /* We are router and we will perform route lookup for the destination
4374 and send the packet to fastest route. */
4375 if (server->server_type == SILC_ROUTER && !server->standalone) {
4376 /* Check first that the ID is valid */
4377 client = silc_idlist_find_client_by_id(server->global_list, id,
4380 SilcSocketConnection dst_sock;
4382 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
4386 *idata = (SilcIDListData)dst_sock->user_data;
4395 /* Encodes and returns channel list of channels the `client' has joined.
4396 Secret channels are not put to the list. */
4398 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
4399 SilcClientEntry client,
4402 SilcBuffer *user_mode_list)
4404 SilcBuffer buffer = NULL;
4405 SilcChannelEntry channel;
4406 SilcChannelClientEntry chl;
4407 SilcHashTableList htl;
4410 SilcUInt16 name_len;
4414 *user_mode_list = NULL;
4416 silc_hash_table_list(client->channels, &htl);
4417 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
4418 channel = chl->channel;
4420 if (channel->mode & SILC_CHANNEL_MODE_SECRET && !get_secret)
4422 if (channel->mode & SILC_CHANNEL_MODE_PRIVATE && !get_private)
4425 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
4426 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
4427 name_len = strlen(channel->channel_name);
4429 len = 4 + name_len + id_len + 4;
4430 buffer = silc_buffer_realloc(buffer,
4431 (buffer ? buffer->truelen + len : len));
4432 silc_buffer_pull_tail(buffer, (buffer->end - buffer->data));
4433 silc_buffer_format(buffer,
4434 SILC_STR_UI_SHORT(name_len),
4435 SILC_STR_UI_XNSTRING(channel->channel_name,
4437 SILC_STR_UI_SHORT(id_len),
4438 SILC_STR_UI_XNSTRING(cid, id_len),
4439 SILC_STR_UI_INT(chl->channel->mode),
4441 silc_buffer_pull(buffer, len);
4444 if (user_mode_list) {
4445 *user_mode_list = silc_buffer_realloc(*user_mode_list,
4447 (*user_mode_list)->truelen + 4 :
4449 silc_buffer_pull_tail(*user_mode_list, ((*user_mode_list)->end -
4450 (*user_mode_list)->data));
4451 SILC_PUT32_MSB(chl->mode, (*user_mode_list)->data);
4452 silc_buffer_pull(*user_mode_list, 4);
4455 silc_hash_table_list_reset(&htl);
4458 silc_buffer_push(buffer, buffer->data - buffer->head);
4459 if (user_mode_list && *user_mode_list)
4460 silc_buffer_push(*user_mode_list, ((*user_mode_list)->data -
4461 (*user_mode_list)->head));
4466 /* Finds client entry by Client ID and if it is not found then resolves
4467 it using WHOIS command. */
4469 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
4470 SilcClientID *client_id,
4471 bool always_resolve,
4474 SilcClientEntry client;
4479 client = silc_idlist_find_client_by_id(server->local_list, client_id,
4482 client = silc_idlist_find_client_by_id(server->global_list,
4483 client_id, TRUE, NULL);
4484 if (!client && server->server_type == SILC_ROUTER)
4488 if (!client && server->standalone)
4491 if (!client || !client->nickname || !client->username ||
4493 SilcBuffer buffer, idp;
4496 client->data.status |= SILC_IDLIST_STATUS_RESOLVING;
4497 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
4498 client->resolve_cmd_ident = ++server->cmd_ident;
4501 idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
4502 buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
4503 server->cmd_ident, 1,
4504 4, idp->data, idp->len);
4505 silc_server_packet_send(server, client ? client->router->connection :
4506 server->router->connection,
4507 SILC_PACKET_COMMAND, 0,
4508 buffer->data, buffer->len, FALSE);
4509 silc_buffer_free(idp);
4510 silc_buffer_free(buffer);
4521 /* A timeout callback for the re-key. We will be the initiator of the
4524 SILC_TASK_CALLBACK(silc_server_rekey_callback)
4526 SilcSocketConnection sock = (SilcSocketConnection)context;
4527 SilcIDListData idata = (SilcIDListData)sock->user_data;
4528 SilcServer server = (SilcServer)idata->rekey->context;
4529 SilcProtocol protocol;
4530 SilcServerRekeyInternalContext *proto_ctx;
4532 SILC_LOG_DEBUG(("Start"));
4534 /* Allocate internal protocol context. This is sent as context
4536 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
4537 proto_ctx->server = (void *)server;
4538 proto_ctx->sock = sock;
4539 proto_ctx->responder = FALSE;
4540 proto_ctx->pfs = idata->rekey->pfs;
4542 /* Perform rekey protocol. Will call the final callback after the
4543 protocol is over. */
4544 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
4545 &protocol, proto_ctx, silc_server_rekey_final);
4546 sock->protocol = protocol;
4548 /* Run the protocol */
4549 silc_protocol_execute(protocol, server->schedule, 0, 0);
4551 /* Re-register re-key timeout */
4552 silc_schedule_task_add(server->schedule, sock->sock,
4553 silc_server_rekey_callback,
4554 context, idata->rekey->timeout, 0,
4555 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
4558 /* The final callback for the REKEY protocol. This will actually take the
4559 new key material into use. */
4561 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final)
4563 SilcProtocol protocol = (SilcProtocol)context;
4564 SilcServerRekeyInternalContext *ctx =
4565 (SilcServerRekeyInternalContext *)protocol->context;
4566 SilcServer server = (SilcServer)ctx->server;
4567 SilcSocketConnection sock = ctx->sock;
4569 SILC_LOG_DEBUG(("Start"));
4571 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
4572 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
4573 /* Error occured during protocol */
4574 SILC_LOG_ERROR(("Error occurred during rekey protocol"));
4575 silc_protocol_cancel(protocol, server->schedule);
4576 silc_protocol_free(protocol);
4577 sock->protocol = NULL;
4579 silc_packet_context_free(ctx->packet);
4581 silc_ske_free(ctx->ske);
4586 /* Purge the outgoing data queue to assure that all rekey packets really
4587 go to the network before we quit the protocol. */
4588 silc_server_packet_queue_purge(server, sock);
4591 silc_protocol_free(protocol);
4592 sock->protocol = NULL;
4594 silc_packet_context_free(ctx->packet);
4596 silc_ske_free(ctx->ske);
4600 /* Task callback used to retrieve network statistical information from
4601 router server once in a while. */
4603 SILC_TASK_CALLBACK(silc_server_get_stats)
4605 SilcServer server = (SilcServer)context;
4606 SilcBuffer idp, packet;
4608 SILC_LOG_DEBUG(("Retrieving stats from router"));
4610 if (!server->standalone) {
4611 idp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
4612 packet = silc_command_payload_encode_va(SILC_COMMAND_STATS,
4613 ++server->cmd_ident, 1,
4614 1, idp->data, idp->len);
4615 silc_server_packet_send(server, server->router->connection,
4616 SILC_PACKET_COMMAND, 0, packet->data,
4617 packet->len, FALSE);
4618 silc_buffer_free(packet);
4619 silc_buffer_free(idp);
4622 silc_schedule_task_add(server->schedule, 0, silc_server_get_stats,
4623 server, 120, 0, SILC_TASK_TIMEOUT,