5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; 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_router);
32 SILC_TASK_CALLBACK(silc_server_connect_to_router);
33 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
34 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
35 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
36 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
37 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
38 SILC_TASK_CALLBACK(silc_server_packet_process);
39 SILC_TASK_CALLBACK(silc_server_packet_parse_real);
40 SILC_TASK_CALLBACK(silc_server_timeout_remote);
41 SILC_TASK_CALLBACK(silc_server_failure_callback);
42 SILC_TASK_CALLBACK(silc_server_rekey_callback);
44 /* Allocates a new SILC server object. This has to be done before the server
45 can be used. After allocation one must call silc_server_init to initialize
46 the server. The new allocated server object is returned to the new_server
49 int silc_server_alloc(SilcServer *new_server)
53 SILC_LOG_DEBUG(("Allocating new server object"));
55 server = silc_calloc(1, sizeof(*server));
56 server->server_type = SILC_SERVER;
57 server->standalone = TRUE;
58 server->local_list = silc_calloc(1, sizeof(*server->local_list));
59 server->global_list = silc_calloc(1, sizeof(*server->global_list));
60 server->pending_commands = silc_dlist_init();
62 server->sim = silc_dlist_init();
70 /* Free's the SILC server object. This is called at the very end before
73 void silc_server_free(SilcServer server)
80 if (server->local_list)
81 silc_free(server->local_list);
82 if (server->global_list)
83 silc_free(server->global_list);
85 silc_rng_free(server->rng);
88 while ((sim = silc_dlist_get(server->sim)) != SILC_LIST_END) {
89 silc_dlist_del(server->sim, sim);
92 silc_dlist_uninit(server->sim);
96 silc_free(server->params);
98 if (server->pending_commands)
99 silc_dlist_uninit(server->pending_commands);
105 /* Initializes the entire SILC server. This is called always before running
106 the server. This is called only once at the initialization of the program.
107 This binds the server to its listenning port. After this function returns
108 one should call silc_server_run to start the server. This returns TRUE
109 when everything is ok to run the server. Configuration file must be
110 read and parsed before calling this. */
112 int silc_server_init(SilcServer server)
114 int *sock = NULL, sock_count = 0, i;
116 SilcServerEntry id_entry;
117 SilcIDListPurge purge;
119 SILC_LOG_DEBUG(("Initializing server"));
121 assert(server->config);
123 /* Set public and private keys */
124 if (!server->config->server_keys ||
125 !server->config->server_keys->public_key ||
126 !server->config->server_keys->private_key) {
127 SILC_LOG_ERROR(("Server public key and/or private key does not exist"));
130 server->public_key = server->config->server_keys->public_key;
131 server->private_key = server->config->server_keys->private_key;
133 /* XXX After server is made as Silc Server Library this can be given
134 as argument, for now this is hard coded */
135 server->params = silc_calloc(1, sizeof(*server->params));
136 server->params->retry_count = SILC_SERVER_RETRY_COUNT;
137 server->params->retry_interval_min = SILC_SERVER_RETRY_INTERVAL_MIN;
138 server->params->retry_interval_max = SILC_SERVER_RETRY_INTERVAL_MAX;
139 server->params->retry_keep_trying = FALSE;
140 server->params->protocol_timeout = 60;
141 server->params->require_reverse_mapping = FALSE;
143 /* Set log files where log message should be saved. */
144 server->config->server = server;
145 silc_server_config_setlogfiles(server->config);
147 /* Register all configured ciphers, PKCS and hash functions. */
148 if (!silc_server_config_register_ciphers(server->config))
149 silc_cipher_register_default();
150 if (!silc_server_config_register_pkcs(server->config))
151 silc_pkcs_register_default();
152 if (!silc_server_config_register_hashfuncs(server->config))
153 silc_hash_register_default();
154 if (!silc_server_config_register_hmacs(server->config))
155 silc_hmac_register_default();
157 /* Initialize random number generator for the server. */
158 server->rng = silc_rng_alloc();
159 silc_rng_init(server->rng);
160 silc_rng_global_init(server->rng);
162 /* Initialize hash functions for server to use */
163 silc_hash_alloc("md5", &server->md5hash);
164 silc_hash_alloc("sha1", &server->sha1hash);
166 /* Initialize none cipher */
167 silc_cipher_alloc("none", &server->none_cipher);
169 /* Create a listening server. Note that our server can listen on
170 multiple ports. All listeners are created here and now. */
171 /* XXX Still check this whether to use server_info or listen_port. */
173 while(server->config->listen_port) {
176 tmp = silc_net_create_server(server->config->listen_port->port,
177 server->config->listen_port->host);
181 sock = silc_realloc(sock, (sizeof(int *) * (sock_count + 1)));
182 sock[sock_count] = tmp;
183 server->config->listen_port = server->config->listen_port->next;
187 /* Initialize ID caches */
188 server->local_list->clients =
189 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
190 server->local_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
191 server->local_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
193 /* These are allocated for normal server as well as these hold some
194 global information that the server has fetched from its router. For
195 router these are used as they are supposed to be used on router. */
196 server->global_list->clients =
197 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
198 server->global_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
199 server->global_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
201 /* Allocate the entire socket list that is used in server. Eventually
202 all connections will have entry in this table (it is a table of
203 pointers to the actual object that is allocated individually
205 server->sockets = silc_calloc(SILC_SERVER_MAX_CONNECTIONS,
206 sizeof(*server->sockets));
208 for (i = 0; i < sock_count; i++) {
209 SilcSocketConnection newsocket = NULL;
211 /* Set socket to non-blocking mode */
212 silc_net_set_socket_nonblock(sock[i]);
213 server->sock = sock[i];
215 /* Create a Server ID for the server. */
216 silc_id_create_server_id(sock[i], server->rng, &id);
222 server->id_string = silc_id_id2str(id, SILC_ID_SERVER);
223 server->id_string_len = silc_id_get_len(id, SILC_ID_SERVER);
224 server->id_type = SILC_ID_SERVER;
225 server->server_name = server->config->server_info->server_name;
227 /* Add ourselves to the server list. We don't have a router yet
228 beacuse we haven't established a route yet. It will be done later.
229 For now, NULL is sent as router. This allocates new entry to
232 silc_idlist_add_server(server->local_list,
233 server->config->server_info->server_name,
234 server->server_type, server->id, NULL, NULL);
236 SILC_LOG_ERROR(("Could not add ourselves to cache"));
240 /* Add ourselves also to the socket table. The entry allocated above
241 is sent as argument for fast referencing in the future. */
242 silc_socket_alloc(sock[i], SILC_SOCKET_TYPE_SERVER, id_entry,
245 server->sockets[sock[i]] = newsocket;
247 /* Perform name and address lookups to resolve the listenning address
249 if (!silc_net_check_local_by_sock(sock[i], &newsocket->hostname,
251 if ((server->params->require_reverse_mapping && !newsocket->hostname) ||
253 SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
254 newsocket->hostname ? newsocket->hostname :
255 newsocket->ip ? newsocket->ip : ""));
256 server->stat.conn_failures++;
259 if (!newsocket->hostname)
260 newsocket->hostname = strdup(newsocket->ip);
262 newsocket->port = silc_net_get_local_port(sock[i]);
264 /* Put the allocated socket pointer also to the entry allocated above
265 for fast back-referencing to the socket list. */
266 id_entry->connection = (void *)server->sockets[sock[i]];
267 server->id_entry = id_entry;
270 /* Register protocols */
271 silc_server_protocols_register();
273 /* Initialize the scheduler. This will register the task queues as well.
274 In SILC we have by default three task queues. One task queue for
275 non-timeout tasks which perform different kind of I/O on file
276 descriptors, timeout task queue for timeout tasks, and, generic
277 non-timeout task queue whose tasks apply to all connections. */
278 server->schedule = silc_schedule_init(&server->io_queue,
279 &server->timeout_queue,
280 &server->generic_queue,
281 SILC_SERVER_MAX_CONNECTIONS);
282 if (!server->schedule)
285 /* Add the first task to the queue. This is task that is executed by
286 timeout. It expires as soon as the caller calls silc_server_run. This
287 task performs authentication protocol and key exchange with our
289 silc_task_register(server->timeout_queue, sock[0],
290 silc_server_connect_to_router,
291 (void *)server, 0, 1,
293 SILC_TASK_PRI_NORMAL);
295 /* Add listener task to the queue. This task receives new connections to the
296 server. This task remains on the queue until the end of the program. */
297 silc_task_register(server->io_queue, sock[0],
298 silc_server_accept_new_connection,
299 (void *)server, 0, 0,
301 SILC_TASK_PRI_NORMAL);
302 server->listenning = TRUE;
304 /* If server connections has been configured then we must be router as
305 normal server cannot have server connections, only router connections. */
306 if (server->config->servers)
307 server->server_type = SILC_ROUTER;
309 /* Register the ID Cache purge task. This periodically purges the ID cache
310 and removes the expired cache entries. */
312 /* Clients local list */
313 purge = silc_calloc(1, sizeof(*purge));
314 purge->cache = server->local_list->clients;
315 purge->timeout_queue = server->timeout_queue;
316 silc_task_register(purge->timeout_queue, 0,
318 (void *)purge, 600, 0,
319 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
321 /* Clients global list */
322 purge = silc_calloc(1, sizeof(*purge));
323 purge->cache = server->global_list->clients;
324 purge->timeout_queue = server->timeout_queue;
325 silc_task_register(purge->timeout_queue, 0,
327 (void *)purge, 300, 0,
328 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
330 SILC_LOG_DEBUG(("Server initialized"));
332 /* We are done here, return succesfully */
336 for (i = 0; i < sock_count; i++)
337 silc_net_close_server(sock[i]);
342 /* Fork server to background and set gid+uid to non-root.
343 Silcd will not run as root, so trying to set either user or group to
344 root will cause silcd to exit. */
346 void silc_server_daemonise(SilcServer server)
348 /* Are we executing silcd as root or a regular user? */
355 if (!server->config->identity || !server->config->identity->user ||
356 !server->config->identity->group) {
357 fprintf(stderr, "Error:"
358 "\tSILC server must not be run as root. For the security of your\n"
359 "\tsystem it is strongly suggested that you run SILC under dedicated\n"
360 "\tuser account. Modify the [Identity] configuration section to run\n"
361 "\tthe server as non-root user.\n");
365 /* Get the values given for user and group in configuration file */
366 user=server->config->identity->user;
367 group=server->config->identity->group;
369 /* Check whether the user/group information is text */
370 if (atoi(user)!=0 || atoi(group)!=0) {
371 SILC_LOG_DEBUG(("Invalid user and/or group information"));
372 SILC_LOG_DEBUG(("User and/or group given as number"));
373 fprintf(stderr, "Invalid user and/or group information\n");
374 fprintf(stderr, "Please assign them as names, not numbers\n");
378 /* Catch the nasty incident of string "0" returning 0 from atoi */
379 if (strcmp("0", user)==0 || strcmp("0", group)==0) {
380 SILC_LOG_DEBUG(("User and/or group configured to 0. Unacceptable"));
381 fprintf(stderr, "User and/or group configured to 0. Exiting\n");
389 fprintf(stderr, "No such user %s found\n", user);
394 fprintf(stderr, "No such group %s found\n", group);
398 /* Check whether user and/or group is set to root. If yes, exit
399 immediately. Otherwise, setgid and setuid server to user.group */
400 if (gr->gr_gid==0 || pw->pw_uid==0) {
401 fprintf(stderr, "Error:"
402 "\tSILC server must not be run as root. For the security of your\n"
403 "\tsystem it is strongly suggested that you run SILC under dedicated\n"
404 "\tuser account. Modify the [Identity] configuration section to run\n"
405 "\tthe server as non-root user.\n");
408 /* Fork server to background, making it a daemon */
410 SILC_LOG_DEBUG(("Server started as root. Dropping privileges."));
411 SILC_LOG_DEBUG(("Forking SILC server to background"));
416 SILC_LOG_DEBUG(("Changing to group %s", group));
417 if(setgid(gr->gr_gid)==0) {
418 SILC_LOG_DEBUG(("Setgid to %s", group));
420 SILC_LOG_DEBUG(("Setgid to %s failed", group));
421 fprintf(stderr, "Tried to setgid %s but no such group. Exiting\n",
425 SILC_LOG_DEBUG(("Changing to user nobody"));
426 if(setuid(pw->pw_uid)==0) {
427 SILC_LOG_DEBUG(("Setuid to %s", user));
429 SILC_LOG_DEBUG(("Setuid to %s failed", user));
430 fprintf(stderr, "Tried to setuid %s but no such user. Exiting\n",
436 /* Fork server to background, making it a daemon */
438 SILC_LOG_DEBUG(("Server started as user"));
439 SILC_LOG_DEBUG(("Forking SILC server to background"));
446 /* Stops the SILC server. This function is used to shutdown the server.
447 This is usually called after the scheduler has returned. After stopping
448 the server one should call silc_server_free. */
450 void silc_server_stop(SilcServer server)
452 SILC_LOG_DEBUG(("Stopping server"));
454 /* Stop the scheduler, although it might be already stopped. This
455 doesn't hurt anyone. This removes all the tasks and task queues,
457 silc_schedule_stop(server->schedule);
458 silc_schedule_uninit(server->schedule);
460 silc_server_protocols_unregister();
462 SILC_LOG_DEBUG(("Server stopped"));
465 /* The heart of the server. This runs the scheduler thus runs the server.
466 When this returns the server has been stopped and the program will
469 void silc_server_run(SilcServer server)
471 SILC_LOG_DEBUG(("Running server"));
473 SILC_LOG_INFO(("SILC Server started"));
475 /* Start the scheduler, the heart of the SILC server. When this returns
476 the program will be terminated. */
477 silc_schedule(server->schedule);
480 /* Timeout callback that will be called to retry connecting to remote
481 router. This is used by both normal and router server. This will wait
482 before retrying the connecting. The timeout is generated by exponential
483 backoff algorithm. */
485 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry)
487 SilcServerConnection sconn = (SilcServerConnection)context;
488 SilcServer server = sconn->server;
490 SILC_LOG_INFO(("Retrying connecting to a router"));
492 /* Calculate next timeout */
493 if (sconn->retry_count >= 1) {
494 sconn->retry_timeout = sconn->retry_timeout * SILC_SERVER_RETRY_MULTIPLIER;
495 if (sconn->retry_timeout > SILC_SERVER_RETRY_INTERVAL_MAX)
496 sconn->retry_timeout = SILC_SERVER_RETRY_INTERVAL_MAX;
498 sconn->retry_timeout = server->params->retry_interval_min;
500 sconn->retry_count++;
501 sconn->retry_timeout = sconn->retry_timeout +
502 silc_rng_get_rn32(server->rng) % SILC_SERVER_RETRY_RANDOMIZER;
504 /* If we've reached max retry count, give up. */
505 if (sconn->retry_count > server->params->retry_count &&
506 server->params->retry_keep_trying == FALSE) {
507 SILC_LOG_ERROR(("Could not connect to router, giving up"));
511 /* Wait one before retrying */
512 silc_task_register(server->timeout_queue, fd, silc_server_connect_router,
513 context, sconn->retry_timeout,
514 server->params->retry_interval_min_usec,
515 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
518 /* Generic routine to use connect to a router. */
520 SILC_TASK_CALLBACK(silc_server_connect_router)
522 SilcServerConnection sconn = (SilcServerConnection)context;
523 SilcServer server = sconn->server;
524 SilcSocketConnection newsocket;
525 SilcProtocol protocol;
526 SilcServerKEInternalContext *proto_ctx;
529 SILC_LOG_INFO(("Connecting to the router %s on port %d",
530 sconn->remote_host, sconn->remote_port));
532 /* Connect to remote host */
533 sock = silc_net_create_connection(sconn->remote_port,
536 SILC_LOG_ERROR(("Could not connect to router"));
537 silc_task_register(server->timeout_queue, fd,
538 silc_server_connect_to_router_retry,
539 context, 0, 1, SILC_TASK_TIMEOUT,
540 SILC_TASK_PRI_NORMAL);
544 /* Set socket options */
545 silc_net_set_socket_nonblock(sock);
546 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
548 /* Create socket connection for the connection. Even though we
549 know that we are connecting to a router we will mark the socket
550 to be unknown connection until we have executed authentication
552 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
553 server->sockets[sock] = newsocket;
554 newsocket->hostname = strdup(sconn->remote_host);
555 newsocket->ip = strdup(sconn->remote_host);
556 newsocket->port = sconn->remote_port;
557 sconn->sock = newsocket;
559 /* Allocate internal protocol context. This is sent as context
561 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
562 proto_ctx->server = (void *)server;
563 proto_ctx->context = (void *)sconn;
564 proto_ctx->sock = newsocket;
565 proto_ctx->rng = server->rng;
566 proto_ctx->responder = FALSE;
568 /* Perform key exchange protocol. silc_server_connect_to_router_second
569 will be called after the protocol is finished. */
570 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
571 &protocol, proto_ctx,
572 silc_server_connect_to_router_second);
573 newsocket->protocol = protocol;
575 /* Register a timeout task that will be executed if the protocol
576 is not executed within set limit. */
577 proto_ctx->timeout_task =
578 silc_task_register(server->timeout_queue, sock,
579 silc_server_timeout_remote,
580 server, server->params->protocol_timeout,
581 server->params->protocol_timeout_usec,
585 /* Register the connection for network input and output. This sets
586 that scheduler will listen for incoming packets for this connection
587 and sets that outgoing packets may be sent to this connection as
588 well. However, this doesn't set the scheduler for outgoing traffic,
589 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
590 later when outgoing data is available. */
591 context = (void *)server;
592 SILC_REGISTER_CONNECTION_FOR_IO(sock);
594 /* Run the protocol */
595 silc_protocol_execute(protocol, server->timeout_queue, 0, 0);
598 /* This function connects to our primary router or if we are a router this
599 establishes all our primary routes. This is called at the start of the
600 server to do authentication and key exchange with our router - called
603 SILC_TASK_CALLBACK(silc_server_connect_to_router)
605 SilcServer server = (SilcServer)context;
606 SilcServerConnection sconn;
608 SILC_LOG_DEBUG(("Connecting to router(s)"));
610 /* If we are normal SILC server we need to connect to our cell's
612 if (server->server_type == SILC_SERVER) {
613 SILC_LOG_DEBUG(("We are normal server"));
615 /* Create connection to the router, if configured. */
616 if (server->config->routers) {
618 /* Allocate connection object for hold connection specific stuff. */
619 sconn = silc_calloc(1, sizeof(*sconn));
620 sconn->server = server;
621 sconn->remote_host = strdup(server->config->routers->host);
622 sconn->remote_port = server->config->routers->port;
624 silc_task_register(server->timeout_queue, fd,
625 silc_server_connect_router,
626 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
627 SILC_TASK_PRI_NORMAL);
632 /* If we are a SILC router we need to establish all of our primary
634 if (server->server_type == SILC_ROUTER) {
635 SilcServerConfigSectionServerConnection *ptr;
637 SILC_LOG_DEBUG(("We are router"));
639 /* Create the connections to all our routes */
640 ptr = server->config->routers;
643 SILC_LOG_DEBUG(("Router connection [%s] %s:%d",
644 ptr->initiator ? "Initiator" : "Responder",
645 ptr->host, ptr->port));
647 if (ptr->initiator) {
648 /* Allocate connection object for hold connection specific stuff. */
649 sconn = silc_calloc(1, sizeof(*sconn));
650 sconn->server = server;
651 sconn->remote_host = strdup(ptr->host);
652 sconn->remote_port = ptr->port;
654 silc_task_register(server->timeout_queue, fd,
655 silc_server_connect_router,
656 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
657 SILC_TASK_PRI_NORMAL);
667 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
669 /* There wasn't a configured router, we will continue but we don't
670 have a connection to outside world. We will be standalone server. */
671 server->standalone = TRUE;
674 /* Second part of connecting to router(s). Key exchange protocol has been
675 executed and now we will execute authentication protocol. */
677 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
679 SilcProtocol protocol = (SilcProtocol)context;
680 SilcServerKEInternalContext *ctx =
681 (SilcServerKEInternalContext *)protocol->context;
682 SilcServer server = (SilcServer)ctx->server;
683 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
684 SilcSocketConnection sock = ctx->sock;
685 SilcServerConnAuthInternalContext *proto_ctx;
686 SilcServerConfigSectionServerConnection *conn = NULL;
688 SILC_LOG_DEBUG(("Start"));
690 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
691 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
692 /* Error occured during protocol */
693 silc_protocol_free(protocol);
694 sock->protocol = NULL;
695 silc_ske_free_key_material(ctx->keymat);
697 silc_packet_context_free(ctx->packet);
699 silc_ske_free(ctx->ske);
701 silc_free(ctx->dest_id);
703 silc_task_unregister_by_callback(server->timeout_queue,
704 silc_server_failure_callback);
705 silc_server_disconnect_remote(server, sock, "Server closed connection: "
706 "Key exchange failed");
710 /* We now have the key material as the result of the key exchange
711 protocol. Take the key material into use. Free the raw key material
712 as soon as we've set them into use. */
713 if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
714 ctx->ske->prop->cipher,
715 ctx->ske->prop->pkcs,
716 ctx->ske->prop->hash,
717 ctx->ske->prop->hmac,
718 ctx->ske->prop->group,
720 silc_protocol_free(protocol);
721 sock->protocol = NULL;
722 silc_ske_free_key_material(ctx->keymat);
724 silc_packet_context_free(ctx->packet);
726 silc_ske_free(ctx->ske);
728 silc_free(ctx->dest_id);
730 silc_task_unregister_by_callback(server->timeout_queue,
731 silc_server_failure_callback);
732 silc_server_disconnect_remote(server, sock, "Server closed connection: "
733 "Key exchange failed");
736 silc_ske_free_key_material(ctx->keymat);
738 /* Allocate internal context for the authentication protocol. This
739 is sent as context for the protocol. */
740 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
741 proto_ctx->server = (void *)server;
742 proto_ctx->context = (void *)sconn;
743 proto_ctx->sock = sock;
744 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
745 proto_ctx->dest_id_type = ctx->dest_id_type;
746 proto_ctx->dest_id = ctx->dest_id;
748 /* Resolve the authentication method used in this connection. Check if
749 we find a match from user configured connections */
750 conn = silc_server_config_find_router_conn(server->config,
754 /* Match found. Use the configured authentication method */
755 proto_ctx->auth_meth = conn->auth_meth;
756 if (conn->auth_data) {
757 proto_ctx->auth_data = strdup(conn->auth_data);
758 proto_ctx->auth_data_len = strlen(conn->auth_data);
761 SILC_LOG_ERROR(("Could not find connection data for %s (%s) on port",
762 sock->hostname, sock->ip, sock->port));
763 silc_protocol_free(protocol);
764 sock->protocol = NULL;
766 silc_packet_context_free(ctx->packet);
768 silc_ske_free(ctx->ske);
770 silc_free(ctx->dest_id);
772 silc_task_unregister_by_callback(server->timeout_queue,
773 silc_server_failure_callback);
774 silc_server_disconnect_remote(server, sock, "Server closed connection: "
775 "Key exchange failed");
779 /* Free old protocol as it is finished now */
780 silc_protocol_free(protocol);
782 silc_packet_context_free(ctx->packet);
784 sock->protocol = NULL;
786 /* Allocate the authentication protocol. This is allocated here
787 but we won't start it yet. We will be receiving party of this
788 protocol thus we will wait that connecting party will make
790 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
791 &sock->protocol, proto_ctx,
792 silc_server_connect_to_router_final);
794 /* Register timeout task. If the protocol is not executed inside
795 this timelimit the connection will be terminated. Currently
796 this is 15 seconds and is hard coded limit (XXX). */
797 proto_ctx->timeout_task =
798 silc_task_register(server->timeout_queue, sock->sock,
799 silc_server_timeout_remote,
800 (void *)server, 15, 0,
804 /* Run the protocol */
805 silc_protocol_execute(sock->protocol, server->timeout_queue, 0, 0);
808 /* Finalizes the connection to router. Registers a server task to the
809 queue so that we can accept new connections. */
811 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
813 SilcProtocol protocol = (SilcProtocol)context;
814 SilcServerConnAuthInternalContext *ctx =
815 (SilcServerConnAuthInternalContext *)protocol->context;
816 SilcServer server = (SilcServer)ctx->server;
817 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
818 SilcSocketConnection sock = ctx->sock;
819 SilcServerEntry id_entry;
821 SilcServerHBContext hb_context;
822 unsigned char *id_string;
824 SilcIDListData idata;
826 SILC_LOG_DEBUG(("Start"));
828 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
829 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
830 /* Error occured during protocol */
832 silc_free(ctx->dest_id);
833 silc_server_disconnect_remote(server, sock, "Server closed connection: "
834 "Authentication failed");
838 /* Add a task to the queue. This task receives new connections to the
839 server. This task remains on the queue until the end of the program. */
840 if (!server->listenning) {
841 silc_task_register(server->io_queue, server->sock,
842 silc_server_accept_new_connection,
843 (void *)server, 0, 0,
845 SILC_TASK_PRI_NORMAL);
846 server->listenning = TRUE;
849 /* Send NEW_SERVER packet to the router. We will become registered
850 to the SILC network after sending this packet. */
851 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
852 id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
853 packet = silc_buffer_alloc(2 + 2 + id_len + strlen(server->server_name));
854 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
855 silc_buffer_format(packet,
856 SILC_STR_UI_SHORT(id_len),
857 SILC_STR_UI_XNSTRING(id_string, id_len),
858 SILC_STR_UI_SHORT(strlen(server->server_name)),
859 SILC_STR_UI_XNSTRING(server->server_name,
860 strlen(server->server_name)),
863 /* Send the packet */
864 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
865 packet->data, packet->len, TRUE);
866 silc_buffer_free(packet);
867 silc_free(id_string);
869 SILC_LOG_DEBUG(("Connected to router %s", sock->hostname));
871 /* Add the connected router to local server list */
872 server->standalone = FALSE;
873 id_entry = silc_idlist_add_server(server->local_list, strdup(sock->hostname),
874 SILC_ROUTER, ctx->dest_id, NULL, sock);
877 silc_free(ctx->dest_id);
878 silc_server_disconnect_remote(server, sock, "Server closed connection: "
879 "Authentication failed");
883 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
884 silc_free(sock->user_data);
885 sock->user_data = (void *)id_entry;
886 sock->type = SILC_SOCKET_TYPE_ROUTER;
887 server->id_entry->router = id_entry;
888 server->router = id_entry;
889 idata = (SilcIDListData)sock->user_data;
890 idata->registered = TRUE;
892 /* Perform keepalive. The `hb_context' will be freed automatically
893 when finally calling the silc_socket_free function. XXX hardcoded
895 hb_context = silc_calloc(1, sizeof(*hb_context));
896 hb_context->server = server;
897 silc_socket_set_heartbeat(sock, 600, hb_context,
898 silc_server_perform_heartbeat,
899 server->timeout_queue);
901 /* Register re-key timeout */
902 idata->rekey->timeout = 3600; /* XXX hardcoded */
903 idata->rekey->context = (void *)server;
904 silc_task_register(server->timeout_queue, sock->sock,
905 silc_server_rekey_callback,
906 (void *)sock, idata->rekey->timeout, 0,
907 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
909 /* If we are router then announce our possible servers. */
910 if (server->server_type == SILC_ROUTER)
911 silc_server_announce_servers(server);
913 /* Announce our clients and channels to the router */
914 silc_server_announce_clients(server);
915 silc_server_announce_channels(server);
918 /* Free the temporary connection data context */
920 silc_free(sconn->remote_host);
924 /* Free the protocol object */
925 silc_protocol_free(protocol);
927 silc_packet_context_free(ctx->packet);
929 silc_ske_free(ctx->ske);
931 sock->protocol = NULL;
934 /* Accepts new connections to the server. Accepting new connections are
935 done in three parts to make it async. */
937 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
939 SilcServer server = (SilcServer)context;
940 SilcSocketConnection newsocket;
941 SilcServerKEInternalContext *proto_ctx;
943 void *cconfig, *sconfig, *rconfig;
944 SilcServerConfigSectionDenyConnection *deny;
946 SILC_LOG_DEBUG(("Accepting new connection"));
948 server->stat.conn_attempts++;
950 sock = silc_net_accept_connection(server->sock);
952 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
953 server->stat.conn_failures++;
957 /* Check max connections */
958 if (sock > SILC_SERVER_MAX_CONNECTIONS) {
959 SILC_LOG_ERROR(("Refusing connection, server is full"));
960 server->stat.conn_failures++;
964 /* Set socket options */
965 silc_net_set_socket_nonblock(sock);
966 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
968 /* We don't create a ID yet, since we don't know what type of connection
969 this is yet. But, we do add the connection to the socket table. */
970 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
971 server->sockets[sock] = newsocket;
973 /* XXX This MUST be done async as this will block the entire server
974 process. Either we have to do our own resolver stuff or in the future
975 we can use threads. */
976 /* Perform name and address lookups for the remote host. */
977 if (!silc_net_check_host_by_sock(sock, &newsocket->hostname,
979 if ((server->params->require_reverse_mapping && !newsocket->hostname) ||
981 SILC_LOG_ERROR(("IP/DNS lookup failed %s",
982 newsocket->hostname ? newsocket->hostname :
983 newsocket->ip ? newsocket->ip : ""));
984 server->stat.conn_failures++;
987 if (!newsocket->hostname)
988 newsocket->hostname = strdup(newsocket->ip);
990 newsocket->port = silc_net_get_remote_port(sock);
992 /* Register the connection for network input and output. This sets
993 that scheduler will listen for incoming packets for this connection
994 and sets that outgoing packets may be sent to this connection as well.
995 However, this doesn't set the scheduler for outgoing traffic, it
996 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
997 later when outgoing data is available. */
998 SILC_REGISTER_CONNECTION_FOR_IO(sock);
1000 SILC_LOG_INFO(("Incoming connection from %s (%s)", newsocket->hostname,
1003 port = server->sockets[fd]->port; /* Listenning port */
1005 /* Check whether this connection is denied to connect to us. */
1006 deny = silc_server_config_denied_conn(server->config, newsocket->ip, port);
1008 deny = silc_server_config_denied_conn(server->config, newsocket->hostname,
1011 /* The connection is denied */
1012 SILC_LOG_INFO(("Connection %s (%s) is denied",
1013 newsocket->hostname, newsocket->ip));
1014 silc_server_disconnect_remote(server, newsocket, deny->comment ?
1016 "Server closed connection: "
1017 "Connection refused");
1018 server->stat.conn_failures++;
1022 /* Check whether we have configred this sort of connection at all. We
1023 have to check all configurations since we don't know what type of
1024 connection this is. */
1025 if (!(cconfig = silc_server_config_find_client_conn(server->config,
1026 newsocket->ip, port)))
1027 cconfig = silc_server_config_find_client_conn(server->config,
1028 newsocket->hostname,
1030 if (!(sconfig = silc_server_config_find_server_conn(server->config,
1033 sconfig = silc_server_config_find_server_conn(server->config,
1034 newsocket->hostname,
1036 if (!(rconfig = silc_server_config_find_router_conn(server->config,
1037 newsocket->ip, port)))
1038 rconfig = silc_server_config_find_router_conn(server->config,
1039 newsocket->hostname,
1041 if (!cconfig && !sconfig && !rconfig) {
1042 silc_server_disconnect_remote(server, newsocket,
1043 "Server closed connection: "
1044 "Connection refused");
1045 server->stat.conn_failures++;
1049 /* The connection is allowed */
1051 /* Allocate internal context for key exchange protocol. This is
1052 sent as context for the protocol. */
1053 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1054 proto_ctx->server = context;
1055 proto_ctx->sock = newsocket;
1056 proto_ctx->rng = server->rng;
1057 proto_ctx->responder = TRUE;
1058 proto_ctx->cconfig = cconfig;
1059 proto_ctx->sconfig = sconfig;
1060 proto_ctx->rconfig = rconfig;
1062 /* Prepare the connection for key exchange protocol. We allocate the
1063 protocol but will not start it yet. The connector will be the
1064 initiator of the protocol thus we will wait for initiation from
1065 there before we start the protocol. */
1066 server->stat.auth_attempts++;
1067 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1068 &newsocket->protocol, proto_ctx,
1069 silc_server_accept_new_connection_second);
1071 /* Register a timeout task that will be executed if the connector
1072 will not start the key exchange protocol within 60 seconds. For
1073 now, this is a hard coded limit. After 60 secs the connection will
1074 be closed if the key exchange protocol has not been started. */
1075 proto_ctx->timeout_task =
1076 silc_task_register(server->timeout_queue, newsocket->sock,
1077 silc_server_timeout_remote,
1083 /* Second part of accepting new connection. Key exchange protocol has been
1084 performed and now it is time to do little connection authentication
1085 protocol to figure out whether this connection is client or server
1086 and whether it has right to access this server (especially server
1087 connections needs to be authenticated). */
1089 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
1091 SilcProtocol protocol = (SilcProtocol)context;
1092 SilcServerKEInternalContext *ctx =
1093 (SilcServerKEInternalContext *)protocol->context;
1094 SilcServer server = (SilcServer)ctx->server;
1095 SilcSocketConnection sock = ctx->sock;
1096 SilcServerConnAuthInternalContext *proto_ctx;
1098 SILC_LOG_DEBUG(("Start"));
1100 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1101 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1102 /* Error occured during protocol */
1103 silc_protocol_free(protocol);
1104 sock->protocol = NULL;
1105 silc_ske_free_key_material(ctx->keymat);
1107 silc_packet_context_free(ctx->packet);
1109 silc_ske_free(ctx->ske);
1111 silc_free(ctx->dest_id);
1113 silc_task_unregister_by_callback(server->timeout_queue,
1114 silc_server_failure_callback);
1115 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1116 "Key exchange failed");
1117 server->stat.auth_failures++;
1121 /* We now have the key material as the result of the key exchange
1122 protocol. Take the key material into use. Free the raw key material
1123 as soon as we've set them into use. */
1124 if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
1125 ctx->ske->prop->cipher,
1126 ctx->ske->prop->pkcs,
1127 ctx->ske->prop->hash,
1128 ctx->ske->prop->hmac,
1129 ctx->ske->prop->group,
1131 silc_protocol_free(protocol);
1132 sock->protocol = NULL;
1133 silc_ske_free_key_material(ctx->keymat);
1135 silc_packet_context_free(ctx->packet);
1137 silc_ske_free(ctx->ske);
1139 silc_free(ctx->dest_id);
1141 silc_task_unregister_by_callback(server->timeout_queue,
1142 silc_server_failure_callback);
1143 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1144 "Key exchange failed");
1145 server->stat.auth_failures++;
1148 silc_ske_free_key_material(ctx->keymat);
1150 /* Allocate internal context for the authentication protocol. This
1151 is sent as context for the protocol. */
1152 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1153 proto_ctx->server = (void *)server;
1154 proto_ctx->sock = sock;
1155 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
1156 proto_ctx->responder = TRUE;
1157 proto_ctx->dest_id_type = ctx->dest_id_type;
1158 proto_ctx->dest_id = ctx->dest_id;
1159 proto_ctx->cconfig = ctx->cconfig;
1160 proto_ctx->sconfig = ctx->sconfig;
1161 proto_ctx->rconfig = ctx->rconfig;
1163 /* Free old protocol as it is finished now */
1164 silc_protocol_free(protocol);
1166 silc_packet_context_free(ctx->packet);
1168 sock->protocol = NULL;
1170 /* Allocate the authentication protocol. This is allocated here
1171 but we won't start it yet. We will be receiving party of this
1172 protocol thus we will wait that connecting party will make
1173 their first move. */
1174 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1175 &sock->protocol, proto_ctx,
1176 silc_server_accept_new_connection_final);
1178 /* Register timeout task. If the protocol is not executed inside
1179 this timelimit the connection will be terminated. Currently
1180 this is 60 seconds and is hard coded limit (XXX). */
1181 proto_ctx->timeout_task =
1182 silc_task_register(server->timeout_queue, sock->sock,
1183 silc_server_timeout_remote,
1184 (void *)server, 60, 0,
1189 /* Final part of accepting new connection. The connection has now
1190 been authenticated and keys has been exchanged. We also know whether
1191 this is client or server connection. */
1193 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
1195 SilcProtocol protocol = (SilcProtocol)context;
1196 SilcServerConnAuthInternalContext *ctx =
1197 (SilcServerConnAuthInternalContext *)protocol->context;
1198 SilcServer server = (SilcServer)ctx->server;
1199 SilcSocketConnection sock = ctx->sock;
1200 SilcServerHBContext hb_context;
1201 void *id_entry = NULL;
1203 SILC_LOG_DEBUG(("Start"));
1205 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1206 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1207 /* Error occured during protocol */
1208 silc_protocol_free(protocol);
1209 sock->protocol = NULL;
1211 silc_packet_context_free(ctx->packet);
1213 silc_ske_free(ctx->ske);
1215 silc_free(ctx->dest_id);
1218 sock->protocol = NULL;
1219 silc_task_unregister_by_callback(server->timeout_queue,
1220 silc_server_failure_callback);
1221 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1222 "Authentication failed");
1223 server->stat.auth_failures++;
1227 switch(ctx->conn_type) {
1228 case SILC_SOCKET_TYPE_CLIENT:
1230 SilcClientEntry client;
1232 SILC_LOG_DEBUG(("Remote host is client"));
1233 SILC_LOG_INFO(("Connection from %s (%s) is client", sock->hostname,
1236 /* Add the client to the client ID cache. The nickname and Client ID
1237 and other information is created after we have received NEW_CLIENT
1238 packet from client. */
1239 client = silc_idlist_add_client(server->local_list,
1240 NULL, NULL, NULL, NULL, NULL, sock);
1242 SILC_LOG_ERROR(("Could not add new client to cache"));
1243 silc_free(sock->user_data);
1244 silc_server_disconnect_remote(server, sock,
1245 "Server closed connection: "
1246 "Authentication failed");
1247 server->stat.auth_failures++;
1252 server->stat.my_clients++;
1253 server->stat.clients++;
1254 if (server->server_type == SILC_ROUTER)
1255 server->stat.cell_clients++;
1257 id_entry = (void *)client;
1260 case SILC_SOCKET_TYPE_SERVER:
1261 case SILC_SOCKET_TYPE_ROUTER:
1263 SilcServerEntry new_server;
1264 SilcServerConfigSectionServerConnection *conn =
1265 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1266 ctx->sconfig : ctx->rconfig;
1268 SILC_LOG_DEBUG(("Remote host is %s",
1269 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1270 "server" : "router"));
1271 SILC_LOG_INFO(("Connection from %s (%s) is %s", sock->hostname,
1272 sock->ip, ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1273 "server" : "router"));
1275 /* Add the server into server cache. The server name and Server ID
1276 is updated after we have received NEW_SERVER packet from the
1277 server. We mark ourselves as router for this server if we really
1280 silc_idlist_add_server(server->local_list, NULL,
1281 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1282 SILC_SERVER : SILC_ROUTER, NULL,
1283 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1284 server->id_entry : NULL, sock);
1286 SILC_LOG_ERROR(("Could not add new server to cache"));
1287 silc_free(sock->user_data);
1288 silc_server_disconnect_remote(server, sock,
1289 "Server closed connection: "
1290 "Authentication failed");
1291 server->stat.auth_failures++;
1296 if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER)
1297 server->stat.my_servers++;
1299 server->stat.my_routers++;
1300 server->stat.servers++;
1302 id_entry = (void *)new_server;
1304 /* Check whether this connection is to be our primary router connection
1305 if we dont' already have the primary route. */
1306 if (server->standalone && ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1307 if (silc_server_config_is_primary_route(server->config) &&
1311 SILC_LOG_DEBUG(("We are not standalone server anymore"));
1312 server->standalone = FALSE;
1313 if (!server->id_entry->router) {
1314 server->id_entry->router = id_entry;
1315 server->router = id_entry;
1325 sock->type = ctx->conn_type;
1327 /* Add the common data structure to the ID entry. */
1329 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1331 /* Add to sockets internal pointer for fast referencing */
1332 silc_free(sock->user_data);
1333 sock->user_data = id_entry;
1335 /* Connection has been fully established now. Everything is ok. */
1336 SILC_LOG_DEBUG(("New connection authenticated"));
1338 /* Perform keepalive. The `hb_context' will be freed automatically
1339 when finally calling the silc_socket_free function. XXX hardcoded
1341 hb_context = silc_calloc(1, sizeof(*hb_context));
1342 hb_context->server = server;
1343 silc_socket_set_heartbeat(sock, 600, hb_context,
1344 silc_server_perform_heartbeat,
1345 server->timeout_queue);
1348 silc_task_unregister_by_callback(server->timeout_queue,
1349 silc_server_failure_callback);
1350 silc_protocol_free(protocol);
1352 silc_packet_context_free(ctx->packet);
1354 silc_ske_free(ctx->ske);
1356 silc_free(ctx->dest_id);
1358 sock->protocol = NULL;
1361 /* This function is used to read packets from network and send packets to
1362 network. This is usually a generic task. */
1364 SILC_TASK_CALLBACK(silc_server_packet_process)
1366 SilcServer server = (SilcServer)context;
1367 SilcSocketConnection sock = server->sockets[fd];
1368 SilcIDListData idata;
1369 SilcCipher cipher = NULL;
1370 SilcHmac hmac = NULL;
1376 SILC_LOG_DEBUG(("Processing packet"));
1378 /* Packet sending */
1380 if (type == SILC_TASK_WRITE) {
1381 /* Do not send data to disconnected connection */
1382 if (SILC_IS_DISCONNECTED(sock))
1385 server->stat.packets_sent++;
1387 if (sock->outbuf->data - sock->outbuf->head)
1388 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
1390 /* Send the packet */
1391 ret = silc_packet_send(sock, TRUE);
1393 /* If returned -2 could not write to connection now, will do
1401 /* The packet has been sent and now it is time to set the connection
1402 back to only for input. When there is again some outgoing data
1403 available for this connection it will be set for output as well.
1404 This call clears the output setting and sets it only for input. */
1405 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, fd);
1406 SILC_UNSET_OUTBUF_PENDING(sock);
1408 silc_buffer_clear(sock->outbuf);
1412 /* Packet receiving */
1414 /* Read some data from connection */
1415 ret = silc_packet_receive(sock);
1421 SILC_LOG_DEBUG(("Read EOF"));
1423 /* If connection is disconnecting already we will finally
1424 close the connection */
1425 if (SILC_IS_DISCONNECTING(sock)) {
1426 if (sock->user_data)
1427 silc_server_free_sock_user_data(server, sock);
1428 silc_server_close_connection(server, sock);
1432 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1433 SILC_SET_DISCONNECTING(sock);
1435 /* If the closed connection was our primary router connection the
1436 start re-connecting phase. */
1437 if (!server->standalone && sock->type == SILC_SOCKET_TYPE_ROUTER &&
1438 sock == server->router->connection)
1439 silc_task_register(server->timeout_queue, 0,
1440 silc_server_connect_to_router,
1443 SILC_TASK_PRI_NORMAL);
1445 if (sock->user_data)
1446 silc_server_free_sock_user_data(server, sock);
1447 silc_server_close_connection(server, sock);
1451 /* If connection is disconnecting or disconnected we will ignore
1453 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1454 SILC_LOG_DEBUG(("Ignoring read data from disonnected connection"));
1458 server->stat.packets_received++;
1460 /* Get keys and stuff from ID entry */
1461 idata = (SilcIDListData)sock->user_data;
1463 idata->last_receive = time(NULL);
1464 cipher = idata->receive_key;
1465 hmac = idata->hmac_receive;
1468 /* Process the packet. This will call the parser that will then
1469 decrypt and parse the packet. */
1470 silc_packet_receive_process(sock, cipher, hmac, silc_server_packet_parse,
1474 /* Callback function that the silc_packet_decrypt will call to make the
1475 decision whether the packet is normal or special packet. We will
1476 return TRUE if it is normal and FALSE if it is special */
1478 static int silc_server_packet_decrypt_check(SilcPacketType packet_type,
1480 SilcPacketContext *packet,
1483 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1484 SilcServer server = (SilcServer)parse_ctx->context;
1486 /* Packet is normal packet, if:
1488 1) packet is private message packet and does not have private key set
1489 2) is other packet than channel message packet
1490 3) is channel message packet and remote is router and we are router
1492 all other packets are special packets
1495 if (packet_type == SILC_PACKET_PRIVATE_MESSAGE &&
1496 (buffer->data[2] & SILC_PACKET_FLAG_PRIVMSG_KEY))
1499 if (packet_type != SILC_PACKET_CHANNEL_MESSAGE ||
1500 (packet_type == SILC_PACKET_CHANNEL_MESSAGE &&
1501 parse_ctx->sock->type == SILC_SOCKET_TYPE_ROUTER &&
1502 server->server_type == SILC_ROUTER))
1508 /* Parses whole packet, received earlier. */
1510 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1512 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1513 SilcServer server = (SilcServer)parse_ctx->context;
1514 SilcSocketConnection sock = parse_ctx->sock;
1515 SilcPacketContext *packet = parse_ctx->packet;
1516 SilcIDListData idata = (SilcIDListData)sock->user_data;
1519 SILC_LOG_DEBUG(("Start"));
1521 /* Decrypt the received packet */
1522 ret = silc_packet_decrypt(idata ? idata->receive_key : NULL,
1523 idata ? idata->hmac_receive : NULL,
1524 packet->buffer, packet,
1525 silc_server_packet_decrypt_check, parse_ctx);
1527 SILC_LOG_WARNING(("Packet decryption failed for connection "
1528 "%s:%d [%s]", sock->hostname, sock->port,
1529 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1530 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1531 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1537 /* Parse the packet. Packet type is returned. */
1538 ret = silc_packet_parse(packet);
1540 /* Parse the packet header in special way as this is "special"
1542 ret = silc_packet_parse_special(packet);
1545 if (ret == SILC_PACKET_NONE)
1548 /* Check that the the current client ID is same as in the client's packet. */
1549 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
1550 SilcClientEntry client = (SilcClientEntry)sock->user_data;
1551 if (client && client->id) {
1552 void *id = silc_id_str2id(packet->src_id, packet->src_id_len,
1553 packet->src_id_type);
1554 if (!id || !SILC_ID_CLIENT_COMPARE(client->id, id)) {
1562 if (server->server_type == SILC_ROUTER) {
1563 /* Route the packet if it is not destined to us. Other ID types but
1564 server are handled separately after processing them. */
1565 if (!(packet->flags & SILC_PACKET_FLAG_BROADCAST) &&
1566 packet->dst_id_type == SILC_ID_SERVER &&
1567 sock->type != SILC_SOCKET_TYPE_CLIENT &&
1568 memcmp(packet->dst_id, server->id_string, packet->dst_id_len)) {
1570 /* Route the packet to fastest route for the destination ID */
1571 void *id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1572 packet->dst_id_type);
1575 silc_server_packet_route(server,
1576 silc_server_route_get(server, id,
1577 packet->dst_id_type),
1584 /* Parse the incoming packet type */
1585 silc_server_packet_parse_type(server, sock, packet);
1587 if (server->server_type == SILC_ROUTER) {
1588 /* Broadcast packet if it is marked as broadcast packet and it is
1589 originated from router and we are router. */
1590 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1591 packet->flags & SILC_PACKET_FLAG_BROADCAST &&
1592 !server->standalone) {
1593 silc_server_packet_broadcast(server, server->router->connection, packet);
1598 /* silc_buffer_clear(sock->inbuf); */
1599 silc_packet_context_free(packet);
1600 silc_free(parse_ctx);
1603 /* Parser callback called by silc_packet_receive_process. This merely
1604 registers timeout that will handle the actual parsing when appropriate. */
1606 void silc_server_packet_parse(SilcPacketParserContext *parser_context)
1608 SilcServer server = (SilcServer)parser_context->context;
1609 SilcSocketConnection sock = parser_context->sock;
1611 switch (sock->type) {
1612 case SILC_SOCKET_TYPE_UNKNOWN:
1613 case SILC_SOCKET_TYPE_CLIENT:
1614 /* Parse the packet with timeout */
1615 silc_task_register(server->timeout_queue, sock->sock,
1616 silc_server_packet_parse_real,
1617 (void *)parser_context, 0, 100000,
1619 SILC_TASK_PRI_NORMAL);
1621 case SILC_SOCKET_TYPE_SERVER:
1622 case SILC_SOCKET_TYPE_ROUTER:
1623 /* Packets from servers are parsed as soon as possible */
1624 silc_task_register(server->timeout_queue, sock->sock,
1625 silc_server_packet_parse_real,
1626 (void *)parser_context, 0, 1,
1628 SILC_TASK_PRI_NORMAL);
1635 /* Parses the packet type and calls what ever routines the packet type
1636 requires. This is done for all incoming packets. */
1638 void silc_server_packet_parse_type(SilcServer server,
1639 SilcSocketConnection sock,
1640 SilcPacketContext *packet)
1642 SilcPacketType type = packet->type;
1644 SILC_LOG_DEBUG(("Parsing packet type %d", type));
1646 /* Parse the packet type */
1648 case SILC_PACKET_DISCONNECT:
1649 SILC_LOG_DEBUG(("Disconnect packet"));
1650 if (packet->flags & SILC_PACKET_FLAG_LIST)
1654 case SILC_PACKET_SUCCESS:
1656 * Success received for something. For now we can have only
1657 * one protocol for connection executing at once hence this
1658 * success message is for whatever protocol is executing currently.
1660 SILC_LOG_DEBUG(("Success packet"));
1661 if (packet->flags & SILC_PACKET_FLAG_LIST)
1664 silc_protocol_execute(sock->protocol, server->timeout_queue, 0, 0);
1667 case SILC_PACKET_FAILURE:
1669 * Failure received for something. For now we can have only
1670 * one protocol for connection executing at once hence this
1671 * failure message is for whatever protocol is executing currently.
1673 SILC_LOG_DEBUG(("Failure packet"));
1674 if (packet->flags & SILC_PACKET_FLAG_LIST)
1676 if (sock->protocol) {
1677 SilcServerFailureContext f;
1678 f = silc_calloc(1, sizeof(*f));
1682 /* We will wait 5 seconds to process this failure packet */
1683 silc_task_register(server->timeout_queue, sock->sock,
1684 silc_server_failure_callback, (void *)f, 5, 0,
1685 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1689 case SILC_PACKET_REJECT:
1690 SILC_LOG_DEBUG(("Reject packet"));
1691 if (packet->flags & SILC_PACKET_FLAG_LIST)
1696 case SILC_PACKET_NOTIFY:
1698 * Received notify packet. Server can receive notify packets from
1699 * router. Server then relays the notify messages to clients if needed.
1701 SILC_LOG_DEBUG(("Notify packet"));
1702 if (packet->flags & SILC_PACKET_FLAG_LIST)
1703 silc_server_notify_list(server, sock, packet);
1705 silc_server_notify(server, sock, packet);
1711 case SILC_PACKET_CHANNEL_MESSAGE:
1713 * Received channel message. Channel messages are special packets
1714 * (although probably most common ones) thus they are handled
1717 SILC_LOG_DEBUG(("Channel Message packet"));
1718 if (packet->flags & SILC_PACKET_FLAG_LIST)
1720 silc_server_channel_message(server, sock, packet);
1723 case SILC_PACKET_CHANNEL_KEY:
1725 * Received key for channel. As channels are created by the router
1726 * the keys are as well. We will distribute the key to all of our
1727 * locally connected clients on the particular channel. Router
1728 * never receives this channel and thus is ignored.
1730 SILC_LOG_DEBUG(("Channel Key packet"));
1731 if (packet->flags & SILC_PACKET_FLAG_LIST)
1733 silc_server_channel_key(server, sock, packet);
1739 case SILC_PACKET_COMMAND:
1741 * Recived command. Processes the command request and allocates the
1742 * command context and calls the command.
1744 SILC_LOG_DEBUG(("Command packet"));
1745 if (packet->flags & SILC_PACKET_FLAG_LIST)
1747 silc_server_command_process(server, sock, packet);
1750 case SILC_PACKET_COMMAND_REPLY:
1752 * Received command reply packet. Received command reply to command. It
1753 * may be reply to command sent by us or reply to command sent by client
1754 * that we've routed further.
1756 SILC_LOG_DEBUG(("Command Reply packet"));
1757 if (packet->flags & SILC_PACKET_FLAG_LIST)
1759 silc_server_command_reply(server, sock, packet);
1763 * Private Message packets
1765 case SILC_PACKET_PRIVATE_MESSAGE:
1767 * Received private message packet. The packet is coming from either
1770 SILC_LOG_DEBUG(("Private Message packet"));
1771 if (packet->flags & SILC_PACKET_FLAG_LIST)
1773 silc_server_private_message(server, sock, packet);
1776 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1778 * Private message key packet.
1780 if (packet->flags & SILC_PACKET_FLAG_LIST)
1782 silc_server_private_message_key(server, sock, packet);
1786 * Key Exchange protocol packets
1788 case SILC_PACKET_KEY_EXCHANGE:
1789 SILC_LOG_DEBUG(("KE packet"));
1790 if (packet->flags & SILC_PACKET_FLAG_LIST)
1793 if (sock->protocol && sock->protocol->protocol &&
1794 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1796 SilcServerKEInternalContext *proto_ctx =
1797 (SilcServerKEInternalContext *)sock->protocol->context;
1799 proto_ctx->packet = silc_packet_context_dup(packet);
1801 /* Let the protocol handle the packet */
1802 silc_protocol_execute(sock->protocol, server->timeout_queue, 0, 100000);
1804 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1805 "protocol active, packet dropped."));
1809 case SILC_PACKET_KEY_EXCHANGE_1:
1810 SILC_LOG_DEBUG(("KE 1 packet"));
1811 if (packet->flags & SILC_PACKET_FLAG_LIST)
1814 if (sock->protocol && sock->protocol->protocol &&
1815 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1816 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1818 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
1819 SilcServerRekeyInternalContext *proto_ctx =
1820 (SilcServerRekeyInternalContext *)sock->protocol->context;
1822 if (proto_ctx->packet)
1823 silc_packet_context_free(proto_ctx->packet);
1825 proto_ctx->packet = silc_packet_context_dup(packet);
1827 /* Let the protocol handle the packet */
1828 silc_protocol_execute(sock->protocol, server->timeout_queue, 0, 0);
1830 SilcServerKEInternalContext *proto_ctx =
1831 (SilcServerKEInternalContext *)sock->protocol->context;
1833 if (proto_ctx->packet)
1834 silc_packet_context_free(proto_ctx->packet);
1836 proto_ctx->packet = silc_packet_context_dup(packet);
1837 proto_ctx->dest_id_type = packet->src_id_type;
1838 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1839 packet->src_id_type);
1840 if (!proto_ctx->dest_id)
1843 /* Let the protocol handle the packet */
1844 silc_protocol_execute(sock->protocol, server->timeout_queue,
1848 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1849 "protocol active, packet dropped."));
1853 case SILC_PACKET_KEY_EXCHANGE_2:
1854 SILC_LOG_DEBUG(("KE 2 packet"));
1855 if (packet->flags & SILC_PACKET_FLAG_LIST)
1858 if (sock->protocol && sock->protocol->protocol &&
1859 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1860 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1862 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
1863 SilcServerRekeyInternalContext *proto_ctx =
1864 (SilcServerRekeyInternalContext *)sock->protocol->context;
1866 if (proto_ctx->packet)
1867 silc_packet_context_free(proto_ctx->packet);
1869 proto_ctx->packet = silc_packet_context_dup(packet);
1871 /* Let the protocol handle the packet */
1872 silc_protocol_execute(sock->protocol, server->timeout_queue, 0, 0);
1874 SilcServerKEInternalContext *proto_ctx =
1875 (SilcServerKEInternalContext *)sock->protocol->context;
1877 if (proto_ctx->packet)
1878 silc_packet_context_free(proto_ctx->packet);
1880 proto_ctx->packet = silc_packet_context_dup(packet);
1881 proto_ctx->dest_id_type = packet->src_id_type;
1882 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1883 packet->src_id_type);
1884 if (!proto_ctx->dest_id)
1887 /* Let the protocol handle the packet */
1888 silc_protocol_execute(sock->protocol, server->timeout_queue,
1892 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1893 "protocol active, packet dropped."));
1897 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1899 * Connection authentication request packet. When we receive this packet
1900 * we will send to the other end information about our mandatory
1901 * authentication method for the connection. This packet maybe received
1904 SILC_LOG_DEBUG(("Connection authentication request packet"));
1905 if (packet->flags & SILC_PACKET_FLAG_LIST)
1907 silc_server_connection_auth_request(server, sock, packet);
1911 * Connection Authentication protocol packets
1913 case SILC_PACKET_CONNECTION_AUTH:
1914 /* Start of the authentication protocol. We receive here the
1915 authentication data and will verify it. */
1916 SILC_LOG_DEBUG(("Connection auth packet"));
1917 if (packet->flags & SILC_PACKET_FLAG_LIST)
1920 if (sock->protocol && sock->protocol->protocol->type
1921 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
1923 SilcServerConnAuthInternalContext *proto_ctx =
1924 (SilcServerConnAuthInternalContext *)sock->protocol->context;
1926 proto_ctx->packet = silc_packet_context_dup(packet);
1928 /* Let the protocol handle the packet */
1929 silc_protocol_execute(sock->protocol, server->timeout_queue, 0, 0);
1931 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
1932 "protocol active, packet dropped."));
1936 case SILC_PACKET_NEW_ID:
1938 * Received New ID packet. This includes some new ID that has been
1939 * created. It may be for client, server or channel. This is the way
1940 * to distribute information about new registered entities in the
1943 SILC_LOG_DEBUG(("New ID packet"));
1944 if (packet->flags & SILC_PACKET_FLAG_LIST)
1945 silc_server_new_id_list(server, sock, packet);
1947 silc_server_new_id(server, sock, packet);
1950 case SILC_PACKET_NEW_CLIENT:
1952 * Received new client packet. This includes client information that
1953 * we will use to create initial client ID. After creating new
1954 * ID we will send it to the client.
1956 SILC_LOG_DEBUG(("New Client packet"));
1957 if (packet->flags & SILC_PACKET_FLAG_LIST)
1959 silc_server_new_client(server, sock, packet);
1962 case SILC_PACKET_NEW_SERVER:
1964 * Received new server packet. This includes Server ID and some other
1965 * information that we may save. This is received after server has
1968 SILC_LOG_DEBUG(("New Server packet"));
1969 if (packet->flags & SILC_PACKET_FLAG_LIST)
1971 silc_server_new_server(server, sock, packet);
1974 case SILC_PACKET_NEW_CHANNEL:
1976 * Received new channel packet. Information about new channel in the
1977 * network are distributed using this packet.
1979 SILC_LOG_DEBUG(("New Channel packet"));
1980 if (packet->flags & SILC_PACKET_FLAG_LIST)
1981 silc_server_new_channel_list(server, sock, packet);
1983 silc_server_new_channel(server, sock, packet);
1986 case SILC_PACKET_HEARTBEAT:
1988 * Received heartbeat.
1990 SILC_LOG_DEBUG(("Heartbeat packet"));
1991 if (packet->flags & SILC_PACKET_FLAG_LIST)
1995 case SILC_PACKET_KEY_AGREEMENT:
1997 * Received heartbeat.
1999 SILC_LOG_DEBUG(("Key agreement packet"));
2000 if (packet->flags & SILC_PACKET_FLAG_LIST)
2002 silc_server_key_agreement(server, sock, packet);
2005 case SILC_PACKET_REKEY:
2007 * Received re-key packet. The sender wants to regenerate the session
2010 SILC_LOG_DEBUG(("Re-key packet"));
2011 if (packet->flags & SILC_PACKET_FLAG_LIST)
2013 silc_server_rekey(server, sock, packet);
2016 case SILC_PACKET_REKEY_DONE:
2018 * The re-key is done.
2020 SILC_LOG_DEBUG(("Re-key done packet"));
2021 if (packet->flags & SILC_PACKET_FLAG_LIST)
2024 if (sock->protocol && sock->protocol->protocol &&
2025 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2027 SilcServerRekeyInternalContext *proto_ctx =
2028 (SilcServerRekeyInternalContext *)sock->protocol->context;
2030 if (proto_ctx->packet)
2031 silc_packet_context_free(proto_ctx->packet);
2033 proto_ctx->packet = silc_packet_context_dup(packet);
2035 /* Let the protocol handle the packet */
2036 silc_protocol_execute(sock->protocol, server->timeout_queue, 0, 0);
2038 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
2039 "protocol active, packet dropped."));
2044 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
2050 /* Creates connection to a remote router. */
2052 void silc_server_create_connection(SilcServer server,
2053 char *remote_host, uint32 port)
2055 SilcServerConnection sconn;
2057 /* Allocate connection object for hold connection specific stuff. */
2058 sconn = silc_calloc(1, sizeof(*sconn));
2059 sconn->server = server;
2060 sconn->remote_host = strdup(remote_host);
2061 sconn->remote_port = port;
2063 silc_task_register(server->timeout_queue, 0,
2064 silc_server_connect_router,
2065 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
2066 SILC_TASK_PRI_NORMAL);
2069 SILC_TASK_CALLBACK(silc_server_close_connection_final)
2071 silc_socket_free((SilcSocketConnection)context);
2074 /* Closes connection to socket connection */
2076 void silc_server_close_connection(SilcServer server,
2077 SilcSocketConnection sock)
2079 SILC_LOG_INFO(("Closing connection %s:%d [%s] (%d)", sock->hostname,
2081 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2082 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2083 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2084 "Router"), sock->sock));
2086 /* If any protocol is active cancel its execution */
2087 if (sock->protocol) {
2088 silc_protocol_cancel(sock->protocol, server->timeout_queue);
2089 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2090 silc_protocol_execute_final(sock->protocol, server->timeout_queue);
2091 sock->protocol = NULL;
2094 /* We won't listen for this connection anymore */
2095 silc_schedule_unset_listen_fd(server->schedule, sock->sock);
2097 /* Unregister all tasks */
2098 silc_task_unregister_by_fd(server->io_queue, sock->sock);
2099 silc_task_unregister_by_fd(server->timeout_queue, sock->sock);
2101 /* Close the actual connection */
2102 silc_net_close_connection(sock->sock);
2103 server->sockets[sock->sock] = NULL;
2105 silc_task_register(server->timeout_queue, 0,
2106 silc_server_close_connection_final,
2107 (void *)sock, 0, 1, SILC_TASK_TIMEOUT,
2108 SILC_TASK_PRI_NORMAL);
2111 /* Sends disconnect message to remote connection and disconnects the
2114 void silc_server_disconnect_remote(SilcServer server,
2115 SilcSocketConnection sock,
2116 const char *fmt, ...)
2119 unsigned char buf[4096];
2124 memset(buf, 0, sizeof(buf));
2126 vsprintf(buf, fmt, ap);
2129 SILC_LOG_DEBUG(("Disconnecting remote host"));
2131 SILC_LOG_INFO(("Disconnecting %s:%d [%s]", sock->hostname,
2133 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2134 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2135 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2138 /* Notify remote end that the conversation is over. The notify message
2139 is tried to be sent immediately. */
2140 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2141 buf, strlen(buf), TRUE);
2143 /* Mark the connection to be disconnected */
2144 SILC_SET_DISCONNECTED(sock);
2145 silc_server_close_connection(server, sock);
2150 SilcClientEntry client;
2151 } *FreeClientInternal;
2153 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout)
2155 FreeClientInternal i = (FreeClientInternal)context;
2157 silc_idlist_del_data(i->client);
2158 silc_idcache_purge_by_context(i->server->local_list->clients, i->client);
2162 /* Frees client data and notifies about client's signoff. */
2164 void silc_server_free_client_data(SilcServer server,
2165 SilcSocketConnection sock,
2166 SilcClientEntry client,
2170 FreeClientInternal i = silc_calloc(1, sizeof(*i));
2172 /* If there is pending outgoing data for the client then purge it
2173 to the network before removing the client entry. */
2174 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
2175 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
2176 server->stat.packets_sent++;
2178 if (sock->outbuf->data - sock->outbuf->head)
2179 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
2181 silc_packet_send(sock, TRUE);
2183 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
2184 SILC_UNSET_OUTBUF_PENDING(sock);
2185 silc_buffer_clear(sock->outbuf);
2188 /* Send SIGNOFF notify to routers. */
2189 if (notify && !server->standalone && server->router)
2190 silc_server_send_notify_signoff(server, server->router->connection,
2191 server->server_type == SILC_SERVER ?
2192 FALSE : TRUE, client->id, signoff);
2194 /* Remove client from all channels */
2196 silc_server_remove_from_channels(server, NULL, client,
2197 TRUE, signoff, TRUE);
2199 silc_server_remove_from_channels(server, NULL, client,
2200 FALSE, NULL, FALSE);
2202 /* We will not delete the client entry right away. We will take it
2203 into history (for WHOWAS command) for 5 minutes */
2206 silc_task_register(server->timeout_queue, 0,
2207 silc_server_free_client_data_timeout,
2209 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2210 client->data.registered = FALSE;
2212 /* Free the client entry and everything in it */
2213 server->stat.my_clients--;
2214 server->stat.clients--;
2215 if (server->server_type == SILC_ROUTER)
2216 server->stat.cell_clients--;
2219 /* Frees user_data pointer from socket connection object. This also sends
2220 appropriate notify packets to the network to inform about leaving
2223 void silc_server_free_sock_user_data(SilcServer server,
2224 SilcSocketConnection sock)
2226 SILC_LOG_DEBUG(("Start"));
2228 switch(sock->type) {
2229 case SILC_SOCKET_TYPE_CLIENT:
2231 SilcClientEntry user_data = (SilcClientEntry)sock->user_data;
2232 silc_server_free_client_data(server, sock, user_data, TRUE, NULL);
2235 case SILC_SOCKET_TYPE_SERVER:
2236 case SILC_SOCKET_TYPE_ROUTER:
2238 SilcServerEntry user_data = (SilcServerEntry)sock->user_data;
2240 /* Free all client entries that this server owns as they will
2241 become invalid now as well. */
2243 silc_server_remove_clients_by_server(server, user_data, TRUE);
2245 /* If this was our primary router connection then we're lost to
2246 the outside world. */
2247 if (server->router == user_data) {
2248 server->id_entry->router = NULL;
2249 server->router = NULL;
2250 server->standalone = TRUE;
2253 /* Free the server entry */
2254 silc_idlist_del_data(user_data);
2255 silc_idlist_del_server(server->local_list, user_data);
2256 server->stat.my_servers--;
2257 server->stat.servers--;
2258 if (server->server_type == SILC_ROUTER)
2259 server->stat.cell_servers--;
2264 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2266 silc_idlist_del_data(user_data);
2267 silc_free(user_data);
2272 sock->user_data = NULL;
2275 /* This function is used to remove all client entries by the server `entry'.
2276 This is called when the connection is lost to the server. In this case
2277 we must invalidate all the client entries owned by the server `entry'.
2278 If the `server_signoff' is TRUE then the SERVER_SIGNOFF notify is
2279 distributed to our local clients. */
2281 int silc_server_remove_clients_by_server(SilcServer server,
2282 SilcServerEntry entry,
2285 SilcIDCacheList list = NULL;
2286 SilcIDCacheEntry id_cache = NULL;
2287 SilcClientEntry client = NULL;
2289 SilcClientEntry *clients = NULL;
2290 uint32 clients_c = 0;
2291 unsigned char **argv = NULL;
2292 uint32 *argv_lens = NULL, *argv_types = NULL, argc = 0;
2295 SILC_LOG_DEBUG(("Start"));
2297 if (server_signoff) {
2298 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2299 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
2300 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) * (argc + 1));
2301 argv_types = silc_realloc(argv_types, sizeof(*argv_types) * (argc + 1));
2302 argv[argc] = idp->data;
2303 argv_lens[argc] = idp->len;
2304 argv_types[argc] = argc + 1;
2306 silc_buffer_free(idp);
2309 if (silc_idcache_get_all(server->local_list->clients, &list)) {
2311 if (silc_idcache_list_first(list, &id_cache)) {
2313 client = (SilcClientEntry)id_cache->context;
2314 if (client->data.registered == FALSE) {
2315 if (!silc_idcache_list_next(list, &id_cache))
2321 if (client->router != entry) {
2322 if (server_signoff && client->connection) {
2323 clients = silc_realloc(clients,
2324 sizeof(*clients) * (clients_c + 1));
2325 clients[clients_c] = client;
2329 if (!silc_idcache_list_next(list, &id_cache))
2335 if (server_signoff) {
2336 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2337 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
2338 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) *
2340 argv_types = silc_realloc(argv_types, sizeof(*argv_types) *
2342 argv[argc] = silc_calloc(idp->len, sizeof(*argv[0]));
2343 memcpy(argv[argc], idp->data, idp->len);
2344 argv_lens[argc] = idp->len;
2345 argv_types[argc] = argc + 1;
2347 silc_buffer_free(idp);
2350 /* Remove the client entry */
2351 silc_server_remove_from_channels(server, NULL, client, FALSE,
2353 silc_idlist_del_client(server->local_list, client);
2355 if (!silc_idcache_list_next(list, &id_cache))
2359 silc_idcache_list_free(list);
2362 if (silc_idcache_get_all(server->global_list->clients, &list)) {
2364 if (silc_idcache_list_first(list, &id_cache)) {
2366 client = (SilcClientEntry)id_cache->context;
2367 if (client->data.registered == FALSE) {
2368 if (!silc_idcache_list_next(list, &id_cache))
2374 if (client->router != entry) {
2375 if (server_signoff && client->connection) {
2376 clients = silc_realloc(clients,
2377 sizeof(*clients) * (clients_c + 1));
2378 clients[clients_c] = client;
2382 if (!silc_idcache_list_next(list, &id_cache))
2388 if (server_signoff) {
2389 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2390 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
2391 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) *
2393 argv_types = silc_realloc(argv_types, sizeof(*argv_types) *
2395 argv[argc] = silc_calloc(idp->len, sizeof(*argv[0]));
2396 memcpy(argv[argc], idp->data, idp->len);
2397 argv_lens[argc] = idp->len;
2398 argv_types[argc] = argc + 1;
2400 silc_buffer_free(idp);
2403 /* Remove the client entry */
2404 silc_server_remove_from_channels(server, NULL, client, FALSE,
2406 silc_idlist_del_client(server->global_list, client);
2408 if (!silc_idcache_list_next(list, &id_cache))
2412 silc_idcache_list_free(list);
2415 /* Send the SERVER_SIGNOFF notify */
2416 if (server_signoff) {
2419 /* Send SERVER_SIGNOFF notify to our primary router */
2420 if (!server->standalone && server->router &&
2421 server->router != entry) {
2422 args = silc_argument_payload_encode(1, argv, argv_lens,
2424 silc_server_send_notify_args(server,
2425 server->router->connection,
2426 server->server_type == SILC_SERVER ?
2428 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
2430 silc_buffer_free(args);
2433 args = silc_argument_payload_encode(argc, argv, argv_lens,
2435 /* Send to local clients */
2436 for (i = 0; i < clients_c; i++) {
2437 silc_server_send_notify_args(server, clients[i]->connection,
2438 FALSE, SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
2443 silc_buffer_free(args);
2445 silc_free(argv_lens);
2446 silc_free(argv_types);
2452 /* Checks whether given channel has global users. If it does this returns
2453 TRUE and FALSE if there is only locally connected clients on the channel. */
2455 int silc_server_channel_has_global(SilcChannelEntry channel)
2457 SilcChannelClientEntry chl;
2458 SilcHashTableList htl;
2460 silc_hash_table_list(channel->user_list, &htl);
2461 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
2462 if (chl->client->router)
2469 /* Checks whether given channel has locally connected users. If it does this
2470 returns TRUE and FALSE if there is not one locally connected client. */
2472 int silc_server_channel_has_local(SilcChannelEntry channel)
2474 SilcChannelClientEntry chl;
2475 SilcHashTableList htl;
2477 silc_hash_table_list(channel->user_list, &htl);
2478 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
2479 if (!chl->client->router)
2486 /* Removes client from all channels it has joined. This is used when client
2487 connection is disconnected. If the client on a channel is last, the
2488 channel is removed as well. This sends the SIGNOFF notify types. */
2490 void silc_server_remove_from_channels(SilcServer server,
2491 SilcSocketConnection sock,
2492 SilcClientEntry client,
2494 char *signoff_message,
2497 SilcChannelEntry channel;
2498 SilcChannelClientEntry chl;
2499 SilcHashTableList htl;
2502 SILC_LOG_DEBUG(("Start"));
2504 if (!client || !client->id)
2507 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2509 /* Remove the client from all channels. The client is removed from
2510 the channels' user list. */
2511 silc_hash_table_list(client->channels, &htl);
2512 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
2513 channel = chl->channel;
2515 /* Remove channel from client's channel list */
2516 silc_hash_table_del(client->channels, channel);
2518 /* Remove channel if there is no users anymore */
2519 if (server->server_type == SILC_ROUTER &&
2520 silc_hash_table_count(channel->user_list) < 2) {
2522 silc_task_unregister_by_context(server->timeout_queue, channel->rekey);
2523 if (!silc_idlist_del_channel(server->local_list, channel))
2524 silc_idlist_del_channel(server->global_list, channel);
2525 server->stat.my_channels--;
2529 /* Remove client from channel's client list */
2530 silc_hash_table_del(channel->user_list, chl->client);
2532 server->stat.my_chanclients--;
2534 /* If there is no global users on the channel anymore mark the channel
2535 as local channel. */
2536 if (server->server_type == SILC_SERVER &&
2537 !silc_server_channel_has_global(channel))
2538 channel->global_users = FALSE;
2540 /* If there is not at least one local user on the channel then we don't
2541 need the channel entry anymore, we can remove it safely. */
2542 if (server->server_type == SILC_SERVER &&
2543 !silc_server_channel_has_local(channel)) {
2544 /* Notify about leaving client if this channel has global users. */
2545 if (notify && channel->global_users)
2546 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2547 SILC_NOTIFY_TYPE_SIGNOFF,
2548 signoff_message ? 2 : 1,
2549 clidp->data, clidp->len,
2550 signoff_message, signoff_message ?
2551 strlen(signoff_message) : 0);
2554 silc_task_unregister_by_context(server->timeout_queue, channel->rekey);
2556 if (channel->founder_key) {
2557 /* The founder auth data exists, do not remove the channel entry */
2558 SilcChannelClientEntry chl2;
2559 SilcHashTableList htl2;
2563 silc_hash_table_list(channel->user_list, &htl2);
2564 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
2565 silc_hash_table_del(chl2->client->channels, channel);
2566 silc_hash_table_del(channel->user_list, chl2->client);
2572 /* Remove the channel entry */
2573 if (!silc_idlist_del_channel(server->local_list, channel))
2574 silc_idlist_del_channel(server->global_list, channel);
2575 server->stat.my_channels--;
2579 /* Send notify to channel about client leaving SILC and thus
2580 the entire channel. */
2582 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2583 SILC_NOTIFY_TYPE_SIGNOFF,
2584 signoff_message ? 2 : 1,
2585 clidp->data, clidp->len,
2586 signoff_message, signoff_message ?
2587 strlen(signoff_message) : 0);
2589 if (keygen && !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2590 /* Re-generate channel key */
2591 silc_server_create_channel_key(server, channel, 0);
2593 /* Send the channel key to the channel. The key of course is not sent
2594 to the client who was removed from the channel. */
2595 silc_server_send_channel_key(server, client->connection, channel,
2596 server->server_type == SILC_ROUTER ?
2597 FALSE : !server->standalone);
2601 silc_buffer_free(clidp);
2604 /* Removes client from one channel. This is used for example when client
2605 calls LEAVE command to remove itself from the channel. Returns TRUE
2606 if channel still exists and FALSE if the channel is removed when
2607 last client leaves the channel. If `notify' is FALSE notify messages
2610 int silc_server_remove_from_one_channel(SilcServer server,
2611 SilcSocketConnection sock,
2612 SilcChannelEntry channel,
2613 SilcClientEntry client,
2616 SilcChannelClientEntry chl;
2619 SILC_LOG_DEBUG(("Start"));
2621 /* Get the entry to the channel, if this client is not on the channel
2623 if (!silc_hash_table_find(client->channels, channel, NULL, (void *)&chl))
2626 /* Remove the client from the channel. The client is removed from
2627 the channel's user list. */
2629 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2631 /* Remove channel from client's channel list */
2632 silc_hash_table_del(client->channels, chl->channel);
2634 /* Remove channel if there is no users anymore */
2635 if (server->server_type == SILC_ROUTER &&
2636 silc_hash_table_count(channel->user_list) < 2) {
2638 silc_task_unregister_by_context(server->timeout_queue, channel->rekey);
2639 if (!silc_idlist_del_channel(server->local_list, channel))
2640 silc_idlist_del_channel(server->global_list, channel);
2641 silc_buffer_free(clidp);
2642 server->stat.my_channels--;
2646 /* Remove client from channel's client list */
2647 silc_hash_table_del(channel->user_list, chl->client);
2649 server->stat.my_chanclients--;
2651 /* If there is no global users on the channel anymore mark the channel
2652 as local channel. */
2653 if (server->server_type == SILC_SERVER &&
2654 !silc_server_channel_has_global(channel))
2655 channel->global_users = FALSE;
2657 /* If there is not at least one local user on the channel then we don't
2658 need the channel entry anymore, we can remove it safely. */
2659 if (server->server_type == SILC_SERVER &&
2660 !silc_server_channel_has_local(channel)) {
2661 /* Notify about leaving client if this channel has global users. */
2662 if (notify && channel->global_users)
2663 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2664 SILC_NOTIFY_TYPE_LEAVE, 1,
2665 clidp->data, clidp->len);
2667 silc_buffer_free(clidp);
2670 silc_task_unregister_by_context(server->timeout_queue, channel->rekey);
2672 if (channel->founder_key) {
2673 /* The founder auth data exists, do not remove the channel entry */
2674 SilcChannelClientEntry chl2;
2675 SilcHashTableList htl2;
2679 silc_hash_table_list(channel->user_list, &htl2);
2680 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
2681 silc_hash_table_del(chl2->client->channels, channel);
2682 silc_hash_table_del(channel->user_list, chl2->client);
2688 /* Remove the channel entry */
2689 if (!silc_idlist_del_channel(server->local_list, channel))
2690 silc_idlist_del_channel(server->global_list, channel);
2691 server->stat.my_channels--;
2695 /* Send notify to channel about client leaving the channel */
2697 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2698 SILC_NOTIFY_TYPE_LEAVE, 1,
2699 clidp->data, clidp->len);
2701 silc_buffer_free(clidp);
2705 /* Returns TRUE if the given client is on the channel. FALSE if not.
2706 This works because we assure that the user list on the channel is
2707 always in up to date thus we can only check the channel list from
2708 `client' which is faster than checking the user list from `channel'. */
2710 int silc_server_client_on_channel(SilcClientEntry client,
2711 SilcChannelEntry channel)
2713 if (!client || !channel)
2716 if (silc_hash_table_find(client->channels, channel, NULL, NULL))
2722 /* Timeout callback. This is called if connection is idle or for some
2723 other reason is not responding within some period of time. This
2724 disconnects the remote end. */
2726 SILC_TASK_CALLBACK(silc_server_timeout_remote)
2728 SilcServer server = (SilcServer)context;
2729 SilcSocketConnection sock = server->sockets[fd];
2731 SILC_LOG_DEBUG(("Start"));
2736 /* If we have protocol active we must assure that we call the protocol's
2737 final callback so that all the memory is freed. */
2738 if (sock->protocol) {
2739 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2740 silc_protocol_execute_final(sock->protocol, server->timeout_queue);
2744 if (sock->user_data)
2745 silc_server_free_sock_user_data(server, sock);
2747 silc_server_disconnect_remote(server, sock, "Server closed connection: "
2748 "Connection timeout");
2751 /* Creates new channel. Sends NEW_CHANNEL packet to primary route. This
2752 function may be used only by router. In real SILC network all channels
2753 are created by routers thus this function is never used by normal
2756 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
2757 SilcServerID *router_id,
2763 SilcChannelID *channel_id;
2764 SilcChannelEntry entry;
2768 SILC_LOG_DEBUG(("Creating new channel"));
2771 cipher = "aes-256-cbc";
2773 hmac = "hmac-sha1-96";
2775 /* Allocate cipher */
2776 if (!silc_cipher_alloc(cipher, &key))
2780 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2781 silc_cipher_free(key);
2785 channel_name = strdup(channel_name);
2787 /* Create the channel */
2788 silc_id_create_channel_id(router_id, server->rng, &channel_id);
2789 entry = silc_idlist_add_channel(server->local_list, channel_name,
2790 SILC_CHANNEL_MODE_NONE, channel_id,
2791 NULL, key, newhmac);
2793 silc_free(channel_name);
2794 silc_cipher_free(key);
2795 silc_hmac_free(newhmac);
2799 entry->cipher = strdup(cipher);
2800 entry->hmac_name = strdup(hmac);
2802 /* Now create the actual key material */
2803 silc_server_create_channel_key(server, entry,
2804 silc_cipher_get_key_len(key) / 8);
2806 /* Notify other routers about the new channel. We send the packet
2807 to our primary route. */
2808 if (broadcast && server->standalone == FALSE)
2809 silc_server_send_new_channel(server, server->router->connection, TRUE,
2810 channel_name, entry->id,
2811 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
2814 server->stat.my_channels++;
2819 /* Same as above but creates the channel with Channel ID `channel_id. */
2822 silc_server_create_new_channel_with_id(SilcServer server,
2826 SilcChannelID *channel_id,
2829 SilcChannelEntry entry;
2833 SILC_LOG_DEBUG(("Creating new channel"));
2836 cipher = "aes-256-cbc";
2838 hmac = "hmac-sha1-96";
2840 /* Allocate cipher */
2841 if (!silc_cipher_alloc(cipher, &key))
2845 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2846 silc_cipher_free(key);
2850 channel_name = strdup(channel_name);
2852 /* Create the channel */
2853 entry = silc_idlist_add_channel(server->local_list, channel_name,
2854 SILC_CHANNEL_MODE_NONE, channel_id,
2855 NULL, key, newhmac);
2857 silc_free(channel_name);
2861 /* Now create the actual key material */
2862 silc_server_create_channel_key(server, entry,
2863 silc_cipher_get_key_len(key) / 8);
2865 /* Notify other routers about the new channel. We send the packet
2866 to our primary route. */
2867 if (broadcast && server->standalone == FALSE)
2868 silc_server_send_new_channel(server, server->router->connection, TRUE,
2869 channel_name, entry->id,
2870 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
2873 server->stat.my_channels++;
2878 /* Channel's key re-key timeout callback. */
2880 SILC_TASK_CALLBACK(silc_server_channel_key_rekey)
2882 SilcServerChannelRekey rekey = (SilcServerChannelRekey)context;
2883 SilcServer server = (SilcServer)rekey->context;
2885 silc_server_create_channel_key(server, rekey->channel, rekey->key_len);
2886 silc_server_send_channel_key(server, NULL, rekey->channel, FALSE);
2888 silc_task_register(server->timeout_queue, 0,
2889 silc_server_channel_key_rekey,
2890 (void *)rekey, 3600, 0,
2892 SILC_TASK_PRI_NORMAL);
2895 /* Generates new channel key. This is used to create the initial channel key
2896 but also to re-generate new key for channel. If `key_len' is provided
2897 it is the bytes of the key length. */
2899 void silc_server_create_channel_key(SilcServer server,
2900 SilcChannelEntry channel,
2904 unsigned char channel_key[32], hash[32];
2907 SILC_LOG_DEBUG(("Generating channel key"));
2909 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2910 SILC_LOG_DEBUG(("Channel has private keys, will not generate new key"));
2914 if (!channel->channel_key)
2915 if (!silc_cipher_alloc("aes-256-cbc", &channel->channel_key))
2920 else if (channel->key_len)
2921 len = channel->key_len / 8;
2923 len = silc_cipher_get_key_len(channel->channel_key) / 8;
2925 /* Create channel key */
2926 for (i = 0; i < len; i++) channel_key[i] = silc_rng_get_byte(server->rng);
2929 silc_cipher_set_key(channel->channel_key, channel_key, len * 8);
2931 /* Remove old key if exists */
2933 memset(channel->key, 0, channel->key_len / 8);
2934 silc_free(channel->key);
2938 channel->key_len = len * 8;
2939 channel->key = silc_calloc(len, sizeof(*channel->key));
2940 memcpy(channel->key, channel_key, len);
2941 memset(channel_key, 0, sizeof(channel_key));
2943 /* Generate HMAC key from the channel key data and set it */
2945 silc_hmac_alloc("hmac-sha1-96", NULL, &channel->hmac);
2946 silc_hash_make(channel->hmac->hash, channel->key, len, hash);
2947 silc_hmac_set_key(channel->hmac, hash, silc_hash_len(channel->hmac->hash));
2948 memset(hash, 0, sizeof(hash));
2950 if (server->server_type == SILC_ROUTER) {
2951 if (!channel->rekey)
2952 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
2953 channel->rekey->context = (void *)server;
2954 channel->rekey->channel = channel;
2955 channel->rekey->key_len = key_len;
2957 silc_task_unregister_by_callback(server->timeout_queue,
2958 silc_server_channel_key_rekey);
2959 silc_task_register(server->timeout_queue, 0,
2960 silc_server_channel_key_rekey,
2961 (void *)channel->rekey, 3600, 0,
2963 SILC_TASK_PRI_NORMAL);
2967 /* Saves the channel key found in the encoded `key_payload' buffer. This
2968 function is used when we receive Channel Key Payload and also when we're
2969 processing JOIN command reply. Returns entry to the channel. */
2971 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
2972 SilcBuffer key_payload,
2973 SilcChannelEntry channel)
2975 SilcChannelKeyPayload payload = NULL;
2976 SilcChannelID *id = NULL;
2977 unsigned char *tmp, hash[32];
2981 SILC_LOG_DEBUG(("Start"));
2983 /* Decode channel key payload */
2984 payload = silc_channel_key_payload_parse(key_payload);
2986 SILC_LOG_ERROR(("Bad channel key payload, dropped"));
2991 /* Get the channel entry */
2994 /* Get channel ID */
2995 tmp = silc_channel_key_get_id(payload, &tmp_len);
2996 id = silc_id_str2id(tmp, tmp_len, SILC_ID_CHANNEL);
3002 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
3004 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
3006 SILC_LOG_ERROR(("Received key for non-existent channel"));
3012 tmp = silc_channel_key_get_key(payload, &tmp_len);
3018 cipher = silc_channel_key_get_cipher(payload, NULL);
3024 /* Remove old key if exists */
3026 memset(channel->key, 0, channel->key_len / 8);
3027 silc_free(channel->key);
3028 silc_cipher_free(channel->channel_key);
3031 /* Create new cipher */
3032 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3037 if (channel->cipher)
3038 silc_free(channel->cipher);
3039 channel->cipher = strdup(cipher);
3042 channel->key_len = tmp_len * 8;
3043 channel->key = silc_calloc(tmp_len, sizeof(unsigned char));
3044 memcpy(channel->key, tmp, tmp_len);
3045 silc_cipher_set_key(channel->channel_key, tmp, channel->key_len);
3047 /* Generate HMAC key from the channel key data and set it */
3049 silc_hmac_alloc("hmac-sha1-96", NULL, &channel->hmac);
3050 silc_hash_make(channel->hmac->hash, tmp, tmp_len, hash);
3051 silc_hmac_set_key(channel->hmac, hash, silc_hash_len(channel->hmac->hash));
3053 memset(hash, 0, sizeof(hash));
3054 memset(tmp, 0, tmp_len);
3056 if (server->server_type == SILC_ROUTER) {
3057 if (!channel->rekey)
3058 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
3059 channel->rekey->context = (void *)server;
3060 channel->rekey->channel = channel;
3062 silc_task_unregister_by_callback(server->timeout_queue,
3063 silc_server_channel_key_rekey);
3064 silc_task_register(server->timeout_queue, 0,
3065 silc_server_channel_key_rekey,
3066 (void *)channel->rekey, 3600, 0,
3068 SILC_TASK_PRI_NORMAL);
3075 silc_channel_key_payload_free(payload);
3080 /* Heartbeat callback. This function is set as argument for the
3081 silc_socket_set_heartbeat function. The library will call this function
3082 at the set time interval. */
3084 void silc_server_perform_heartbeat(SilcSocketConnection sock,
3087 SilcServerHBContext hb = (SilcServerHBContext)hb_context;
3089 SILC_LOG_DEBUG(("Sending heartbeat to %s (%s)", sock->hostname,
3092 /* Send the heartbeat */
3093 silc_server_send_heartbeat(hb->server, sock);
3096 /* Returns assembled of all servers in the given ID list. The packet's
3097 form is dictated by the New ID payload. */
3099 static void silc_server_announce_get_servers(SilcServer server,
3101 SilcBuffer *servers)
3103 SilcIDCacheList list;
3104 SilcIDCacheEntry id_cache;
3105 SilcServerEntry entry;
3108 /* Go through all clients in the list */
3109 if (silc_idcache_get_all(id_list->servers, &list)) {
3110 if (silc_idcache_list_first(list, &id_cache)) {
3112 entry = (SilcServerEntry)id_cache->context;
3114 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
3116 *servers = silc_buffer_realloc(*servers,
3118 (*servers)->truelen + idp->len :
3120 silc_buffer_pull_tail(*servers, ((*servers)->end - (*servers)->data));
3121 silc_buffer_put(*servers, idp->data, idp->len);
3122 silc_buffer_pull(*servers, idp->len);
3123 silc_buffer_free(idp);
3125 if (!silc_idcache_list_next(list, &id_cache))
3130 silc_idcache_list_free(list);
3134 /* This function is used by router to announce existing servers to our
3135 primary router when we've connected to it. */
3137 void silc_server_announce_servers(SilcServer server)
3139 SilcBuffer servers = NULL;
3141 SILC_LOG_DEBUG(("Announcing servers"));
3143 /* Get servers in local list */
3144 silc_server_announce_get_servers(server, server->local_list, &servers);
3146 /* Get servers in global list */
3147 silc_server_announce_get_servers(server, server->global_list, &servers);
3150 silc_buffer_push(servers, servers->data - servers->head);
3151 SILC_LOG_HEXDUMP(("servers"), servers->data, servers->len);
3153 /* Send the packet */
3154 silc_server_packet_send(server, server->router->connection,
3155 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3156 servers->data, servers->len, TRUE);
3158 silc_buffer_free(servers);
3162 /* Returns assembled packet of all clients in the given ID list. The
3163 packet's form is dictated by the New ID Payload. */
3165 static void silc_server_announce_get_clients(SilcServer server,
3167 SilcBuffer *clients)
3169 SilcIDCacheList list;
3170 SilcIDCacheEntry id_cache;
3171 SilcClientEntry client;
3174 /* Go through all clients in the list */
3175 if (silc_idcache_get_all(id_list->clients, &list)) {
3176 if (silc_idcache_list_first(list, &id_cache)) {
3178 client = (SilcClientEntry)id_cache->context;
3180 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3182 *clients = silc_buffer_realloc(*clients,
3184 (*clients)->truelen + idp->len :
3186 silc_buffer_pull_tail(*clients, ((*clients)->end - (*clients)->data));
3187 silc_buffer_put(*clients, idp->data, idp->len);
3188 silc_buffer_pull(*clients, idp->len);
3189 silc_buffer_free(idp);
3191 if (!silc_idcache_list_next(list, &id_cache))
3196 silc_idcache_list_free(list);
3200 /* This function is used to announce our existing clients to our router
3201 when we've connected to it. */
3203 void silc_server_announce_clients(SilcServer server)
3205 SilcBuffer clients = NULL;
3207 SILC_LOG_DEBUG(("Announcing clients"));
3209 /* Get clients in local list */
3210 silc_server_announce_get_clients(server, server->local_list,
3213 /* As router we announce our global list as well */
3214 if (server->server_type == SILC_ROUTER)
3215 silc_server_announce_get_clients(server, server->global_list,
3219 silc_buffer_push(clients, clients->data - clients->head);
3220 SILC_LOG_HEXDUMP(("clients"), clients->data, clients->len);
3222 /* Send the packet */
3223 silc_server_packet_send(server, server->router->connection,
3224 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3225 clients->data, clients->len, TRUE);
3227 silc_buffer_free(clients);
3232 silc_server_announce_encode_notify(SilcNotifyType notify, uint32 argc, ...)
3237 return silc_notify_payload_encode(notify, argc, ap);
3240 /* Returns assembled packets for channel users of the `channel'. */
3242 void silc_server_announce_get_channel_users(SilcServer server,
3243 SilcChannelEntry channel,
3244 SilcBuffer *channel_users,
3245 SilcBuffer *channel_users_modes)
3247 SilcChannelClientEntry chl;
3248 SilcHashTableList htl;
3249 SilcBuffer chidp, clidp;
3252 unsigned char mode[4];
3254 SILC_LOG_DEBUG(("Start"));
3256 /* Now find all users on the channel */
3257 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3258 silc_hash_table_list(channel->user_list, &htl);
3259 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3260 clidp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3263 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_JOIN, 2,
3264 clidp->data, clidp->len,
3265 chidp->data, chidp->len);
3268 silc_buffer_realloc(*channel_users,
3270 (*channel_users)->truelen + len : len));
3271 silc_buffer_pull_tail(*channel_users,
3272 ((*channel_users)->end -
3273 (*channel_users)->data));
3275 silc_buffer_put(*channel_users, tmp->data, tmp->len);
3276 silc_buffer_pull(*channel_users, len);
3277 silc_buffer_free(tmp);
3279 /* CUMODE notify for mode change on the channel */
3280 SILC_PUT32_MSB(chl->mode, mode);
3281 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CUMODE_CHANGE,
3282 3, clidp->data, clidp->len,
3284 clidp->data, clidp->len);
3286 *channel_users_modes =
3287 silc_buffer_realloc(*channel_users_modes,
3288 (*channel_users_modes ?
3289 (*channel_users_modes)->truelen + len : len));
3290 silc_buffer_pull_tail(*channel_users_modes,
3291 ((*channel_users_modes)->end -
3292 (*channel_users_modes)->data));
3294 silc_buffer_put(*channel_users_modes, tmp->data, tmp->len);
3295 silc_buffer_pull(*channel_users_modes, len);
3296 silc_buffer_free(tmp);
3298 silc_buffer_free(clidp);
3300 silc_buffer_free(chidp);
3303 /* Returns assembled packets for all channels and users on those channels
3304 from the given ID List. The packets are in the form dictated by the
3305 New Channel and New Channel User payloads. */
3307 void silc_server_announce_get_channels(SilcServer server,
3309 SilcBuffer *channels,
3310 SilcBuffer *channel_users,
3311 SilcBuffer **channel_users_modes,
3312 uint32 *channel_users_modes_c,
3313 SilcChannelID ***channel_ids)
3315 SilcIDCacheList list;
3316 SilcIDCacheEntry id_cache;
3317 SilcChannelEntry channel;
3322 int i = *channel_users_modes_c;
3324 SILC_LOG_DEBUG(("Start"));
3326 /* Go through all channels in the list */
3327 if (silc_idcache_get_all(id_list->channels, &list)) {
3328 if (silc_idcache_list_first(list, &id_cache)) {
3330 channel = (SilcChannelEntry)id_cache->context;
3332 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3333 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3334 name_len = strlen(channel->channel_name);
3336 len = 4 + name_len + id_len + 4;
3338 silc_buffer_realloc(*channels,
3339 (*channels ? (*channels)->truelen + len : len));
3340 silc_buffer_pull_tail(*channels,
3341 ((*channels)->end - (*channels)->data));
3342 silc_buffer_format(*channels,
3343 SILC_STR_UI_SHORT(name_len),
3344 SILC_STR_UI_XNSTRING(channel->channel_name,
3346 SILC_STR_UI_SHORT(id_len),
3347 SILC_STR_UI_XNSTRING(cid, id_len),
3348 SILC_STR_UI_INT(channel->mode),
3350 silc_buffer_pull(*channels, len);
3352 *channel_users_modes = silc_realloc(*channel_users_modes,
3353 sizeof(**channel_users_modes) *
3355 (*channel_users_modes)[i] = NULL;
3356 *channel_ids = silc_realloc(*channel_ids,
3357 sizeof(**channel_ids) * (i + 1));
3358 (*channel_ids)[i] = NULL;
3359 silc_server_announce_get_channel_users(server, channel,
3361 channel_users_modes[i]);
3362 (*channel_ids)[i] = channel->id;
3365 if (!silc_idcache_list_next(list, &id_cache))
3369 *channel_users_modes_c += i;
3372 silc_idcache_list_free(list);
3376 /* This function is used to announce our existing channels to our router
3377 when we've connected to it. This also announces the users on the
3378 channels to the router. */
3380 void silc_server_announce_channels(SilcServer server)
3382 SilcBuffer channels = NULL, channel_users = NULL;
3383 SilcBuffer *channel_users_modes = NULL;
3384 uint32 channel_users_modes_c = 0;
3385 SilcChannelID **channel_ids = NULL;
3387 SILC_LOG_DEBUG(("Announcing channels and channel users"));
3389 /* Get channels and channel users in local list */
3390 silc_server_announce_get_channels(server, server->local_list,
3391 &channels, &channel_users,
3392 &channel_users_modes,
3393 &channel_users_modes_c,
3396 /* Get channels and channel users in global list */
3397 silc_server_announce_get_channels(server, server->global_list,
3398 &channels, &channel_users,
3399 &channel_users_modes,
3400 &channel_users_modes_c,
3404 silc_buffer_push(channels, channels->data - channels->head);
3405 SILC_LOG_HEXDUMP(("channels"), channels->data, channels->len);
3407 /* Send the packet */
3408 silc_server_packet_send(server, server->router->connection,
3409 SILC_PACKET_NEW_CHANNEL, SILC_PACKET_FLAG_LIST,
3410 channels->data, channels->len,
3413 silc_buffer_free(channels);
3416 if (channel_users) {
3417 silc_buffer_push(channel_users, channel_users->data - channel_users->head);
3418 SILC_LOG_HEXDUMP(("channel users"), channel_users->data,
3419 channel_users->len);
3421 /* Send the packet */
3422 silc_server_packet_send(server, server->router->connection,
3423 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3424 channel_users->data, channel_users->len,
3427 silc_buffer_free(channel_users);
3430 if (channel_users_modes) {
3433 for (i = 0; i < channel_users_modes_c; i++) {
3434 silc_buffer_push(channel_users_modes[i],
3435 channel_users_modes[i]->data -
3436 channel_users_modes[i]->head);
3437 SILC_LOG_HEXDUMP(("channel users modes"), channel_users_modes[i]->data,
3438 channel_users_modes[i]->len);
3439 silc_server_packet_send_dest(server, server->router->connection,
3440 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3441 channel_ids[i], SILC_ID_CHANNEL,
3442 channel_users_modes[i]->data,
3443 channel_users_modes[i]->len,
3445 silc_buffer_free(channel_users_modes[i]);
3447 silc_free(channel_users_modes);
3448 silc_free(channel_ids);
3452 /* Failure timeout callback. If this is called then we will immediately
3453 process the received failure. We always process the failure with timeout
3454 since we do not want to blindly trust to received failure packets.
3455 This won't be called (the timeout is cancelled) if the failure was
3456 bogus (it is bogus if remote does not close the connection after sending
3459 SILC_TASK_CALLBACK(silc_server_failure_callback)
3461 SilcServerFailureContext f = (SilcServerFailureContext)context;
3463 if (f->sock->protocol) {
3464 f->sock->protocol->state = SILC_PROTOCOL_STATE_FAILURE;
3465 silc_protocol_execute(f->sock->protocol, f->server->timeout_queue, 0, 0);
3471 /* Assembles user list and users mode list from the `channel'. */
3473 void silc_server_get_users_on_channel(SilcServer server,
3474 SilcChannelEntry channel,
3475 SilcBuffer *user_list,
3476 SilcBuffer *mode_list,
3479 SilcChannelClientEntry chl;
3480 SilcHashTableList htl;
3481 SilcBuffer client_id_list;
3482 SilcBuffer client_mode_list;
3484 uint32 list_count = 0;
3486 /* XXX rewrite - this does not support IPv6 based Client ID's. */
3489 silc_buffer_alloc((SILC_ID_CLIENT_LEN + 4) *
3490 silc_hash_table_count(channel->user_list));
3492 silc_buffer_alloc(4 * silc_hash_table_count(channel->user_list));
3493 silc_buffer_pull_tail(client_id_list, SILC_BUFFER_END(client_id_list));
3494 silc_buffer_pull_tail(client_mode_list, SILC_BUFFER_END(client_mode_list));
3495 silc_hash_table_list(channel->user_list, &htl);
3496 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3498 idp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3499 silc_buffer_put(client_id_list, idp->data, idp->len);
3500 silc_buffer_pull(client_id_list, idp->len);
3501 silc_buffer_free(idp);
3503 /* Client's mode on channel */
3504 SILC_PUT32_MSB(chl->mode, client_mode_list->data);
3505 silc_buffer_pull(client_mode_list, 4);
3509 silc_buffer_push(client_id_list,
3510 client_id_list->data - client_id_list->head);
3511 silc_buffer_push(client_mode_list,
3512 client_mode_list->data - client_mode_list->head);
3514 *user_list = client_id_list;
3515 *mode_list = client_mode_list;
3516 *user_count = list_count;
3519 /* Saves users and their modes to the `channel'. */
3521 void silc_server_save_users_on_channel(SilcServer server,
3522 SilcSocketConnection sock,
3523 SilcChannelEntry channel,
3524 SilcClientID *noadd,
3525 SilcBuffer user_list,
3526 SilcBuffer mode_list,
3531 /* Cache the received Client ID's and modes. This cache expires
3532 whenever server sends notify message to channel. It means two things;
3533 some user has joined or leaved the channel. XXX TODO! */
3534 for (i = 0; i < user_count; i++) {
3537 SilcClientID *client_id;
3538 SilcClientEntry client;
3541 SILC_GET16_MSB(idp_len, user_list->data + 2);
3543 client_id = silc_id_payload_parse_id(user_list->data, idp_len);
3544 silc_buffer_pull(user_list, idp_len);
3549 SILC_GET32_MSB(mode, mode_list->data);
3550 silc_buffer_pull(mode_list, 4);
3552 if (noadd && SILC_ID_CLIENT_COMPARE(client_id, noadd)) {
3553 silc_free(client_id);
3557 /* Check if we have this client cached already. */
3558 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3561 client = silc_idlist_find_client_by_id(server->global_list,
3564 /* If router did not find such Client ID in its lists then this must
3565 be bogus client or some router in the net is buggy. */
3566 if (server->server_type == SILC_ROUTER) {
3567 silc_free(client_id);
3571 /* We don't have that client anywhere, add it. The client is added
3572 to global list since server didn't have it in the lists so it must be
3574 client = silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
3575 silc_id_dup(client_id, SILC_ID_CLIENT),
3576 sock->user_data, NULL);
3578 silc_free(client_id);
3582 client->data.registered = TRUE;
3585 silc_free(client_id);
3587 if (!silc_server_client_on_channel(client, channel)) {
3588 /* Client was not on the channel, add it. */
3589 SilcChannelClientEntry chl = silc_calloc(1, sizeof(*chl));
3590 chl->client = client;
3592 chl->channel = channel;
3593 silc_hash_table_add(channel->user_list, chl->client, chl);
3594 silc_hash_table_add(client->channels, chl->channel, chl);
3599 /* Lookups route to the client indicated by the `id_data'. The connection
3600 object and internal data object is returned. Returns NULL if route
3601 could not be found to the client. If the `client_id' is specified then
3602 it is used and the `id_data' is ignored. */
3604 SilcSocketConnection silc_server_get_client_route(SilcServer server,
3605 unsigned char *id_data,
3607 SilcClientID *client_id,
3608 SilcIDListData *idata)
3611 SilcClientEntry client;
3613 SILC_LOG_DEBUG(("Start"));
3615 /* Decode destination Client ID */
3617 id = silc_id_str2id(id_data, id_len, SILC_ID_CLIENT);
3619 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
3623 id = silc_id_dup(client_id, SILC_ID_CLIENT);
3626 /* If the destination belongs to our server we don't have to route
3627 the packet anywhere but to send it to the local destination. */
3628 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
3632 if (client && client->data.registered == FALSE)
3635 /* If we are router and the client has router then the client is in
3636 our cell but not directly connected to us. */
3637 if (server->server_type == SILC_ROUTER && client->router) {
3638 /* We are of course in this case the client's router thus the real
3639 "router" of the client is the server who owns the client. Thus
3640 we will send the packet to that server. */
3642 *idata = (SilcIDListData)client->router;
3643 return client->router->connection;
3646 /* Seems that client really is directly connected to us */
3648 *idata = (SilcIDListData)client;
3649 return client->connection;
3652 /* Destination belongs to someone not in this server. If we are normal
3653 server our action is to send the packet to our router. */
3654 if (server->server_type == SILC_SERVER && !server->standalone) {
3657 *idata = (SilcIDListData)server->router;
3658 return server->router->connection;
3661 /* We are router and we will perform route lookup for the destination
3662 and send the packet to fastest route. */
3663 if (server->server_type == SILC_ROUTER && !server->standalone) {
3664 /* Check first that the ID is valid */
3665 client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
3667 SilcSocketConnection dst_sock;
3669 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
3673 *idata = (SilcIDListData)dst_sock->user_data;
3682 /* Encodes and returns channel list of channels the `client' has joined.
3683 Secret channels are not put to the list. */
3685 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
3686 SilcClientEntry client)
3688 SilcBuffer buffer = NULL;
3689 SilcChannelEntry channel;
3690 SilcChannelClientEntry chl;
3691 SilcHashTableList htl;
3697 silc_hash_table_list(client->channels, &htl);
3698 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3699 channel = chl->channel;
3701 if (channel->mode & SILC_CHANNEL_MODE_SECRET)
3704 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3705 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3706 name_len = strlen(channel->channel_name);
3708 len = 4 + name_len + id_len + 4;
3709 buffer = silc_buffer_realloc(buffer,
3710 (buffer ? (buffer)->truelen + len : len));
3711 silc_buffer_pull_tail(buffer, ((buffer)->end - (buffer)->data));
3712 silc_buffer_format(buffer,
3713 SILC_STR_UI_SHORT(name_len),
3714 SILC_STR_UI_XNSTRING(channel->channel_name,
3716 SILC_STR_UI_SHORT(id_len),
3717 SILC_STR_UI_XNSTRING(cid, id_len),
3718 SILC_STR_UI_INT(chl->mode), /* Client's mode */
3720 silc_buffer_pull(buffer, len);
3725 silc_buffer_push(buffer, buffer->data - buffer->head);
3730 /* Finds client entry by Client ID and if it is not found then resolves
3731 it using WHOIS command. */
3733 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
3734 SilcClientID *client_id)
3736 SilcClientEntry client;
3738 client = silc_idlist_find_client_by_id(server->local_list, client_id, NULL);
3740 client = silc_idlist_find_client_by_id(server->global_list,
3742 if (!client && server->server_type == SILC_ROUTER)
3746 if (!client && server->standalone)
3749 if (!client || !client->nickname || !client->username) {
3750 SilcBuffer buffer, idp;
3752 idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3753 buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
3754 ++server->cmd_ident, 1,
3755 3, idp->data, idp->len);
3756 silc_server_packet_send(server, client ? client->router->connection :
3757 server->router->connection,
3758 SILC_PACKET_COMMAND, 0,
3759 buffer->data, buffer->len, FALSE);
3760 silc_buffer_free(idp);
3761 silc_buffer_free(buffer);
3767 /* A timeout callback for the re-key. We will be the initiator of the
3770 SILC_TASK_CALLBACK(silc_server_rekey_callback)
3772 SilcSocketConnection sock = (SilcSocketConnection)context;
3773 SilcIDListData idata = (SilcIDListData)sock->user_data;
3774 SilcServer server = (SilcServer)idata->rekey->context;
3775 SilcProtocol protocol;
3776 SilcServerRekeyInternalContext *proto_ctx;
3778 SILC_LOG_DEBUG(("Start"));
3780 /* Allocate internal protocol context. This is sent as context
3782 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3783 proto_ctx->server = (void *)server;
3784 proto_ctx->sock = sock;
3785 proto_ctx->responder = FALSE;
3786 proto_ctx->pfs = idata->rekey->pfs;
3788 /* Perform rekey protocol. Will call the final callback after the
3789 protocol is over. */
3790 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3791 &protocol, proto_ctx, silc_server_rekey_final);
3792 sock->protocol = protocol;
3794 /* Run the protocol */
3795 silc_protocol_execute(protocol, server->timeout_queue, 0, 0);
3797 /* Re-register re-key timeout */
3798 silc_task_register(server->timeout_queue, sock->sock,
3799 silc_server_rekey_callback,
3800 context, idata->rekey->timeout, 0,
3801 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
3804 /* The final callback for the REKEY protocol. This will actually take the
3805 new key material into use. */
3807 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final)
3809 SilcProtocol protocol = (SilcProtocol)context;
3810 SilcServerRekeyInternalContext *ctx =
3811 (SilcServerRekeyInternalContext *)protocol->context;
3812 SilcServer server = (SilcServer)ctx->server;
3813 SilcSocketConnection sock = ctx->sock;
3815 SILC_LOG_DEBUG(("Start"));
3817 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
3818 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
3819 /* Error occured during protocol */
3820 silc_protocol_cancel(protocol, server->timeout_queue);
3821 silc_protocol_free(protocol);
3822 sock->protocol = NULL;
3824 silc_packet_context_free(ctx->packet);
3826 silc_ske_free(ctx->ske);
3832 silc_protocol_free(protocol);
3833 sock->protocol = NULL;
3835 silc_packet_context_free(ctx->packet);
3837 silc_ske_free(ctx->ske);