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 silc_free(server->local_list);
81 silc_free(server->global_list);
83 silc_rng_free(server->rng);
86 silc_pkcs_free(server->pkcs);
89 while ((sim = silc_dlist_get(server->sim)) != SILC_LIST_END) {
90 silc_dlist_del(server->sim, sim);
93 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;
118 SilcServerConfigSectionListenPort *listen;
120 SILC_LOG_DEBUG(("Initializing server"));
122 assert(server->config);
124 /* Set public and private keys */
125 if (!server->config->server_keys ||
126 !server->config->server_keys->public_key ||
127 !server->config->server_keys->private_key) {
128 SILC_LOG_ERROR(("Server public key and/or private key does not exist"));
131 server->public_key = server->config->server_keys->public_key;
132 server->private_key = server->config->server_keys->private_key;
134 /* XXX After server is made as Silc Server Library this can be given
135 as argument, for now this is hard coded */
136 server->params = silc_calloc(1, sizeof(*server->params));
137 server->params->retry_count = SILC_SERVER_RETRY_COUNT;
138 server->params->retry_interval_min = SILC_SERVER_RETRY_INTERVAL_MIN;
139 server->params->retry_interval_max = SILC_SERVER_RETRY_INTERVAL_MAX;
140 server->params->retry_keep_trying = FALSE;
141 server->params->protocol_timeout = 60;
142 server->params->require_reverse_mapping = FALSE;
144 /* Set log files where log message should be saved. */
145 server->config->server = server;
146 silc_server_config_setlogfiles(server->config);
148 /* Register all configured ciphers, PKCS and hash functions. */
149 if (!silc_server_config_register_ciphers(server->config))
150 silc_cipher_register_default();
151 if (!silc_server_config_register_pkcs(server->config))
152 silc_pkcs_register_default();
153 if (!silc_server_config_register_hashfuncs(server->config))
154 silc_hash_register_default();
155 if (!silc_server_config_register_hmacs(server->config))
156 silc_hmac_register_default();
158 /* Initialize random number generator for the server. */
159 server->rng = silc_rng_alloc();
160 silc_rng_init(server->rng);
161 silc_rng_global_init(server->rng);
163 /* Initialize hash functions for server to use */
164 silc_hash_alloc("md5", &server->md5hash);
165 silc_hash_alloc("sha1", &server->sha1hash);
167 /* Initialize none cipher */
168 silc_cipher_alloc("none", &server->none_cipher);
170 /* Allocate PKCS context for local public and private keys */
171 silc_pkcs_alloc(server->public_key->name, &server->pkcs);
172 silc_pkcs_public_key_set(server->pkcs, server->public_key);
173 silc_pkcs_private_key_set(server->pkcs, server->private_key);
175 /* Create a listening server. Note that our server can listen on multiple
176 ports. All listeners are created here and now. */
178 listen = server->config->listen_port;
182 tmp = silc_net_create_server(server->config->listen_port->port,
183 server->config->listen_port->listener_ip);
187 sock = silc_realloc(sock, (sizeof(int *) * (sock_count + 1)));
188 sock[sock_count] = tmp;
190 listen = listen->next;
193 /* Initialize ID caches */
194 server->local_list->clients =
195 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
196 server->local_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
197 server->local_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
199 /* These are allocated for normal server as well as these hold some
200 global information that the server has fetched from its router. For
201 router these are used as they are supposed to be used on router. */
202 server->global_list->clients =
203 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
204 server->global_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
205 server->global_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
207 /* Allocate the entire socket list that is used in server. Eventually
208 all connections will have entry in this table (it is a table of
209 pointers to the actual object that is allocated individually
211 server->sockets = silc_calloc(SILC_SERVER_MAX_CONNECTIONS,
212 sizeof(*server->sockets));
214 for (i = 0; i < sock_count; i++) {
215 SilcSocketConnection newsocket = NULL;
217 /* Set socket to non-blocking mode */
218 silc_net_set_socket_nonblock(sock[i]);
219 server->sock = sock[i];
221 /* Create a Server ID for the server. */
222 silc_id_create_server_id(sock[i], server->rng, &id);
228 server->id_string = silc_id_id2str(id, SILC_ID_SERVER);
229 server->id_string_len = silc_id_get_len(id, SILC_ID_SERVER);
230 server->id_type = SILC_ID_SERVER;
231 server->server_name = server->config->server_info->server_name;
233 /* Add ourselves to the server list. We don't have a router yet
234 beacuse we haven't established a route yet. It will be done later.
235 For now, NULL is sent as router. This allocates new entry to
238 silc_idlist_add_server(server->local_list,
239 server->config->server_info->server_name,
240 server->server_type, server->id, NULL, NULL);
242 SILC_LOG_ERROR(("Could not add ourselves to cache"));
245 id_entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
247 /* Add ourselves also to the socket table. The entry allocated above
248 is sent as argument for fast referencing in the future. */
249 silc_socket_alloc(sock[i], SILC_SOCKET_TYPE_SERVER, id_entry,
252 server->sockets[sock[i]] = newsocket;
254 /* Perform name and address lookups to resolve the listenning address
256 if (!silc_net_check_local_by_sock(sock[i], &newsocket->hostname,
258 if ((server->params->require_reverse_mapping && !newsocket->hostname) ||
260 SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
261 newsocket->hostname ? newsocket->hostname :
262 newsocket->ip ? newsocket->ip : ""));
263 server->stat.conn_failures++;
266 if (!newsocket->hostname)
267 newsocket->hostname = strdup(newsocket->ip);
269 newsocket->port = silc_net_get_local_port(sock[i]);
271 /* Put the allocated socket pointer also to the entry allocated above
272 for fast back-referencing to the socket list. */
273 id_entry->connection = (void *)server->sockets[sock[i]];
274 server->id_entry = id_entry;
277 /* Register protocols */
278 silc_server_protocols_register();
280 /* Initialize the scheduler. */
281 server->schedule = silc_schedule_init(SILC_SERVER_MAX_CONNECTIONS);
282 if (!server->schedule)
285 /* Add the first task to the scheduler. 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_schedule_task_add(server->schedule, sock[0],
290 silc_server_connect_to_router,
291 (void *)server, 0, 1,
293 SILC_TASK_PRI_NORMAL);
295 /* Add listener task to the scheduler. This task receives new connections
296 to the server. This task remains on the queue until the end of the
298 silc_schedule_task_add(server->schedule, sock[0],
299 silc_server_accept_new_connection,
300 (void *)server, 0, 0,
302 SILC_TASK_PRI_NORMAL);
303 server->listenning = TRUE;
305 /* If server connections has been configured then we must be router as
306 normal server cannot have server connections, only router connections. */
307 if (server->config->servers) {
308 SilcServerConfigSectionServerConnection *ptr = server->config->servers;
310 server->server_type = SILC_ROUTER;
312 if (ptr->backup_router) {
313 server->server_type = SILC_BACKUP_ROUTER;
314 server->backup_router = TRUE;
315 server->id_entry->server_type = SILC_BACKUP_ROUTER;
322 /* Register the ID Cache purge task. This periodically purges the ID cache
323 and removes the expired cache entries. */
325 /* Clients local list */
326 purge = silc_calloc(1, sizeof(*purge));
327 purge->cache = server->local_list->clients;
328 purge->schedule = server->schedule;
329 silc_schedule_task_add(purge->schedule, 0,
331 (void *)purge, 600, 0,
332 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
334 /* Clients global list */
335 purge = silc_calloc(1, sizeof(*purge));
336 purge->cache = server->global_list->clients;
337 purge->schedule = server->schedule;
338 silc_schedule_task_add(purge->schedule, 0,
340 (void *)purge, 300, 0,
341 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
343 SILC_LOG_DEBUG(("Server initialized"));
345 /* We are done here, return succesfully */
349 for (i = 0; i < sock_count; i++)
350 silc_net_close_server(sock[i]);
355 /* Fork server to background and set gid+uid to non-root.
356 Silcd will not run as root, so trying to set either user or group to
357 root will cause silcd to exit. */
359 void silc_server_daemonise(SilcServer server)
361 /* Are we executing silcd as root or a regular user? */
368 if (!server->config->identity || !server->config->identity->user ||
369 !server->config->identity->group) {
370 fprintf(stderr, "Error:"
371 "\tSILC server must not be run as root. For the security of your\n"
372 "\tsystem it is strongly suggested that you run SILC under dedicated\n"
373 "\tuser account. Modify the [Identity] configuration section to run\n"
374 "\tthe server as non-root user.\n");
378 /* Get the values given for user and group in configuration file */
379 user=server->config->identity->user;
380 group=server->config->identity->group;
382 /* Check whether the user/group information is text */
383 if (atoi(user)!=0 || atoi(group)!=0) {
384 SILC_LOG_DEBUG(("Invalid user and/or group information"));
385 SILC_LOG_DEBUG(("User and/or group given as number"));
386 fprintf(stderr, "Invalid user and/or group information\n");
387 fprintf(stderr, "Please assign them as names, not numbers\n");
391 /* Catch the nasty incident of string "0" returning 0 from atoi */
392 if (strcmp("0", user)==0 || strcmp("0", group)==0) {
393 SILC_LOG_DEBUG(("User and/or group configured to 0. Unacceptable"));
394 fprintf(stderr, "User and/or group configured to 0. Exiting\n");
402 fprintf(stderr, "No such user %s found\n", user);
407 fprintf(stderr, "No such group %s found\n", group);
411 /* Check whether user and/or group is set to root. If yes, exit
412 immediately. Otherwise, setgid and setuid server to user.group */
413 if (gr->gr_gid==0 || pw->pw_uid==0) {
414 fprintf(stderr, "Error:"
415 "\tSILC server must not be run as root. For the security of your\n"
416 "\tsystem it is strongly suggested that you run SILC under dedicated\n"
417 "\tuser account. Modify the [Identity] configuration section to run\n"
418 "\tthe server as non-root user.\n");
421 /* Fork server to background, making it a daemon */
423 SILC_LOG_DEBUG(("Server started as root. Dropping privileges."));
424 SILC_LOG_DEBUG(("Forking SILC server to background"));
429 SILC_LOG_DEBUG(("Changing to group %s", group));
430 if(setgid(gr->gr_gid)==0) {
431 SILC_LOG_DEBUG(("Setgid to %s", group));
433 SILC_LOG_DEBUG(("Setgid to %s failed", group));
434 fprintf(stderr, "Tried to setgid %s but no such group. Exiting\n",
438 SILC_LOG_DEBUG(("Changing to user nobody"));
439 if(setuid(pw->pw_uid)==0) {
440 SILC_LOG_DEBUG(("Setuid to %s", user));
442 SILC_LOG_DEBUG(("Setuid to %s failed", user));
443 fprintf(stderr, "Tried to setuid %s but no such user. Exiting\n",
449 /* Fork server to background, making it a daemon */
451 SILC_LOG_DEBUG(("Server started as user"));
452 SILC_LOG_DEBUG(("Forking SILC server to background"));
459 /* The heart of the server. This runs the scheduler thus runs the server.
460 When this returns the server has been stopped and the program will
463 void silc_server_run(SilcServer server)
465 SILC_LOG_DEBUG(("Running server"));
467 SILC_LOG_INFO(("SILC Server started"));
469 /* Start the scheduler, the heart of the SILC server. When this returns
470 the program will be terminated. */
471 silc_schedule(server->schedule);
474 /* Stops the SILC server. This function is used to shutdown the server.
475 This is usually called after the scheduler has returned. After stopping
476 the server one should call silc_server_free. */
478 void silc_server_stop(SilcServer server)
480 SILC_LOG_DEBUG(("Stopping server"));
482 silc_schedule_stop(server->schedule);
483 silc_schedule_uninit(server->schedule);
485 silc_server_protocols_unregister();
487 SILC_LOG_DEBUG(("Server stopped"));
490 /* Function that is called when the network connection to a router has
491 been established. This will continue with the key exchange protocol
492 with the remote router. */
494 void silc_server_start_key_exchange(SilcServer server,
495 SilcServerConnection sconn,
498 SilcSocketConnection newsocket;
499 SilcProtocol protocol;
500 SilcServerKEInternalContext *proto_ctx;
503 /* Set socket options */
504 silc_net_set_socket_nonblock(sock);
505 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
507 /* Create socket connection for the connection. Even though we
508 know that we are connecting to a router we will mark the socket
509 to be unknown connection until we have executed authentication
511 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
512 server->sockets[sock] = newsocket;
513 newsocket->hostname = strdup(sconn->remote_host);
514 newsocket->ip = strdup(sconn->remote_host);
515 newsocket->port = sconn->remote_port;
516 sconn->sock = newsocket;
518 /* Allocate internal protocol context. This is sent as context
520 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
521 proto_ctx->server = (void *)server;
522 proto_ctx->context = (void *)sconn;
523 proto_ctx->sock = newsocket;
524 proto_ctx->rng = server->rng;
525 proto_ctx->responder = FALSE;
527 /* Perform key exchange protocol. silc_server_connect_to_router_second
528 will be called after the protocol is finished. */
529 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
530 &protocol, proto_ctx,
531 silc_server_connect_to_router_second);
532 newsocket->protocol = protocol;
534 /* Register a timeout task that will be executed if the protocol
535 is not executed within set limit. */
536 proto_ctx->timeout_task =
537 silc_schedule_task_add(server->schedule, sock,
538 silc_server_timeout_remote,
539 server, server->params->protocol_timeout,
540 server->params->protocol_timeout_usec,
544 /* Register the connection for network input and output. This sets
545 that scheduler will listen for incoming packets for this connection
546 and sets that outgoing packets may be sent to this connection as
547 well. However, this doesn't set the scheduler for outgoing traffic,
548 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
549 later when outgoing data is available. */
550 context = (void *)server;
551 SILC_REGISTER_CONNECTION_FOR_IO(sock);
553 /* Run the protocol */
554 silc_protocol_execute(protocol, server->schedule, 0, 0);
557 /* Timeout callback that will be called to retry connecting to remote
558 router. This is used by both normal and router server. This will wait
559 before retrying the connecting. The timeout is generated by exponential
560 backoff algorithm. */
562 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry)
564 SilcServerConnection sconn = (SilcServerConnection)context;
565 SilcServer server = sconn->server;
567 SILC_LOG_INFO(("Retrying connecting to a router"));
569 /* Calculate next timeout */
570 if (sconn->retry_count >= 1) {
571 sconn->retry_timeout = sconn->retry_timeout * SILC_SERVER_RETRY_MULTIPLIER;
572 if (sconn->retry_timeout > SILC_SERVER_RETRY_INTERVAL_MAX)
573 sconn->retry_timeout = SILC_SERVER_RETRY_INTERVAL_MAX;
575 sconn->retry_timeout = server->params->retry_interval_min;
577 sconn->retry_count++;
578 sconn->retry_timeout = sconn->retry_timeout +
579 silc_rng_get_rn32(server->rng) % SILC_SERVER_RETRY_RANDOMIZER;
581 /* If we've reached max retry count, give up. */
582 if (sconn->retry_count > server->params->retry_count &&
583 server->params->retry_keep_trying == FALSE) {
584 SILC_LOG_ERROR(("Could not connect to router, giving up"));
588 /* Wait one before retrying */
589 silc_schedule_task_add(server->schedule, fd, silc_server_connect_router,
590 context, sconn->retry_timeout,
591 server->params->retry_interval_min_usec,
592 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
595 /* Generic routine to use connect to a router. */
597 SILC_TASK_CALLBACK(silc_server_connect_router)
599 SilcServerConnection sconn = (SilcServerConnection)context;
600 SilcServer server = sconn->server;
603 SILC_LOG_INFO(("Connecting to the %s %s on port %d",
604 (sconn->backup ? "backup router" : "router"),
605 sconn->remote_host, sconn->remote_port));
607 server->router_connect = time(0);
609 /* Connect to remote host */
610 sock = silc_net_create_connection(server->config->listen_port->local_ip,
614 SILC_LOG_ERROR(("Could not connect to router"));
615 silc_schedule_task_add(server->schedule, fd,
616 silc_server_connect_to_router_retry,
617 context, 0, 1, SILC_TASK_TIMEOUT,
618 SILC_TASK_PRI_NORMAL);
622 /* Continue with key exchange protocol */
623 silc_server_start_key_exchange(server, sconn, sock);
626 /* This function connects to our primary router or if we are a router this
627 establishes all our primary routes. This is called at the start of the
628 server to do authentication and key exchange with our router - called
631 SILC_TASK_CALLBACK(silc_server_connect_to_router)
633 SilcServer server = (SilcServer)context;
634 SilcServerConnection sconn;
635 SilcServerConfigSectionServerConnection *ptr;
637 SILC_LOG_DEBUG(("Connecting to router(s)"));
639 if (server->server_type == SILC_SERVER) {
640 SILC_LOG_DEBUG(("We are normal server"));
641 } else if (server->server_type == SILC_ROUTER) {
642 SILC_LOG_DEBUG(("We are router"));
644 SILC_LOG_DEBUG(("We are backup router/normal server"));
647 /* Create the connections to all our routes */
648 ptr = server->config->routers;
651 SILC_LOG_DEBUG(("%s connection [%s] %s:%d",
652 ptr->backup_router ? "Backup router" : "Router",
653 ptr->initiator ? "Initiator" : "Responder",
654 ptr->host, ptr->port));
656 if (ptr->initiator) {
657 /* Allocate connection object for hold connection specific stuff. */
658 sconn = silc_calloc(1, sizeof(*sconn));
659 sconn->server = server;
660 sconn->remote_host = strdup(ptr->host);
661 sconn->remote_port = ptr->port;
662 sconn->backup = ptr->backup_router;
664 sconn->backup_replace_ip = strdup(ptr->backup_replace_ip);
665 sconn->backup_replace_port = ptr->backup_replace_port;
668 silc_schedule_task_add(server->schedule, fd,
669 silc_server_connect_router,
670 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
671 SILC_TASK_PRI_NORMAL);
680 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
682 /* There wasn't a configured router, we will continue but we don't
683 have a connection to outside world. We will be standalone server. */
684 server->standalone = TRUE;
687 /* Second part of connecting to router(s). Key exchange protocol has been
688 executed and now we will execute authentication protocol. */
690 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
692 SilcProtocol protocol = (SilcProtocol)context;
693 SilcServerKEInternalContext *ctx =
694 (SilcServerKEInternalContext *)protocol->context;
695 SilcServer server = (SilcServer)ctx->server;
696 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
697 SilcSocketConnection sock = ctx->sock;
698 SilcServerConnAuthInternalContext *proto_ctx;
699 SilcServerConfigSectionServerConnection *conn = NULL;
701 SILC_LOG_DEBUG(("Start"));
703 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
704 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
705 /* Error occured during protocol */
706 silc_protocol_free(protocol);
707 sock->protocol = NULL;
708 silc_ske_free_key_material(ctx->keymat);
710 silc_packet_context_free(ctx->packet);
712 silc_ske_free(ctx->ske);
713 silc_free(ctx->dest_id);
715 silc_schedule_task_del_by_callback(server->schedule,
716 silc_server_failure_callback);
717 silc_server_disconnect_remote(server, sock, "Server closed connection: "
718 "Key exchange failed");
722 /* We now have the key material as the result of the key exchange
723 protocol. Take the key material into use. Free the raw key material
724 as soon as we've set them into use. */
725 if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
726 ctx->ske->prop->cipher,
727 ctx->ske->prop->pkcs,
728 ctx->ske->prop->hash,
729 ctx->ske->prop->hmac,
730 ctx->ske->prop->group,
732 silc_protocol_free(protocol);
733 sock->protocol = NULL;
734 silc_ske_free_key_material(ctx->keymat);
736 silc_packet_context_free(ctx->packet);
738 silc_ske_free(ctx->ske);
739 silc_free(ctx->dest_id);
741 silc_schedule_task_del_by_callback(server->schedule,
742 silc_server_failure_callback);
743 silc_server_disconnect_remote(server, sock, "Server closed connection: "
744 "Key exchange failed");
747 silc_ske_free_key_material(ctx->keymat);
749 /* Allocate internal context for the authentication protocol. This
750 is sent as context for the protocol. */
751 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
752 proto_ctx->server = (void *)server;
753 proto_ctx->context = (void *)sconn;
754 proto_ctx->sock = sock;
755 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
756 proto_ctx->dest_id_type = ctx->dest_id_type;
757 proto_ctx->dest_id = ctx->dest_id;
759 /* Resolve the authentication method used in this connection. Check if
760 we find a match from user configured connections */
761 conn = silc_server_config_find_router_conn(server->config,
765 /* Match found. Use the configured authentication method */
766 proto_ctx->auth_meth = conn->auth_meth;
767 if (conn->auth_data) {
768 proto_ctx->auth_data = strdup(conn->auth_data);
769 proto_ctx->auth_data_len = strlen(conn->auth_data);
772 SILC_LOG_ERROR(("Could not find connection data for %s (%s) on port",
773 sock->hostname, sock->ip, sock->port));
774 silc_protocol_free(protocol);
775 sock->protocol = NULL;
777 silc_packet_context_free(ctx->packet);
779 silc_ske_free(ctx->ske);
780 silc_free(ctx->dest_id);
782 silc_schedule_task_del_by_callback(server->schedule,
783 silc_server_failure_callback);
784 silc_server_disconnect_remote(server, sock, "Server closed connection: "
785 "Key exchange failed");
789 /* Free old protocol as it is finished now */
790 silc_protocol_free(protocol);
792 silc_packet_context_free(ctx->packet);
794 sock->protocol = NULL;
796 /* Allocate the authentication protocol. This is allocated here
797 but we won't start it yet. We will be receiving party of this
798 protocol thus we will wait that connecting party will make
800 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
801 &sock->protocol, proto_ctx,
802 silc_server_connect_to_router_final);
804 /* Register timeout task. If the protocol is not executed inside
805 this timelimit the connection will be terminated. Currently
806 this is 15 seconds and is hard coded limit (XXX). */
807 proto_ctx->timeout_task =
808 silc_schedule_task_add(server->schedule, sock->sock,
809 silc_server_timeout_remote,
810 (void *)server, 15, 0,
814 /* Run the protocol */
815 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
818 /* Finalizes the connection to router. Registers a server task to the
819 queue so that we can accept new connections. */
821 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
823 SilcProtocol protocol = (SilcProtocol)context;
824 SilcServerConnAuthInternalContext *ctx =
825 (SilcServerConnAuthInternalContext *)protocol->context;
826 SilcServer server = (SilcServer)ctx->server;
827 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
828 SilcSocketConnection sock = ctx->sock;
829 SilcServerEntry id_entry;
831 SilcServerHBContext hb_context;
832 unsigned char *id_string;
834 SilcIDListData idata;
836 SILC_LOG_DEBUG(("Start"));
838 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
839 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
840 /* Error occured during protocol */
841 silc_free(ctx->dest_id);
842 silc_server_disconnect_remote(server, sock, "Server closed connection: "
843 "Authentication failed");
847 /* Add a task to the queue. This task receives new connections to the
848 server. This task remains on the queue until the end of the program. */
849 if (!server->listenning && !sconn->backup) {
850 silc_schedule_task_add(server->schedule, server->sock,
851 silc_server_accept_new_connection,
852 (void *)server, 0, 0,
854 SILC_TASK_PRI_NORMAL);
855 server->listenning = TRUE;
858 /* Send NEW_SERVER packet to the router. We will become registered
859 to the SILC network after sending this packet. */
860 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
861 id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
862 packet = silc_buffer_alloc(2 + 2 + id_len + strlen(server->server_name));
863 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
864 silc_buffer_format(packet,
865 SILC_STR_UI_SHORT(id_len),
866 SILC_STR_UI_XNSTRING(id_string, id_len),
867 SILC_STR_UI_SHORT(strlen(server->server_name)),
868 SILC_STR_UI_XNSTRING(server->server_name,
869 strlen(server->server_name)),
872 /* Send the packet */
873 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
874 packet->data, packet->len, TRUE);
875 silc_buffer_free(packet);
876 silc_free(id_string);
878 SILC_LOG_INFO(("Connected to router %s", sock->hostname));
880 /* Check that we do not have this ID already */
881 id_entry = silc_idlist_find_server_by_id(server->local_list,
882 ctx->dest_id, TRUE, NULL);
884 silc_idcache_del_by_context(server->local_list->servers, id_entry);
886 id_entry = silc_idlist_find_server_by_id(server->global_list,
887 ctx->dest_id, TRUE, NULL);
889 silc_idcache_del_by_context(server->global_list->servers, id_entry);
892 SILC_LOG_DEBUG(("New server id(%s)",
893 silc_id_render(ctx->dest_id, SILC_ID_SERVER)));
895 /* Add the connected router to global server list */
896 id_entry = silc_idlist_add_server(server->global_list,
897 strdup(sock->hostname),
898 SILC_ROUTER, ctx->dest_id, NULL, sock);
900 silc_free(ctx->dest_id);
901 silc_server_disconnect_remote(server, sock, "Server closed connection: "
902 "Authentication failed");
906 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
907 silc_free(sock->user_data);
908 sock->user_data = (void *)id_entry;
909 sock->type = SILC_SOCKET_TYPE_ROUTER;
910 idata = (SilcIDListData)sock->user_data;
911 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
913 /* Perform keepalive. The `hb_context' will be freed automatically
914 when finally calling the silc_socket_free function. XXX hardcoded
916 hb_context = silc_calloc(1, sizeof(*hb_context));
917 hb_context->server = server;
918 silc_socket_set_heartbeat(sock, 600, hb_context,
919 silc_server_perform_heartbeat,
922 /* Register re-key timeout */
923 idata->rekey->timeout = 3600; /* XXX hardcoded */
924 idata->rekey->context = (void *)server;
925 silc_schedule_task_add(server->schedule, sock->sock,
926 silc_server_rekey_callback,
927 (void *)sock, idata->rekey->timeout, 0,
928 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
930 if (!sconn->backup) {
931 /* Mark this router our primary router if we're still standalone */
932 if (server->standalone) {
933 server->id_entry->router = id_entry;
934 server->router = id_entry;
935 server->standalone = FALSE;
937 /* If we are router then announce our possible servers. */
938 if (server->server_type == SILC_ROUTER)
939 silc_server_announce_servers(server, FALSE, 0,
940 server->router->connection);
942 /* Announce our clients and channels to the router */
943 silc_server_announce_clients(server, 0, server->router->connection);
944 silc_server_announce_channels(server, 0, server->router->connection);
947 /* Add this server to be our backup router */
948 silc_server_backup_add(server, id_entry, sconn->backup_replace_ip,
949 sconn->backup_replace_port, FALSE);
952 sock->protocol = NULL;
954 /* Call the completion callback to indicate that we've connected to
957 (*sconn->callback)(server, id_entry, sconn->callback_context);
960 /* Free the temporary connection data context */
962 silc_free(sconn->remote_host);
963 silc_free(sconn->backup_replace_ip);
967 /* Free the protocol object */
968 if (sock->protocol == protocol)
969 sock->protocol = NULL;
970 silc_protocol_free(protocol);
972 silc_packet_context_free(ctx->packet);
974 silc_ske_free(ctx->ske);
978 /* Host lookup callbcak that is called after the incoming connection's
979 IP and FQDN lookup is performed. This will actually check the acceptance
980 of the incoming connection and will register the key exchange protocol
981 for this connection. */
984 silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
987 SilcServer server = (SilcServer)context;
988 SilcServerKEInternalContext *proto_ctx;
989 void *cconfig, *sconfig, *rconfig;
990 SilcServerConfigSectionDenyConnection *deny;
993 SILC_LOG_DEBUG(("Start"));
995 /* Check whether we could resolve both IP and FQDN. */
996 if (!sock->ip || (!strcmp(sock->ip, sock->hostname) &&
997 server->params->require_reverse_mapping)) {
998 SILC_LOG_ERROR(("IP/DNS lookup failed %s",
999 sock->hostname ? sock->hostname :
1000 sock->ip ? sock->ip : ""));
1001 server->stat.conn_failures++;
1002 silc_server_disconnect_remote(server, sock,
1003 "Server closed connection: Unknown host");
1007 /* Register the connection for network input and output. This sets
1008 that scheduler will listen for incoming packets for this connection
1009 and sets that outgoing packets may be sent to this connection as well.
1010 However, this doesn't set the scheduler for outgoing traffic, it
1011 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
1012 later when outgoing data is available. */
1013 SILC_REGISTER_CONNECTION_FOR_IO(sock->sock);
1015 SILC_LOG_INFO(("Incoming connection from %s (%s)", sock->hostname,
1018 port = server->sockets[server->sock]->port; /* Listenning port */
1020 /* Check whether this connection is denied to connect to us. */
1021 deny = silc_server_config_denied_conn(server->config, sock->ip, port);
1023 deny = silc_server_config_denied_conn(server->config, sock->hostname,
1026 /* The connection is denied */
1027 SILC_LOG_INFO(("Connection %s (%s) is denied",
1028 sock->hostname, sock->ip));
1029 silc_server_disconnect_remote(server, sock, deny->comment ?
1031 "Server closed connection: "
1032 "Connection refused");
1033 server->stat.conn_failures++;
1037 /* Check whether we have configred this sort of connection at all. We
1038 have to check all configurations since we don't know what type of
1039 connection this is. */
1040 if (!(cconfig = silc_server_config_find_client_conn(server->config,
1042 cconfig = silc_server_config_find_client_conn(server->config,
1045 if (!(sconfig = silc_server_config_find_server_conn(server->config,
1048 sconfig = silc_server_config_find_server_conn(server->config,
1051 if (!(rconfig = silc_server_config_find_router_conn(server->config,
1053 rconfig = silc_server_config_find_router_conn(server->config,
1056 if (!cconfig && !sconfig && !rconfig) {
1057 silc_server_disconnect_remote(server, sock,
1058 "Server closed connection: "
1059 "Connection refused");
1060 server->stat.conn_failures++;
1064 /* The connection is allowed */
1066 /* Allocate internal context for key exchange protocol. This is
1067 sent as context for the protocol. */
1068 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1069 proto_ctx->server = context;
1070 proto_ctx->sock = sock;
1071 proto_ctx->rng = server->rng;
1072 proto_ctx->responder = TRUE;
1073 proto_ctx->cconfig = cconfig;
1074 proto_ctx->sconfig = sconfig;
1075 proto_ctx->rconfig = rconfig;
1077 /* Prepare the connection for key exchange protocol. We allocate the
1078 protocol but will not start it yet. The connector will be the
1079 initiator of the protocol thus we will wait for initiation from
1080 there before we start the protocol. */
1081 server->stat.auth_attempts++;
1082 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1083 &sock->protocol, proto_ctx,
1084 silc_server_accept_new_connection_second);
1086 /* Register a timeout task that will be executed if the connector
1087 will not start the key exchange protocol within 60 seconds. For
1088 now, this is a hard coded limit. After 60 secs the connection will
1089 be closed if the key exchange protocol has not been started. */
1090 proto_ctx->timeout_task =
1091 silc_schedule_task_add(server->schedule, sock->sock,
1092 silc_server_timeout_remote,
1098 /* Accepts new connections to the server. Accepting new connections are
1099 done in three parts to make it async. */
1101 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
1103 SilcServer server = (SilcServer)context;
1104 SilcSocketConnection newsocket;
1107 SILC_LOG_DEBUG(("Accepting new connection"));
1109 server->stat.conn_attempts++;
1111 sock = silc_net_accept_connection(server->sock);
1113 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
1114 server->stat.conn_failures++;
1118 /* Check max connections */
1119 if (sock > SILC_SERVER_MAX_CONNECTIONS) {
1120 SILC_LOG_ERROR(("Refusing connection, server is full"));
1121 server->stat.conn_failures++;
1125 /* Set socket options */
1126 silc_net_set_socket_nonblock(sock);
1127 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
1129 /* We don't create a ID yet, since we don't know what type of connection
1130 this is yet. But, we do add the connection to the socket table. */
1131 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
1132 server->sockets[sock] = newsocket;
1134 /* Perform asynchronous host lookup. This will lookup the IP and the
1135 FQDN of the remote connection. After the lookup is done the connection
1136 is accepted further. */
1137 silc_socket_host_lookup(newsocket, TRUE,
1138 silc_server_accept_new_connection_lookup, context,
1142 /* Second part of accepting new connection. Key exchange protocol has been
1143 performed and now it is time to do little connection authentication
1144 protocol to figure out whether this connection is client or server
1145 and whether it has right to access this server (especially server
1146 connections needs to be authenticated). */
1148 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
1150 SilcProtocol protocol = (SilcProtocol)context;
1151 SilcServerKEInternalContext *ctx =
1152 (SilcServerKEInternalContext *)protocol->context;
1153 SilcServer server = (SilcServer)ctx->server;
1154 SilcSocketConnection sock = ctx->sock;
1155 SilcServerConnAuthInternalContext *proto_ctx;
1157 SILC_LOG_DEBUG(("Start"));
1159 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1160 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1161 /* Error occured during protocol */
1162 silc_protocol_free(protocol);
1163 sock->protocol = NULL;
1164 silc_ske_free_key_material(ctx->keymat);
1166 silc_packet_context_free(ctx->packet);
1168 silc_ske_free(ctx->ske);
1169 silc_free(ctx->dest_id);
1171 silc_schedule_task_del_by_callback(server->schedule,
1172 silc_server_failure_callback);
1173 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1174 "Key exchange failed");
1175 server->stat.auth_failures++;
1179 /* We now have the key material as the result of the key exchange
1180 protocol. Take the key material into use. Free the raw key material
1181 as soon as we've set them into use. */
1182 if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
1183 ctx->ske->prop->cipher,
1184 ctx->ske->prop->pkcs,
1185 ctx->ske->prop->hash,
1186 ctx->ske->prop->hmac,
1187 ctx->ske->prop->group,
1189 silc_protocol_free(protocol);
1190 sock->protocol = NULL;
1191 silc_ske_free_key_material(ctx->keymat);
1193 silc_packet_context_free(ctx->packet);
1195 silc_ske_free(ctx->ske);
1196 silc_free(ctx->dest_id);
1198 silc_schedule_task_del_by_callback(server->schedule,
1199 silc_server_failure_callback);
1200 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1201 "Key exchange failed");
1202 server->stat.auth_failures++;
1205 silc_ske_free_key_material(ctx->keymat);
1207 /* Allocate internal context for the authentication protocol. This
1208 is sent as context for the protocol. */
1209 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1210 proto_ctx->server = (void *)server;
1211 proto_ctx->sock = sock;
1212 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
1213 proto_ctx->responder = TRUE;
1214 proto_ctx->dest_id_type = ctx->dest_id_type;
1215 proto_ctx->dest_id = ctx->dest_id;
1216 proto_ctx->cconfig = ctx->cconfig;
1217 proto_ctx->sconfig = ctx->sconfig;
1218 proto_ctx->rconfig = ctx->rconfig;
1220 /* Free old protocol as it is finished now */
1221 silc_protocol_free(protocol);
1223 silc_packet_context_free(ctx->packet);
1225 sock->protocol = NULL;
1227 /* Allocate the authentication protocol. This is allocated here
1228 but we won't start it yet. We will be receiving party of this
1229 protocol thus we will wait that connecting party will make
1230 their first move. */
1231 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1232 &sock->protocol, proto_ctx,
1233 silc_server_accept_new_connection_final);
1235 /* Register timeout task. If the protocol is not executed inside
1236 this timelimit the connection will be terminated. Currently
1237 this is 60 seconds and is hard coded limit (XXX). */
1238 proto_ctx->timeout_task =
1239 silc_schedule_task_add(server->schedule, sock->sock,
1240 silc_server_timeout_remote,
1241 (void *)server, 60, 0,
1246 /* Final part of accepting new connection. The connection has now
1247 been authenticated and keys has been exchanged. We also know whether
1248 this is client or server connection. */
1250 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
1252 SilcProtocol protocol = (SilcProtocol)context;
1253 SilcServerConnAuthInternalContext *ctx =
1254 (SilcServerConnAuthInternalContext *)protocol->context;
1255 SilcServer server = (SilcServer)ctx->server;
1256 SilcSocketConnection sock = ctx->sock;
1257 SilcServerHBContext hb_context;
1258 SilcUnknownEntry entry = (SilcUnknownEntry)sock->user_data;
1259 void *id_entry = NULL;
1261 SILC_LOG_DEBUG(("Start"));
1263 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1264 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1265 /* Error occured during protocol */
1266 silc_protocol_free(protocol);
1267 sock->protocol = NULL;
1269 silc_packet_context_free(ctx->packet);
1271 silc_ske_free(ctx->ske);
1272 silc_free(ctx->dest_id);
1275 sock->protocol = NULL;
1276 silc_schedule_task_del_by_callback(server->schedule,
1277 silc_server_failure_callback);
1278 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1279 "Authentication failed");
1280 server->stat.auth_failures++;
1284 entry->data.last_receive = time(NULL);
1286 switch (ctx->conn_type) {
1287 case SILC_SOCKET_TYPE_CLIENT:
1289 SilcClientEntry client;
1291 SILC_LOG_DEBUG(("Remote host is client"));
1292 SILC_LOG_INFO(("Connection from %s (%s) is client", sock->hostname,
1295 /* Add the client to the client ID cache. The nickname and Client ID
1296 and other information is created after we have received NEW_CLIENT
1297 packet from client. */
1298 client = silc_idlist_add_client(server->local_list,
1299 NULL, NULL, NULL, NULL, NULL, sock);
1301 SILC_LOG_ERROR(("Could not add new client to cache"));
1302 silc_free(sock->user_data);
1303 silc_server_disconnect_remote(server, sock,
1304 "Server closed connection: "
1305 "Authentication failed");
1306 server->stat.auth_failures++;
1311 server->stat.my_clients++;
1312 server->stat.clients++;
1313 if (server->server_type == SILC_ROUTER)
1314 server->stat.cell_clients++;
1316 id_entry = (void *)client;
1319 case SILC_SOCKET_TYPE_SERVER:
1320 case SILC_SOCKET_TYPE_ROUTER:
1322 SilcServerEntry new_server;
1323 SilcServerConfigSectionServerConnection *conn =
1324 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1325 ctx->sconfig : ctx->rconfig;
1327 SILC_LOG_DEBUG(("Remote host is %s",
1328 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1329 "server" : (conn->backup_router ?
1330 "backup router" : "router")));
1331 SILC_LOG_INFO(("Connection from %s (%s) is %s", sock->hostname,
1332 sock->ip, ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1333 "server" : (conn->backup_router ?
1334 "backup router" : "router")));
1336 /* Add the server into server cache. The server name and Server ID
1337 is updated after we have received NEW_SERVER packet from the
1338 server. We mark ourselves as router for this server if we really
1341 silc_idlist_add_server((ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1342 server->local_list : (conn->backup_router ?
1343 server->local_list :
1344 server->global_list)),
1346 (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1347 SILC_SERVER : SILC_ROUTER),
1349 (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1350 server->id_entry : (conn->backup_router ?
1351 server->id_entry : NULL)),
1354 SILC_LOG_ERROR(("Could not add new server to cache"));
1355 silc_free(sock->user_data);
1356 silc_server_disconnect_remote(server, sock,
1357 "Server closed connection: "
1358 "Authentication failed");
1359 server->stat.auth_failures++;
1364 if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER)
1365 server->stat.my_servers++;
1367 server->stat.my_routers++;
1368 server->stat.servers++;
1370 id_entry = (void *)new_server;
1372 /* If the incoming connection is router and marked as backup router
1373 then add it to be one of our backups */
1374 if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER && conn->backup_router) {
1375 silc_server_backup_add(server, new_server, conn->backup_replace_ip,
1376 conn->backup_replace_port, conn->backup_local);
1378 /* Change it back to SERVER type since that's what it really is. */
1379 if (conn->backup_local)
1380 ctx->conn_type = SILC_SOCKET_TYPE_SERVER;
1382 new_server->server_type = SILC_BACKUP_ROUTER;
1385 /* Check whether this connection is to be our primary router connection
1386 if we do not already have the primary route. */
1387 if (server->standalone && ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1388 if (silc_server_config_is_primary_route(server->config) &&
1392 SILC_LOG_DEBUG(("We are not standalone server anymore"));
1393 server->standalone = FALSE;
1394 if (!server->id_entry->router) {
1395 server->id_entry->router = id_entry;
1396 server->router = id_entry;
1406 sock->type = ctx->conn_type;
1408 /* Add the common data structure to the ID entry. */
1410 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1412 /* Add to sockets internal pointer for fast referencing */
1413 silc_free(sock->user_data);
1414 sock->user_data = id_entry;
1416 /* Connection has been fully established now. Everything is ok. */
1417 SILC_LOG_DEBUG(("New connection authenticated"));
1419 /* Perform keepalive. The `hb_context' will be freed automatically
1420 when finally calling the silc_socket_free function. XXX hardcoded
1422 hb_context = silc_calloc(1, sizeof(*hb_context));
1423 hb_context->server = server;
1424 silc_socket_set_heartbeat(sock, 600, hb_context,
1425 silc_server_perform_heartbeat,
1429 silc_schedule_task_del_by_callback(server->schedule,
1430 silc_server_failure_callback);
1431 silc_protocol_free(protocol);
1433 silc_packet_context_free(ctx->packet);
1435 silc_ske_free(ctx->ske);
1436 silc_free(ctx->dest_id);
1438 sock->protocol = NULL;
1441 /* This function is used to read packets from network and send packets to
1442 network. This is usually a generic task. */
1444 SILC_TASK_CALLBACK(silc_server_packet_process)
1446 SilcServer server = (SilcServer)context;
1447 SilcSocketConnection sock = server->sockets[fd];
1448 SilcIDListData idata;
1449 SilcCipher cipher = NULL;
1450 SilcHmac hmac = NULL;
1451 uint32 sequence = 0;
1457 SILC_LOG_DEBUG(("Processing packet"));
1459 /* Packet sending */
1461 if (type == SILC_TASK_WRITE) {
1462 /* Do not send data to disconnected connection */
1463 if (SILC_IS_DISCONNECTED(sock))
1466 server->stat.packets_sent++;
1468 if (sock->outbuf->data - sock->outbuf->head)
1469 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
1471 /* Send the packet */
1472 ret = silc_packet_send(sock, TRUE);
1474 /* If returned -2 could not write to connection now, will do
1480 SILC_LOG_ERROR(("Error sending packet to connection "
1481 "%s:%d [%s]", sock->hostname, sock->port,
1482 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1483 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1484 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1489 /* The packet has been sent and now it is time to set the connection
1490 back to only for input. When there is again some outgoing data
1491 available for this connection it will be set for output as well.
1492 This call clears the output setting and sets it only for input. */
1493 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, fd);
1494 SILC_UNSET_OUTBUF_PENDING(sock);
1496 silc_buffer_clear(sock->outbuf);
1500 /* Packet receiving */
1502 /* Read some data from connection */
1503 ret = silc_packet_receive(sock);
1507 SILC_LOG_ERROR(("Error receiving packet from connection "
1508 "%s:%d [%s]", sock->hostname, sock->port,
1509 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1510 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1511 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1518 SILC_LOG_DEBUG(("Read EOF"));
1520 /* If connection is disconnecting already we will finally
1521 close the connection */
1522 if (SILC_IS_DISCONNECTING(sock)) {
1523 if (sock->user_data)
1524 silc_server_free_sock_user_data(server, sock);
1525 silc_server_close_connection(server, sock);
1529 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1530 SILC_SET_DISCONNECTING(sock);
1532 if (sock->user_data)
1533 silc_server_free_sock_user_data(server, sock);
1534 silc_server_close_connection(server, sock);
1538 /* If connection is disconnecting or disconnected we will ignore
1540 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1541 SILC_LOG_DEBUG(("Ignoring read data from disonnected connection"));
1545 server->stat.packets_received++;
1547 /* Get keys and stuff from ID entry */
1548 idata = (SilcIDListData)sock->user_data;
1550 cipher = idata->receive_key;
1551 hmac = idata->hmac_receive;
1552 sequence = idata->psn_receive;
1555 /* Process the packet. This will call the parser that will then
1556 decrypt and parse the packet. */
1557 silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1558 TRUE : FALSE, cipher, hmac, sequence,
1559 silc_server_packet_parse, server);
1562 /* Parses whole packet, received earlier. */
1564 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1566 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1567 SilcServer server = (SilcServer)parse_ctx->context;
1568 SilcSocketConnection sock = parse_ctx->sock;
1569 SilcPacketContext *packet = parse_ctx->packet;
1570 SilcIDListData idata = (SilcIDListData)sock->user_data;
1573 SILC_LOG_DEBUG(("Start"));
1575 /* XXX backwards support for 0.5.x
1576 XXX remove in 0.7.x */
1577 packet->sock = sock;
1579 /* Parse the packet */
1580 if (parse_ctx->normal)
1581 ret = silc_packet_parse(packet, idata ? idata->receive_key : NULL);
1583 ret = silc_packet_parse_special(packet, idata ? idata->receive_key : NULL);
1585 /* If entry is disabled ignore what we got. */
1586 if (ret != SILC_PACKET_RESUME_ROUTER &&
1587 idata && idata->status & SILC_IDLIST_STATUS_DISABLED) {
1588 SILC_LOG_DEBUG(("Connection is disabled"));
1592 if (ret == SILC_PACKET_NONE)
1595 /* Check that the the current client ID is same as in the client's packet. */
1596 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
1597 SilcClientEntry client = (SilcClientEntry)sock->user_data;
1598 if (client && client->id) {
1599 void *id = silc_id_str2id(packet->src_id, packet->src_id_len,
1600 packet->src_id_type);
1601 if (!id || !SILC_ID_CLIENT_COMPARE(client->id, id)) {
1609 if (server->server_type == SILC_ROUTER) {
1610 /* Route the packet if it is not destined to us. Other ID types but
1611 server are handled separately after processing them. */
1612 if (!(packet->flags & SILC_PACKET_FLAG_BROADCAST) &&
1613 packet->dst_id_type == SILC_ID_SERVER &&
1614 sock->type != SILC_SOCKET_TYPE_CLIENT &&
1615 memcmp(packet->dst_id, server->id_string, packet->dst_id_len)) {
1617 /* Route the packet to fastest route for the destination ID */
1618 void *id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1619 packet->dst_id_type);
1622 silc_server_packet_route(server,
1623 silc_server_route_get(server, id,
1624 packet->dst_id_type),
1631 /* Parse the incoming packet type */
1632 silc_server_packet_parse_type(server, sock, packet);
1634 if (server->server_type == SILC_ROUTER) {
1635 /* Broadcast packet if it is marked as broadcast packet and it is
1636 originated from router and we are router. */
1637 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1638 packet->flags & SILC_PACKET_FLAG_BROADCAST &&
1639 !server->standalone) {
1640 /* Broadcast to our primary route */
1641 silc_server_packet_broadcast(server, server->router->connection, packet);
1643 /* If we have backup routers then we need to feed all broadcast
1644 data to those servers. */
1645 silc_server_backup_broadcast(server, sock, packet);
1650 silc_packet_context_free(packet);
1651 silc_free(parse_ctx);
1654 /* Parser callback called by silc_packet_receive_process. This merely
1655 registers timeout that will handle the actual parsing when appropriate. */
1657 void silc_server_packet_parse(SilcPacketParserContext *parser_context,
1660 SilcServer server = (SilcServer)context;
1661 SilcSocketConnection sock = parser_context->sock;
1662 SilcIDListData idata = (SilcIDListData)sock->user_data;
1665 idata->psn_receive = parser_context->packet->sequence + 1;
1667 switch (sock->type) {
1668 case SILC_SOCKET_TYPE_UNKNOWN:
1669 case SILC_SOCKET_TYPE_CLIENT:
1670 /* Parse the packet with timeout */
1671 silc_schedule_task_add(server->schedule, sock->sock,
1672 silc_server_packet_parse_real,
1673 (void *)parser_context, 0, 100000,
1675 SILC_TASK_PRI_NORMAL);
1677 case SILC_SOCKET_TYPE_SERVER:
1678 case SILC_SOCKET_TYPE_ROUTER:
1679 /* Packets from servers are parsed as soon as possible */
1680 silc_schedule_task_add(server->schedule, sock->sock,
1681 silc_server_packet_parse_real,
1682 (void *)parser_context, 0, 1,
1684 SILC_TASK_PRI_NORMAL);
1691 /* Parses the packet type and calls what ever routines the packet type
1692 requires. This is done for all incoming packets. */
1694 void silc_server_packet_parse_type(SilcServer server,
1695 SilcSocketConnection sock,
1696 SilcPacketContext *packet)
1698 SilcPacketType type = packet->type;
1699 SilcIDListData idata = (SilcIDListData)sock->user_data;
1701 SILC_LOG_DEBUG(("Parsing packet type %d", type));
1703 /* Parse the packet type */
1705 case SILC_PACKET_DISCONNECT:
1706 SILC_LOG_DEBUG(("Disconnect packet"));
1707 if (packet->flags & SILC_PACKET_FLAG_LIST)
1711 case SILC_PACKET_SUCCESS:
1713 * Success received for something. For now we can have only
1714 * one protocol for connection executing at once hence this
1715 * success message is for whatever protocol is executing currently.
1717 SILC_LOG_DEBUG(("Success packet"));
1718 if (packet->flags & SILC_PACKET_FLAG_LIST)
1721 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1724 case SILC_PACKET_FAILURE:
1726 * Failure received for something. For now we can have only
1727 * one protocol for connection executing at once hence this
1728 * failure message is for whatever protocol is executing currently.
1730 SILC_LOG_DEBUG(("Failure packet"));
1731 if (packet->flags & SILC_PACKET_FLAG_LIST)
1733 if (sock->protocol) {
1734 SilcServerFailureContext f;
1735 f = silc_calloc(1, sizeof(*f));
1739 /* We will wait 5 seconds to process this failure packet */
1740 silc_schedule_task_add(server->schedule, sock->sock,
1741 silc_server_failure_callback, (void *)f, 5, 0,
1742 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1746 case SILC_PACKET_REJECT:
1747 SILC_LOG_DEBUG(("Reject packet"));
1748 if (packet->flags & SILC_PACKET_FLAG_LIST)
1753 case SILC_PACKET_NOTIFY:
1755 * Received notify packet. Server can receive notify packets from
1756 * router. Server then relays the notify messages to clients if needed.
1758 SILC_LOG_DEBUG(("Notify packet"));
1759 if (packet->flags & SILC_PACKET_FLAG_LIST)
1760 silc_server_notify_list(server, sock, packet);
1762 silc_server_notify(server, sock, packet);
1768 case SILC_PACKET_CHANNEL_MESSAGE:
1770 * Received channel message. Channel messages are special packets
1771 * (although probably most common ones) thus they are handled
1774 SILC_LOG_DEBUG(("Channel Message packet"));
1775 if (packet->flags & SILC_PACKET_FLAG_LIST)
1777 idata->last_receive = time(NULL);
1778 silc_server_channel_message(server, sock, packet);
1781 case SILC_PACKET_CHANNEL_KEY:
1783 * Received key for channel. As channels are created by the router
1784 * the keys are as well. We will distribute the key to all of our
1785 * locally connected clients on the particular channel. Router
1786 * never receives this channel and thus is ignored.
1788 SILC_LOG_DEBUG(("Channel Key packet"));
1789 if (packet->flags & SILC_PACKET_FLAG_LIST)
1791 silc_server_channel_key(server, sock, packet);
1797 case SILC_PACKET_COMMAND:
1799 * Recived command. Processes the command request and allocates the
1800 * command context and calls the command.
1802 SILC_LOG_DEBUG(("Command packet"));
1803 if (packet->flags & SILC_PACKET_FLAG_LIST)
1805 silc_server_command_process(server, sock, packet);
1808 case SILC_PACKET_COMMAND_REPLY:
1810 * Received command reply packet. Received command reply to command. It
1811 * may be reply to command sent by us or reply to command sent by client
1812 * that we've routed further.
1814 SILC_LOG_DEBUG(("Command Reply packet"));
1815 if (packet->flags & SILC_PACKET_FLAG_LIST)
1817 silc_server_command_reply(server, sock, packet);
1821 * Private Message packets
1823 case SILC_PACKET_PRIVATE_MESSAGE:
1825 * Received private message packet. The packet is coming from either
1828 SILC_LOG_DEBUG(("Private Message packet"));
1829 if (packet->flags & SILC_PACKET_FLAG_LIST)
1831 idata->last_receive = time(NULL);
1832 silc_server_private_message(server, sock, packet);
1835 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1837 * Private message key packet.
1839 if (packet->flags & SILC_PACKET_FLAG_LIST)
1841 silc_server_private_message_key(server, sock, packet);
1845 * Key Exchange protocol packets
1847 case SILC_PACKET_KEY_EXCHANGE:
1848 SILC_LOG_DEBUG(("KE packet"));
1849 if (packet->flags & SILC_PACKET_FLAG_LIST)
1852 if (sock->protocol && sock->protocol->protocol &&
1853 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1855 SilcServerKEInternalContext *proto_ctx =
1856 (SilcServerKEInternalContext *)sock->protocol->context;
1858 proto_ctx->packet = silc_packet_context_dup(packet);
1860 /* Let the protocol handle the packet */
1861 silc_protocol_execute(sock->protocol, server->schedule, 0, 100000);
1863 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1864 "protocol active, packet dropped."));
1868 case SILC_PACKET_KEY_EXCHANGE_1:
1869 SILC_LOG_DEBUG(("KE 1 packet"));
1870 if (packet->flags & SILC_PACKET_FLAG_LIST)
1873 if (sock->protocol && sock->protocol->protocol &&
1874 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1875 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1877 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
1878 SilcServerRekeyInternalContext *proto_ctx =
1879 (SilcServerRekeyInternalContext *)sock->protocol->context;
1881 if (proto_ctx->packet)
1882 silc_packet_context_free(proto_ctx->packet);
1884 proto_ctx->packet = silc_packet_context_dup(packet);
1886 /* Let the protocol handle the packet */
1887 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1889 SilcServerKEInternalContext *proto_ctx =
1890 (SilcServerKEInternalContext *)sock->protocol->context;
1892 if (proto_ctx->packet)
1893 silc_packet_context_free(proto_ctx->packet);
1895 proto_ctx->packet = silc_packet_context_dup(packet);
1896 proto_ctx->dest_id_type = packet->src_id_type;
1897 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1898 packet->src_id_type);
1899 if (!proto_ctx->dest_id)
1902 /* Let the protocol handle the packet */
1903 silc_protocol_execute(sock->protocol, server->schedule,
1907 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1908 "protocol active, packet dropped."));
1912 case SILC_PACKET_KEY_EXCHANGE_2:
1913 SILC_LOG_DEBUG(("KE 2 packet"));
1914 if (packet->flags & SILC_PACKET_FLAG_LIST)
1917 if (sock->protocol && sock->protocol->protocol &&
1918 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1919 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1921 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
1922 SilcServerRekeyInternalContext *proto_ctx =
1923 (SilcServerRekeyInternalContext *)sock->protocol->context;
1925 if (proto_ctx->packet)
1926 silc_packet_context_free(proto_ctx->packet);
1928 proto_ctx->packet = silc_packet_context_dup(packet);
1930 /* Let the protocol handle the packet */
1931 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1933 SilcServerKEInternalContext *proto_ctx =
1934 (SilcServerKEInternalContext *)sock->protocol->context;
1936 if (proto_ctx->packet)
1937 silc_packet_context_free(proto_ctx->packet);
1939 proto_ctx->packet = silc_packet_context_dup(packet);
1940 proto_ctx->dest_id_type = packet->src_id_type;
1941 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1942 packet->src_id_type);
1943 if (!proto_ctx->dest_id)
1946 /* Let the protocol handle the packet */
1947 silc_protocol_execute(sock->protocol, server->schedule,
1951 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1952 "protocol active, packet dropped."));
1956 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1958 * Connection authentication request packet. When we receive this packet
1959 * we will send to the other end information about our mandatory
1960 * authentication method for the connection. This packet maybe received
1963 SILC_LOG_DEBUG(("Connection authentication request packet"));
1964 if (packet->flags & SILC_PACKET_FLAG_LIST)
1966 silc_server_connection_auth_request(server, sock, packet);
1970 * Connection Authentication protocol packets
1972 case SILC_PACKET_CONNECTION_AUTH:
1973 /* Start of the authentication protocol. We receive here the
1974 authentication data and will verify it. */
1975 SILC_LOG_DEBUG(("Connection auth packet"));
1976 if (packet->flags & SILC_PACKET_FLAG_LIST)
1979 if (sock->protocol && sock->protocol->protocol->type
1980 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
1982 SilcServerConnAuthInternalContext *proto_ctx =
1983 (SilcServerConnAuthInternalContext *)sock->protocol->context;
1985 proto_ctx->packet = silc_packet_context_dup(packet);
1987 /* Let the protocol handle the packet */
1988 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1990 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
1991 "protocol active, packet dropped."));
1995 case SILC_PACKET_NEW_ID:
1997 * Received New ID packet. This includes some new ID that has been
1998 * created. It may be for client, server or channel. This is the way
1999 * to distribute information about new registered entities in the
2002 SILC_LOG_DEBUG(("New ID packet"));
2003 if (packet->flags & SILC_PACKET_FLAG_LIST)
2004 silc_server_new_id_list(server, sock, packet);
2006 silc_server_new_id(server, sock, packet);
2009 case SILC_PACKET_NEW_CLIENT:
2011 * Received new client packet. This includes client information that
2012 * we will use to create initial client ID. After creating new
2013 * ID we will send it to the client.
2015 SILC_LOG_DEBUG(("New Client packet"));
2016 if (packet->flags & SILC_PACKET_FLAG_LIST)
2018 silc_server_new_client(server, sock, packet);
2021 case SILC_PACKET_NEW_SERVER:
2023 * Received new server packet. This includes Server ID and some other
2024 * information that we may save. This is received after server has
2027 SILC_LOG_DEBUG(("New Server packet"));
2028 if (packet->flags & SILC_PACKET_FLAG_LIST)
2030 silc_server_new_server(server, sock, packet);
2033 case SILC_PACKET_NEW_CHANNEL:
2035 * Received new channel packet. Information about new channel in the
2036 * network are distributed using this packet.
2038 SILC_LOG_DEBUG(("New Channel packet"));
2039 if (packet->flags & SILC_PACKET_FLAG_LIST)
2040 silc_server_new_channel_list(server, sock, packet);
2042 silc_server_new_channel(server, sock, packet);
2045 case SILC_PACKET_HEARTBEAT:
2047 * Received heartbeat.
2049 SILC_LOG_DEBUG(("Heartbeat packet"));
2050 if (packet->flags & SILC_PACKET_FLAG_LIST)
2054 case SILC_PACKET_KEY_AGREEMENT:
2056 * Received heartbeat.
2058 SILC_LOG_DEBUG(("Key agreement packet"));
2059 if (packet->flags & SILC_PACKET_FLAG_LIST)
2061 silc_server_key_agreement(server, sock, packet);
2064 case SILC_PACKET_REKEY:
2066 * Received re-key packet. The sender wants to regenerate the session
2069 SILC_LOG_DEBUG(("Re-key packet"));
2070 if (packet->flags & SILC_PACKET_FLAG_LIST)
2072 silc_server_rekey(server, sock, packet);
2075 case SILC_PACKET_REKEY_DONE:
2077 * The re-key is done.
2079 SILC_LOG_DEBUG(("Re-key done packet"));
2080 if (packet->flags & SILC_PACKET_FLAG_LIST)
2083 if (sock->protocol && sock->protocol->protocol &&
2084 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2086 SilcServerRekeyInternalContext *proto_ctx =
2087 (SilcServerRekeyInternalContext *)sock->protocol->context;
2089 if (proto_ctx->packet)
2090 silc_packet_context_free(proto_ctx->packet);
2092 proto_ctx->packet = silc_packet_context_dup(packet);
2094 /* Let the protocol handle the packet */
2095 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2097 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
2098 "protocol active, packet dropped."));
2102 case SILC_PACKET_FTP:
2106 case SILC_PACKET_RESUME_ROUTER:
2107 /* Resume router packet received. This packet is received for backup
2108 router resuming protocol. */
2109 SILC_LOG_DEBUG(("Resume router packet"));
2110 if (packet->flags & SILC_PACKET_FLAG_LIST)
2112 silc_server_backup_resume_router(server, sock, packet);
2116 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
2122 /* Creates connection to a remote router. */
2124 void silc_server_create_connection(SilcServer server,
2125 char *remote_host, uint32 port)
2127 SilcServerConnection sconn;
2129 /* Allocate connection object for hold connection specific stuff. */
2130 sconn = silc_calloc(1, sizeof(*sconn));
2131 sconn->server = server;
2132 sconn->remote_host = strdup(remote_host);
2133 sconn->remote_port = port;
2135 silc_schedule_task_add(server->schedule, 0,
2136 silc_server_connect_router,
2137 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
2138 SILC_TASK_PRI_NORMAL);
2141 SILC_TASK_CALLBACK(silc_server_close_connection_final)
2143 silc_socket_free((SilcSocketConnection)context);
2146 /* Closes connection to socket connection */
2148 void silc_server_close_connection(SilcServer server,
2149 SilcSocketConnection sock)
2151 SILC_LOG_INFO(("Closing connection %s:%d [%s]", sock->hostname,
2153 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2154 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2155 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2158 /* We won't listen for this connection anymore */
2159 silc_schedule_unset_listen_fd(server->schedule, sock->sock);
2161 /* Unregister all tasks */
2162 silc_schedule_task_del_by_fd(server->schedule, sock->sock);
2164 /* Close the actual connection */
2165 silc_net_close_connection(sock->sock);
2166 server->sockets[sock->sock] = NULL;
2168 /* If sock->user_data is NULL then we'll check for active protocols
2169 here since the silc_server_free_sock_user_data has not been called
2170 for this connection. */
2171 if (!sock->user_data) {
2172 /* If any protocol is active cancel its execution. It will call
2173 the final callback which will finalize the disconnection. */
2174 if (sock->protocol) {
2175 silc_protocol_cancel(sock->protocol, server->schedule);
2176 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2177 silc_protocol_execute_final(sock->protocol, server->schedule);
2178 sock->protocol = NULL;
2183 silc_schedule_task_add(server->schedule, 0,
2184 silc_server_close_connection_final,
2185 (void *)sock, 0, 1, SILC_TASK_TIMEOUT,
2186 SILC_TASK_PRI_NORMAL);
2189 /* Sends disconnect message to remote connection and disconnects the
2192 void silc_server_disconnect_remote(SilcServer server,
2193 SilcSocketConnection sock,
2194 const char *fmt, ...)
2197 unsigned char buf[4096];
2202 memset(buf, 0, sizeof(buf));
2204 vsprintf(buf, fmt, ap);
2207 SILC_LOG_DEBUG(("Disconnecting remote host"));
2209 /* Notify remote end that the conversation is over. The notify message
2210 is tried to be sent immediately. */
2211 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2212 buf, strlen(buf), TRUE);
2214 /* Mark the connection to be disconnected */
2215 SILC_SET_DISCONNECTED(sock);
2216 silc_server_close_connection(server, sock);
2221 SilcClientEntry client;
2222 } *FreeClientInternal;
2224 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout)
2226 FreeClientInternal i = (FreeClientInternal)context;
2228 silc_idlist_del_data(i->client);
2229 silc_idcache_purge_by_context(i->server->local_list->clients, i->client);
2233 /* Frees client data and notifies about client's signoff. */
2235 void silc_server_free_client_data(SilcServer server,
2236 SilcSocketConnection sock,
2237 SilcClientEntry client,
2241 FreeClientInternal i = silc_calloc(1, sizeof(*i));
2243 /* If there is pending outgoing data for the client then purge it
2244 to the network before removing the client entry. */
2245 silc_server_packet_queue_purge(server, sock);
2247 /* Send SIGNOFF notify to routers. */
2248 if (notify && !server->standalone && server->router)
2249 silc_server_send_notify_signoff(server, server->router->connection,
2250 server->server_type == SILC_SERVER ?
2251 FALSE : TRUE, client->id, signoff);
2253 /* Remove client from all channels */
2255 silc_server_remove_from_channels(server, NULL, client,
2256 TRUE, signoff, TRUE);
2258 silc_server_remove_from_channels(server, NULL, client,
2259 FALSE, NULL, FALSE);
2261 /* We will not delete the client entry right away. We will take it
2262 into history (for WHOWAS command) for 5 minutes */
2265 silc_schedule_task_add(server->schedule, 0,
2266 silc_server_free_client_data_timeout,
2268 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2269 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
2270 client->router = NULL;
2271 client->connection = NULL;
2273 /* Free the client entry and everything in it */
2274 server->stat.my_clients--;
2275 server->stat.clients--;
2276 if (server->server_type == SILC_ROUTER)
2277 server->stat.cell_clients--;
2280 /* Frees user_data pointer from socket connection object. This also sends
2281 appropriate notify packets to the network to inform about leaving
2284 void silc_server_free_sock_user_data(SilcServer server,
2285 SilcSocketConnection sock)
2287 SILC_LOG_DEBUG(("Start"));
2289 switch (sock->type) {
2290 case SILC_SOCKET_TYPE_CLIENT:
2292 SilcClientEntry user_data = (SilcClientEntry)sock->user_data;
2293 silc_server_free_client_data(server, sock, user_data, TRUE, NULL);
2296 case SILC_SOCKET_TYPE_SERVER:
2297 case SILC_SOCKET_TYPE_ROUTER:
2299 SilcServerEntry user_data = (SilcServerEntry)sock->user_data;
2300 SilcServerEntry backup_router = NULL;
2303 backup_router = silc_server_backup_get(server, user_data->id);
2305 /* If this was our primary router connection then we're lost to
2306 the outside world. */
2307 if (server->router == user_data) {
2308 /* Check whether we have a backup router connection */
2309 if (!backup_router || backup_router == user_data) {
2310 silc_schedule_task_add(server->schedule, 0,
2311 silc_server_connect_to_router,
2314 SILC_TASK_PRI_NORMAL);
2316 server->id_entry->router = NULL;
2317 server->router = NULL;
2318 server->standalone = TRUE;
2319 backup_router = NULL;
2321 SILC_LOG_INFO(("New primary router is backup router %s",
2322 backup_router->server_name));
2323 SILC_LOG_DEBUG(("New primary router is backup router %s",
2324 backup_router->server_name));
2325 server->id_entry->router = backup_router;
2326 server->router = backup_router;
2327 server->router_connect = time(0);
2328 server->backup_primary = TRUE;
2329 if (server->server_type == SILC_BACKUP_ROUTER) {
2330 server->server_type = SILC_ROUTER;
2332 /* We'll need to constantly try to reconnect to the primary
2333 router so that we'll see when it comes back online. */
2334 silc_server_backup_reconnect(server, sock->ip, sock->port,
2335 silc_server_backup_connected,
2339 /* Mark this connection as replaced */
2340 silc_server_backup_replaced_add(server, user_data->id,
2343 } else if (backup_router) {
2344 SILC_LOG_INFO(("Enabling the use of backup router %s",
2345 backup_router->server_name));
2346 SILC_LOG_DEBUG(("Enabling the use of backup router %s",
2347 backup_router->server_name));
2349 /* Mark this connection as replaced */
2350 silc_server_backup_replaced_add(server, user_data->id,
2354 if (!backup_router) {
2355 /* Free all client entries that this server owns as they will
2356 become invalid now as well. */
2358 silc_server_remove_clients_by_server(server, user_data, TRUE);
2360 /* Update the client entries of this server to the new backup
2361 router. This also removes the clients that *really* was owned
2362 by the primary router and went down with the router. */
2363 silc_server_update_clients_by_server(server, user_data, backup_router,
2365 silc_server_update_servers_by_server(server, user_data, backup_router);
2368 /* Free the server entry */
2369 silc_server_backup_del(server, user_data);
2370 silc_server_backup_replaced_del(server, user_data);
2371 silc_idlist_del_data(user_data);
2372 if (!silc_idlist_del_server(server->local_list, user_data))
2373 silc_idlist_del_server(server->global_list, user_data);
2374 server->stat.my_servers--;
2375 server->stat.servers--;
2376 if (server->server_type == SILC_ROUTER)
2377 server->stat.cell_servers--;
2379 if (backup_router) {
2380 /* Announce all of our stuff that was created about 5 minutes ago.
2381 The backup router knows all the other stuff already. */
2382 if (server->server_type == SILC_ROUTER)
2383 silc_server_announce_servers(server, FALSE, time(0) - 300,
2384 backup_router->connection);
2386 /* Announce our clients and channels to the router */
2387 silc_server_announce_clients(server, time(0) - 300,
2388 backup_router->connection);
2389 silc_server_announce_channels(server, time(0) - 300,
2390 backup_router->connection);
2396 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2398 silc_idlist_del_data(user_data);
2399 silc_free(user_data);
2404 /* If any protocol is active cancel its execution */
2405 if (sock->protocol) {
2406 silc_protocol_cancel(sock->protocol, server->schedule);
2407 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2408 silc_protocol_execute_final(sock->protocol, server->schedule);
2409 sock->protocol = NULL;
2412 sock->user_data = NULL;
2415 /* Removes client from all channels it has joined. This is used when client
2416 connection is disconnected. If the client on a channel is last, the
2417 channel is removed as well. This sends the SIGNOFF notify types. */
2419 void silc_server_remove_from_channels(SilcServer server,
2420 SilcSocketConnection sock,
2421 SilcClientEntry client,
2423 char *signoff_message,
2426 SilcChannelEntry channel;
2427 SilcChannelClientEntry chl;
2428 SilcHashTableList htl;
2431 SILC_LOG_DEBUG(("Start"));
2433 if (!client || !client->id)
2436 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2438 /* Remove the client from all channels. The client is removed from
2439 the channels' user list. */
2440 silc_hash_table_list(client->channels, &htl);
2441 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
2442 channel = chl->channel;
2444 /* Remove channel from client's channel list */
2445 silc_hash_table_del(client->channels, channel);
2447 /* Remove channel if there is no users anymore */
2448 if (server->server_type == SILC_ROUTER &&
2449 silc_hash_table_count(channel->user_list) < 2) {
2451 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2452 if (silc_idlist_del_channel(server->local_list, channel))
2453 server->stat.my_channels--;
2454 else if (silc_idlist_del_channel(server->global_list, channel))
2455 server->stat.my_channels--;
2459 /* Remove client from channel's client list */
2460 silc_hash_table_del(channel->user_list, chl->client);
2462 /* If there is no global users on the channel anymore mark the channel
2463 as local channel. Do not check if the removed client is local client. */
2464 if (server->server_type != SILC_ROUTER && channel->global_users &&
2465 chl->client->router && !silc_server_channel_has_global(channel))
2466 channel->global_users = FALSE;
2469 server->stat.my_chanclients--;
2471 /* If there is not at least one local user on the channel then we don't
2472 need the channel entry anymore, we can remove it safely. */
2473 if (server->server_type != SILC_ROUTER &&
2474 !silc_server_channel_has_local(channel)) {
2475 /* Notify about leaving client if this channel has global users. */
2476 if (notify && channel->global_users)
2477 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2478 SILC_NOTIFY_TYPE_SIGNOFF,
2479 signoff_message ? 2 : 1,
2480 clidp->data, clidp->len,
2481 signoff_message, signoff_message ?
2482 strlen(signoff_message) : 0);
2485 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2487 if (channel->founder_key) {
2488 /* The founder auth data exists, do not remove the channel entry */
2489 SilcChannelClientEntry chl2;
2490 SilcHashTableList htl2;
2494 silc_hash_table_list(channel->user_list, &htl2);
2495 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
2496 silc_hash_table_del(chl2->client->channels, channel);
2497 silc_hash_table_del(channel->user_list, chl2->client);
2503 /* Remove the channel entry */
2504 if (silc_idlist_del_channel(server->local_list, channel))
2505 server->stat.my_channels--;
2506 else if (silc_idlist_del_channel(server->global_list, channel))
2507 server->stat.my_channels--;
2511 /* Send notify to channel about client leaving SILC and thus
2512 the entire channel. */
2514 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2515 SILC_NOTIFY_TYPE_SIGNOFF,
2516 signoff_message ? 2 : 1,
2517 clidp->data, clidp->len,
2518 signoff_message, signoff_message ?
2519 strlen(signoff_message) : 0);
2521 if (keygen && !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2522 /* Re-generate channel key */
2523 if (!silc_server_create_channel_key(server, channel, 0))
2526 /* Send the channel key to the channel. The key of course is not sent
2527 to the client who was removed from the channel. */
2528 silc_server_send_channel_key(server, client->connection, channel,
2529 server->server_type == SILC_ROUTER ?
2530 FALSE : !server->standalone);
2534 silc_buffer_free(clidp);
2537 /* Removes client from one channel. This is used for example when client
2538 calls LEAVE command to remove itself from the channel. Returns TRUE
2539 if channel still exists and FALSE if the channel is removed when
2540 last client leaves the channel. If `notify' is FALSE notify messages
2543 int silc_server_remove_from_one_channel(SilcServer server,
2544 SilcSocketConnection sock,
2545 SilcChannelEntry channel,
2546 SilcClientEntry client,
2549 SilcChannelClientEntry chl;
2552 SILC_LOG_DEBUG(("Start"));
2554 /* Get the entry to the channel, if this client is not on the channel
2556 if (!silc_hash_table_find(client->channels, channel, NULL, (void *)&chl))
2559 /* Remove the client from the channel. The client is removed from
2560 the channel's user list. */
2562 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2564 /* Remove channel from client's channel list */
2565 silc_hash_table_del(client->channels, chl->channel);
2567 /* Remove channel if there is no users anymore */
2568 if (server->server_type == SILC_ROUTER &&
2569 silc_hash_table_count(channel->user_list) < 2) {
2571 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2572 if (silc_idlist_del_channel(server->local_list, channel))
2573 server->stat.my_channels--;
2574 else if (silc_idlist_del_channel(server->global_list, channel))
2575 server->stat.my_channels--;
2576 silc_buffer_free(clidp);
2580 /* Remove client from channel's client list */
2581 silc_hash_table_del(channel->user_list, chl->client);
2583 /* If there is no global users on the channel anymore mark the channel
2584 as local channel. Do not check if the client is local client. */
2585 if (server->server_type != SILC_ROUTER && channel->global_users &&
2586 chl->client->router && !silc_server_channel_has_global(channel))
2587 channel->global_users = FALSE;
2590 server->stat.my_chanclients--;
2592 /* If there is not at least one local user on the channel then we don't
2593 need the channel entry anymore, we can remove it safely. */
2594 if (server->server_type != SILC_ROUTER &&
2595 !silc_server_channel_has_local(channel)) {
2596 /* Notify about leaving client if this channel has global users. */
2597 if (notify && channel->global_users)
2598 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2599 SILC_NOTIFY_TYPE_LEAVE, 1,
2600 clidp->data, clidp->len);
2602 silc_buffer_free(clidp);
2605 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2607 if (channel->founder_key) {
2608 /* The founder auth data exists, do not remove the channel entry */
2609 SilcChannelClientEntry chl2;
2610 SilcHashTableList htl2;
2614 silc_hash_table_list(channel->user_list, &htl2);
2615 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
2616 silc_hash_table_del(chl2->client->channels, channel);
2617 silc_hash_table_del(channel->user_list, chl2->client);
2623 /* Remove the channel entry */
2624 if (silc_idlist_del_channel(server->local_list, channel))
2625 server->stat.my_channels--;
2626 else if (silc_idlist_del_channel(server->global_list, channel))
2627 server->stat.my_channels--;
2631 /* Send notify to channel about client leaving the channel */
2633 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2634 SILC_NOTIFY_TYPE_LEAVE, 1,
2635 clidp->data, clidp->len);
2637 silc_buffer_free(clidp);
2641 /* Timeout callback. This is called if connection is idle or for some
2642 other reason is not responding within some period of time. This
2643 disconnects the remote end. */
2645 SILC_TASK_CALLBACK(silc_server_timeout_remote)
2647 SilcServer server = (SilcServer)context;
2648 SilcSocketConnection sock = server->sockets[fd];
2650 SILC_LOG_DEBUG(("Start"));
2655 /* If we have protocol active we must assure that we call the protocol's
2656 final callback so that all the memory is freed. */
2657 if (sock->protocol) {
2658 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2659 silc_protocol_execute_final(sock->protocol, server->schedule);
2663 if (sock->user_data)
2664 silc_server_free_sock_user_data(server, sock);
2666 silc_server_disconnect_remote(server, sock, "Server closed connection: "
2667 "Connection timeout");
2670 /* Creates new channel. Sends NEW_CHANNEL packet to primary route. This
2671 function may be used only by router. In real SILC network all channels
2672 are created by routers thus this function is never used by normal
2675 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
2676 SilcServerID *router_id,
2682 SilcChannelID *channel_id;
2683 SilcChannelEntry entry;
2687 SILC_LOG_DEBUG(("Creating new channel"));
2690 cipher = SILC_DEFAULT_CIPHER;
2692 hmac = SILC_DEFAULT_HMAC;
2694 /* Allocate cipher */
2695 if (!silc_cipher_alloc(cipher, &key))
2699 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2700 silc_cipher_free(key);
2704 channel_name = strdup(channel_name);
2706 /* Create the channel */
2707 if (!silc_id_create_channel_id(server, router_id, server->rng,
2709 silc_free(channel_name);
2710 silc_cipher_free(key);
2711 silc_hmac_free(newhmac);
2714 entry = silc_idlist_add_channel(server->local_list, channel_name,
2715 SILC_CHANNEL_MODE_NONE, channel_id,
2716 NULL, key, newhmac);
2718 silc_free(channel_name);
2719 silc_cipher_free(key);
2720 silc_hmac_free(newhmac);
2724 entry->cipher = strdup(cipher);
2725 entry->hmac_name = strdup(hmac);
2727 /* Now create the actual key material */
2728 if (!silc_server_create_channel_key(server, entry,
2729 silc_cipher_get_key_len(key) / 8)) {
2730 silc_free(channel_name);
2731 silc_cipher_free(key);
2732 silc_hmac_free(newhmac);
2733 silc_free(entry->cipher);
2734 silc_free(entry->hmac_name);
2738 /* Notify other routers about the new channel. We send the packet
2739 to our primary route. */
2740 if (broadcast && server->standalone == FALSE)
2741 silc_server_send_new_channel(server, server->router->connection, TRUE,
2742 channel_name, entry->id,
2743 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
2746 server->stat.my_channels++;
2751 /* Same as above but creates the channel with Channel ID `channel_id. */
2754 silc_server_create_new_channel_with_id(SilcServer server,
2758 SilcChannelID *channel_id,
2761 SilcChannelEntry entry;
2765 SILC_LOG_DEBUG(("Creating new channel"));
2768 cipher = SILC_DEFAULT_CIPHER;
2770 hmac = SILC_DEFAULT_HMAC;
2772 /* Allocate cipher */
2773 if (!silc_cipher_alloc(cipher, &key))
2777 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2778 silc_cipher_free(key);
2782 channel_name = strdup(channel_name);
2784 /* Create the channel */
2785 entry = silc_idlist_add_channel(server->local_list, channel_name,
2786 SILC_CHANNEL_MODE_NONE, channel_id,
2787 NULL, key, newhmac);
2789 silc_free(channel_name);
2793 /* Now create the actual key material */
2794 if (!silc_server_create_channel_key(server, entry,
2795 silc_cipher_get_key_len(key) / 8)) {
2796 silc_free(channel_name);
2800 /* Notify other routers about the new channel. We send the packet
2801 to our primary route. */
2802 if (broadcast && server->standalone == FALSE)
2803 silc_server_send_new_channel(server, server->router->connection, TRUE,
2804 channel_name, entry->id,
2805 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
2808 server->stat.my_channels++;
2813 /* Channel's key re-key timeout callback. */
2815 SILC_TASK_CALLBACK(silc_server_channel_key_rekey)
2817 SilcServerChannelRekey rekey = (SilcServerChannelRekey)context;
2818 SilcServer server = (SilcServer)rekey->context;
2822 if (!silc_server_create_channel_key(server, rekey->channel, rekey->key_len))
2825 silc_server_send_channel_key(server, NULL, rekey->channel, FALSE);
2828 /* Generates new channel key. This is used to create the initial channel key
2829 but also to re-generate new key for channel. If `key_len' is provided
2830 it is the bytes of the key length. */
2832 bool silc_server_create_channel_key(SilcServer server,
2833 SilcChannelEntry channel,
2837 unsigned char channel_key[32], hash[32];
2840 SILC_LOG_DEBUG(("Generating channel key"));
2842 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2843 SILC_LOG_DEBUG(("Channel has private keys, will not generate new key"));
2847 if (!channel->channel_key)
2848 if (!silc_cipher_alloc(SILC_DEFAULT_CIPHER, &channel->channel_key))
2853 else if (channel->key_len)
2854 len = channel->key_len / 8;
2856 len = silc_cipher_get_key_len(channel->channel_key) / 8;
2858 /* Create channel key */
2859 for (i = 0; i < len; i++) channel_key[i] = silc_rng_get_byte(server->rng);
2862 silc_cipher_set_key(channel->channel_key, channel_key, len * 8);
2864 /* Remove old key if exists */
2866 memset(channel->key, 0, channel->key_len / 8);
2867 silc_free(channel->key);
2871 channel->key_len = len * 8;
2872 channel->key = silc_calloc(len, sizeof(*channel->key));
2873 memcpy(channel->key, channel_key, len);
2874 memset(channel_key, 0, sizeof(channel_key));
2876 /* Generate HMAC key from the channel key data and set it */
2878 silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
2879 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key, len, hash);
2880 silc_hmac_set_key(channel->hmac, hash,
2881 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2882 memset(hash, 0, sizeof(hash));
2884 if (server->server_type == SILC_ROUTER) {
2885 if (!channel->rekey)
2886 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
2887 channel->rekey->context = (void *)server;
2888 channel->rekey->channel = channel;
2889 channel->rekey->key_len = key_len;
2890 if (channel->rekey->task)
2891 silc_schedule_task_del(server->schedule, channel->rekey->task);
2893 channel->rekey->task =
2894 silc_schedule_task_add(server->schedule, 0,
2895 silc_server_channel_key_rekey,
2896 (void *)channel->rekey, 3600, 0,
2898 SILC_TASK_PRI_NORMAL);
2904 /* Saves the channel key found in the encoded `key_payload' buffer. This
2905 function is used when we receive Channel Key Payload and also when we're
2906 processing JOIN command reply. Returns entry to the channel. */
2908 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
2909 SilcBuffer key_payload,
2910 SilcChannelEntry channel)
2912 SilcChannelKeyPayload payload = NULL;
2913 SilcChannelID *id = NULL;
2914 unsigned char *tmp, hash[32];
2918 SILC_LOG_DEBUG(("Start"));
2920 /* Decode channel key payload */
2921 payload = silc_channel_key_payload_parse(key_payload);
2923 SILC_LOG_ERROR(("Bad channel key payload, dropped"));
2928 /* Get the channel entry */
2931 /* Get channel ID */
2932 tmp = silc_channel_key_get_id(payload, &tmp_len);
2933 id = silc_id_str2id(tmp, tmp_len, SILC_ID_CHANNEL);
2939 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
2941 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
2943 SILC_LOG_ERROR(("Received key for non-existent channel"));
2949 tmp = silc_channel_key_get_key(payload, &tmp_len);
2955 cipher = silc_channel_key_get_cipher(payload, NULL);
2961 /* Remove old key if exists */
2963 memset(channel->key, 0, channel->key_len / 8);
2964 silc_free(channel->key);
2965 silc_cipher_free(channel->channel_key);
2968 /* Create new cipher */
2969 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
2974 if (channel->cipher)
2975 silc_free(channel->cipher);
2976 channel->cipher = strdup(cipher);
2979 channel->key_len = tmp_len * 8;
2980 channel->key = silc_calloc(tmp_len, sizeof(unsigned char));
2981 memcpy(channel->key, tmp, tmp_len);
2982 silc_cipher_set_key(channel->channel_key, tmp, channel->key_len);
2984 /* Generate HMAC key from the channel key data and set it */
2986 silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
2987 silc_hash_make(silc_hmac_get_hash(channel->hmac), tmp, tmp_len, hash);
2988 silc_hmac_set_key(channel->hmac, hash,
2989 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2991 memset(hash, 0, sizeof(hash));
2992 memset(tmp, 0, tmp_len);
2994 if (server->server_type == SILC_ROUTER) {
2995 if (!channel->rekey)
2996 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
2997 channel->rekey->context = (void *)server;
2998 channel->rekey->channel = channel;
2999 if (channel->rekey->task)
3000 silc_schedule_task_del(server->schedule, channel->rekey->task);
3002 channel->rekey->task =
3003 silc_schedule_task_add(server->schedule, 0,
3004 silc_server_channel_key_rekey,
3005 (void *)channel->rekey, 3600, 0,
3007 SILC_TASK_PRI_NORMAL);
3013 silc_channel_key_payload_free(payload);
3018 /* Heartbeat callback. This function is set as argument for the
3019 silc_socket_set_heartbeat function. The library will call this function
3020 at the set time interval. */
3022 void silc_server_perform_heartbeat(SilcSocketConnection sock,
3025 SilcServerHBContext hb = (SilcServerHBContext)hb_context;
3027 SILC_LOG_DEBUG(("Sending heartbeat to %s (%s)", sock->hostname,
3030 /* Send the heartbeat */
3031 silc_server_send_heartbeat(hb->server, sock);
3034 /* Returns assembled of all servers in the given ID list. The packet's
3035 form is dictated by the New ID payload. */
3037 static void silc_server_announce_get_servers(SilcServer server,
3038 SilcServerEntry remote,
3040 SilcBuffer *servers,
3041 unsigned long creation_time)
3043 SilcIDCacheList list;
3044 SilcIDCacheEntry id_cache;
3045 SilcServerEntry entry;
3048 /* Go through all clients in the list */
3049 if (silc_idcache_get_all(id_list->servers, &list)) {
3050 if (silc_idcache_list_first(list, &id_cache)) {
3052 entry = (SilcServerEntry)id_cache->context;
3054 /* Do not announce the one we've sending our announcements and
3055 do not announce ourself. Also check the creation time if it's
3057 if ((entry == remote) || (entry == server->id_entry) ||
3058 (creation_time && entry->data.created < creation_time)) {
3059 if (!silc_idcache_list_next(list, &id_cache))
3064 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
3066 *servers = silc_buffer_realloc(*servers,
3068 (*servers)->truelen + idp->len :
3070 silc_buffer_pull_tail(*servers, ((*servers)->end - (*servers)->data));
3071 silc_buffer_put(*servers, idp->data, idp->len);
3072 silc_buffer_pull(*servers, idp->len);
3073 silc_buffer_free(idp);
3075 if (!silc_idcache_list_next(list, &id_cache))
3080 silc_idcache_list_free(list);
3084 /* This function is used by router to announce existing servers to our
3085 primary router when we've connected to it. If `creation_time' is non-zero
3086 then only the servers that has been created after the `creation_time'
3087 will be announced. */
3089 void silc_server_announce_servers(SilcServer server, bool global,
3090 unsigned long creation_time,
3091 SilcSocketConnection remote)
3093 SilcBuffer servers = NULL;
3095 SILC_LOG_DEBUG(("Announcing servers"));
3097 /* Get servers in local list */
3098 silc_server_announce_get_servers(server, remote->user_data,
3099 server->local_list, &servers,
3103 /* Get servers in global list */
3104 silc_server_announce_get_servers(server, remote->user_data,
3105 server->global_list, &servers,
3109 silc_buffer_push(servers, servers->data - servers->head);
3110 SILC_LOG_HEXDUMP(("servers"), servers->data, servers->len);
3112 /* Send the packet */
3113 silc_server_packet_send(server, remote,
3114 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3115 servers->data, servers->len, TRUE);
3117 silc_buffer_free(servers);
3121 /* Returns assembled packet of all clients in the given ID list. The
3122 packet's form is dictated by the New ID Payload. */
3124 static void silc_server_announce_get_clients(SilcServer server,
3126 SilcBuffer *clients,
3127 unsigned long creation_time)
3129 SilcIDCacheList list;
3130 SilcIDCacheEntry id_cache;
3131 SilcClientEntry client;
3134 /* Go through all clients in the list */
3135 if (silc_idcache_get_all(id_list->clients, &list)) {
3136 if (silc_idcache_list_first(list, &id_cache)) {
3138 client = (SilcClientEntry)id_cache->context;
3140 if (creation_time && client->data.created < creation_time) {
3141 if (!silc_idcache_list_next(list, &id_cache))
3146 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3148 *clients = silc_buffer_realloc(*clients,
3150 (*clients)->truelen + idp->len :
3152 silc_buffer_pull_tail(*clients, ((*clients)->end - (*clients)->data));
3153 silc_buffer_put(*clients, idp->data, idp->len);
3154 silc_buffer_pull(*clients, idp->len);
3155 silc_buffer_free(idp);
3157 if (!silc_idcache_list_next(list, &id_cache))
3162 silc_idcache_list_free(list);
3166 /* This function is used to announce our existing clients to our router
3167 when we've connected to it. If `creation_time' is non-zero then only
3168 the clients that has been created after the `creation_time' will be
3171 void silc_server_announce_clients(SilcServer server,
3172 unsigned long creation_time,
3173 SilcSocketConnection remote)
3175 SilcBuffer clients = NULL;
3177 SILC_LOG_DEBUG(("Announcing clients"));
3179 /* Get clients in local list */
3180 silc_server_announce_get_clients(server, server->local_list,
3181 &clients, creation_time);
3183 /* As router we announce our global list as well */
3184 if (server->server_type == SILC_ROUTER)
3185 silc_server_announce_get_clients(server, server->global_list,
3186 &clients, creation_time);
3189 silc_buffer_push(clients, clients->data - clients->head);
3190 SILC_LOG_HEXDUMP(("clients"), clients->data, clients->len);
3192 /* Send the packet */
3193 silc_server_packet_send(server, remote,
3194 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3195 clients->data, clients->len, TRUE);
3197 silc_buffer_free(clients);
3202 silc_server_announce_encode_notify(SilcNotifyType notify, uint32 argc, ...)
3208 p = silc_notify_payload_encode(notify, argc, ap);
3214 /* Returns assembled packets for channel users of the `channel'. */
3216 void silc_server_announce_get_channel_users(SilcServer server,
3217 SilcChannelEntry channel,
3218 SilcBuffer *channel_users,
3219 SilcBuffer *channel_users_modes)
3221 SilcChannelClientEntry chl;
3222 SilcHashTableList htl;
3223 SilcBuffer chidp, clidp;
3226 unsigned char mode[4];
3228 SILC_LOG_DEBUG(("Start"));
3230 /* Now find all users on the channel */
3231 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3232 silc_hash_table_list(channel->user_list, &htl);
3233 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3234 clidp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3237 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_JOIN, 2,
3238 clidp->data, clidp->len,
3239 chidp->data, chidp->len);
3242 silc_buffer_realloc(*channel_users,
3244 (*channel_users)->truelen + len : len));
3245 silc_buffer_pull_tail(*channel_users,
3246 ((*channel_users)->end -
3247 (*channel_users)->data));
3249 silc_buffer_put(*channel_users, tmp->data, tmp->len);
3250 silc_buffer_pull(*channel_users, len);
3251 silc_buffer_free(tmp);
3253 /* CUMODE notify for mode change on the channel */
3254 SILC_PUT32_MSB(chl->mode, mode);
3255 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CUMODE_CHANGE,
3256 3, clidp->data, clidp->len,
3258 clidp->data, clidp->len);
3260 *channel_users_modes =
3261 silc_buffer_realloc(*channel_users_modes,
3262 (*channel_users_modes ?
3263 (*channel_users_modes)->truelen + len : len));
3264 silc_buffer_pull_tail(*channel_users_modes,
3265 ((*channel_users_modes)->end -
3266 (*channel_users_modes)->data));
3268 silc_buffer_put(*channel_users_modes, tmp->data, tmp->len);
3269 silc_buffer_pull(*channel_users_modes, len);
3270 silc_buffer_free(tmp);
3272 silc_buffer_free(clidp);
3274 silc_buffer_free(chidp);
3277 /* Returns assembled packets for all channels and users on those channels
3278 from the given ID List. The packets are in the form dictated by the
3279 New Channel and New Channel User payloads. */
3281 void silc_server_announce_get_channels(SilcServer server,
3283 SilcBuffer *channels,
3284 SilcBuffer *channel_users,
3285 SilcBuffer **channel_users_modes,
3286 uint32 *channel_users_modes_c,
3287 SilcChannelID ***channel_ids,
3288 unsigned long creation_time)
3290 SilcIDCacheList list;
3291 SilcIDCacheEntry id_cache;
3292 SilcChannelEntry channel;
3297 int i = *channel_users_modes_c;
3300 SILC_LOG_DEBUG(("Start"));
3302 /* Go through all channels in the list */
3303 if (silc_idcache_get_all(id_list->channels, &list)) {
3304 if (silc_idcache_list_first(list, &id_cache)) {
3306 channel = (SilcChannelEntry)id_cache->context;
3308 if (creation_time && channel->created < creation_time)
3313 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3314 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3315 name_len = strlen(channel->channel_name);
3318 len = 4 + name_len + id_len + 4;
3320 silc_buffer_realloc(*channels,
3321 (*channels ? (*channels)->truelen +
3323 silc_buffer_pull_tail(*channels,
3324 ((*channels)->end - (*channels)->data));
3325 silc_buffer_format(*channels,
3326 SILC_STR_UI_SHORT(name_len),
3327 SILC_STR_UI_XNSTRING(channel->channel_name,
3329 SILC_STR_UI_SHORT(id_len),
3330 SILC_STR_UI_XNSTRING(cid, id_len),
3331 SILC_STR_UI_INT(channel->mode),
3333 silc_buffer_pull(*channels, len);
3336 *channel_users_modes = silc_realloc(*channel_users_modes,
3337 sizeof(**channel_users_modes) *
3339 (*channel_users_modes)[i] = NULL;
3340 *channel_ids = silc_realloc(*channel_ids,
3341 sizeof(**channel_ids) * (i + 1));
3342 (*channel_ids)[i] = NULL;
3343 silc_server_announce_get_channel_users(server, channel,
3345 channel_users_modes[i]);
3346 (*channel_ids)[i] = channel->id;
3349 if (!silc_idcache_list_next(list, &id_cache))
3353 *channel_users_modes_c += i;
3356 silc_idcache_list_free(list);
3360 /* This function is used to announce our existing channels to our router
3361 when we've connected to it. This also announces the users on the
3362 channels to the router. If the `creation_time' is non-zero only the
3363 channels that was created after the `creation_time' are announced.
3364 Note that the channel users are still announced even if the `creation_time'
3367 void silc_server_announce_channels(SilcServer server,
3368 unsigned long creation_time,
3369 SilcSocketConnection remote)
3371 SilcBuffer channels = NULL, channel_users = NULL;
3372 SilcBuffer *channel_users_modes = NULL;
3373 uint32 channel_users_modes_c = 0;
3374 SilcChannelID **channel_ids = NULL;
3376 SILC_LOG_DEBUG(("Announcing channels and channel users"));
3378 /* Get channels and channel users in local list */
3379 silc_server_announce_get_channels(server, server->local_list,
3380 &channels, &channel_users,
3381 &channel_users_modes,
3382 &channel_users_modes_c,
3383 &channel_ids, creation_time);
3385 /* Get channels and channel users in global list */
3386 silc_server_announce_get_channels(server, server->global_list,
3387 &channels, &channel_users,
3388 &channel_users_modes,
3389 &channel_users_modes_c,
3390 &channel_ids, creation_time);
3393 silc_buffer_push(channels, channels->data - channels->head);
3394 SILC_LOG_HEXDUMP(("channels"), channels->data, channels->len);
3396 /* Send the packet */
3397 silc_server_packet_send(server, remote,
3398 SILC_PACKET_NEW_CHANNEL, SILC_PACKET_FLAG_LIST,
3399 channels->data, channels->len,
3402 silc_buffer_free(channels);
3405 if (channel_users) {
3406 silc_buffer_push(channel_users, channel_users->data - channel_users->head);
3407 SILC_LOG_HEXDUMP(("channel users"), channel_users->data,
3408 channel_users->len);
3410 /* Send the packet */
3411 silc_server_packet_send(server, remote,
3412 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3413 channel_users->data, channel_users->len,
3416 silc_buffer_free(channel_users);
3419 if (channel_users_modes) {
3422 for (i = 0; i < channel_users_modes_c; i++) {
3423 silc_buffer_push(channel_users_modes[i],
3424 channel_users_modes[i]->data -
3425 channel_users_modes[i]->head);
3426 SILC_LOG_HEXDUMP(("channel users modes"), channel_users_modes[i]->data,
3427 channel_users_modes[i]->len);
3428 silc_server_packet_send_dest(server, remote,
3429 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3430 channel_ids[i], SILC_ID_CHANNEL,
3431 channel_users_modes[i]->data,
3432 channel_users_modes[i]->len,
3434 silc_buffer_free(channel_users_modes[i]);
3436 silc_free(channel_users_modes);
3437 silc_free(channel_ids);
3441 /* Failure timeout callback. If this is called then we will immediately
3442 process the received failure. We always process the failure with timeout
3443 since we do not want to blindly trust to received failure packets.
3444 This won't be called (the timeout is cancelled) if the failure was
3445 bogus (it is bogus if remote does not close the connection after sending
3448 SILC_TASK_CALLBACK(silc_server_failure_callback)
3450 SilcServerFailureContext f = (SilcServerFailureContext)context;
3452 if (f->sock->protocol) {
3453 f->sock->protocol->state = SILC_PROTOCOL_STATE_FAILURE;
3454 silc_protocol_execute(f->sock->protocol, f->server->schedule, 0, 0);
3460 /* Assembles user list and users mode list from the `channel'. */
3462 void silc_server_get_users_on_channel(SilcServer server,
3463 SilcChannelEntry channel,
3464 SilcBuffer *user_list,
3465 SilcBuffer *mode_list,
3468 SilcChannelClientEntry chl;
3469 SilcHashTableList htl;
3470 SilcBuffer client_id_list;
3471 SilcBuffer client_mode_list;
3473 uint32 list_count = 0, len = 0;
3475 silc_hash_table_list(channel->user_list, &htl);
3476 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
3477 len += (silc_id_get_len(chl->client->id, SILC_ID_CLIENT) + 4);
3479 client_id_list = silc_buffer_alloc(len);
3481 silc_buffer_alloc(4 * silc_hash_table_count(channel->user_list));
3482 silc_buffer_pull_tail(client_id_list, SILC_BUFFER_END(client_id_list));
3483 silc_buffer_pull_tail(client_mode_list, SILC_BUFFER_END(client_mode_list));
3485 silc_hash_table_list(channel->user_list, &htl);
3486 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3488 idp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3489 silc_buffer_put(client_id_list, idp->data, idp->len);
3490 silc_buffer_pull(client_id_list, idp->len);
3491 silc_buffer_free(idp);
3493 /* Client's mode on channel */
3494 SILC_PUT32_MSB(chl->mode, client_mode_list->data);
3495 silc_buffer_pull(client_mode_list, 4);
3499 silc_buffer_push(client_id_list,
3500 client_id_list->data - client_id_list->head);
3501 silc_buffer_push(client_mode_list,
3502 client_mode_list->data - client_mode_list->head);
3504 *user_list = client_id_list;
3505 *mode_list = client_mode_list;
3506 *user_count = list_count;
3509 /* Saves users and their modes to the `channel'. */
3511 void silc_server_save_users_on_channel(SilcServer server,
3512 SilcSocketConnection sock,
3513 SilcChannelEntry channel,
3514 SilcClientID *noadd,
3515 SilcBuffer user_list,
3516 SilcBuffer mode_list,
3521 for (i = 0; i < user_count; i++) {
3524 SilcClientID *client_id;
3525 SilcClientEntry client;
3528 SILC_GET16_MSB(idp_len, user_list->data + 2);
3530 client_id = silc_id_payload_parse_id(user_list->data, idp_len);
3531 silc_buffer_pull(user_list, idp_len);
3536 SILC_GET32_MSB(mode, mode_list->data);
3537 silc_buffer_pull(mode_list, 4);
3539 if (noadd && SILC_ID_CLIENT_COMPARE(client_id, noadd)) {
3540 silc_free(client_id);
3544 /* Check if we have this client cached already. */
3545 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3546 server->server_type, NULL);
3548 client = silc_idlist_find_client_by_id(server->global_list,
3549 client_id, server->server_type,
3552 /* If router did not find such Client ID in its lists then this must
3553 be bogus client or some router in the net is buggy. */
3554 if (server->server_type == SILC_ROUTER) {
3555 silc_free(client_id);
3559 /* We don't have that client anywhere, add it. The client is added
3560 to global list since server didn't have it in the lists so it must be
3562 client = silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
3563 silc_id_dup(client_id, SILC_ID_CLIENT),
3564 sock->user_data, NULL);
3566 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
3567 silc_free(client_id);
3571 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3574 silc_free(client_id);
3576 if (!silc_server_client_on_channel(client, channel)) {
3577 /* Client was not on the channel, add it. */
3578 SilcChannelClientEntry chl = silc_calloc(1, sizeof(*chl));
3579 chl->client = client;
3581 chl->channel = channel;
3582 silc_hash_table_add(channel->user_list, chl->client, chl);
3583 silc_hash_table_add(client->channels, chl->channel, chl);
3588 /* Lookups route to the client indicated by the `id_data'. The connection
3589 object and internal data object is returned. Returns NULL if route
3590 could not be found to the client. If the `client_id' is specified then
3591 it is used and the `id_data' is ignored. */
3593 SilcSocketConnection silc_server_get_client_route(SilcServer server,
3594 unsigned char *id_data,
3596 SilcClientID *client_id,
3597 SilcIDListData *idata)
3600 SilcClientEntry client;
3602 SILC_LOG_DEBUG(("Start"));
3604 /* Decode destination Client ID */
3606 id = silc_id_str2id(id_data, id_len, SILC_ID_CLIENT);
3608 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
3612 id = silc_id_dup(client_id, SILC_ID_CLIENT);
3615 /* If the destination belongs to our server we don't have to route
3616 the packet anywhere but to send it to the local destination. */
3617 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
3621 /* If we are router and the client has router then the client is in
3622 our cell but not directly connected to us. */
3623 if (server->server_type == SILC_ROUTER && client->router) {
3624 /* We are of course in this case the client's router thus the route
3625 to the client is the server who owns the client. So, we will send
3626 the packet to that server. */
3628 *idata = (SilcIDListData)client->router;
3629 return client->router->connection;
3632 /* Seems that client really is directly connected to us */
3634 *idata = (SilcIDListData)client;
3635 return client->connection;
3638 /* Destination belongs to someone not in this server. If we are normal
3639 server our action is to send the packet to our router. */
3640 if (server->server_type != SILC_ROUTER && !server->standalone) {
3643 *idata = (SilcIDListData)server->router;
3644 return server->router->connection;
3647 /* We are router and we will perform route lookup for the destination
3648 and send the packet to fastest route. */
3649 if (server->server_type == SILC_ROUTER && !server->standalone) {
3650 /* Check first that the ID is valid */
3651 client = silc_idlist_find_client_by_id(server->global_list, id,
3654 SilcSocketConnection dst_sock;
3656 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
3660 *idata = (SilcIDListData)dst_sock->user_data;
3669 /* Encodes and returns channel list of channels the `client' has joined.
3670 Secret channels are not put to the list. */
3672 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
3673 SilcClientEntry client)
3675 SilcBuffer buffer = NULL;
3676 SilcChannelEntry channel;
3677 SilcChannelClientEntry chl;
3678 SilcHashTableList htl;
3684 silc_hash_table_list(client->channels, &htl);
3685 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3686 channel = chl->channel;
3688 if (channel->mode & SILC_CHANNEL_MODE_SECRET ||
3689 channel->mode & SILC_CHANNEL_MODE_PRIVATE)
3692 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3693 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3694 name_len = strlen(channel->channel_name);
3696 len = 4 + name_len + id_len + 4;
3697 buffer = silc_buffer_realloc(buffer,
3698 (buffer ? (buffer)->truelen + len : len));
3699 silc_buffer_pull_tail(buffer, ((buffer)->end - (buffer)->data));
3700 silc_buffer_format(buffer,
3701 SILC_STR_UI_SHORT(name_len),
3702 SILC_STR_UI_XNSTRING(channel->channel_name,
3704 SILC_STR_UI_SHORT(id_len),
3705 SILC_STR_UI_XNSTRING(cid, id_len),
3706 SILC_STR_UI_INT(chl->mode), /* Client's mode */
3708 silc_buffer_pull(buffer, len);
3713 silc_buffer_push(buffer, buffer->data - buffer->head);
3718 /* Finds client entry by Client ID and if it is not found then resolves
3719 it using WHOIS command. */
3721 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
3722 SilcClientID *client_id)
3724 SilcClientEntry client;
3726 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3729 client = silc_idlist_find_client_by_id(server->global_list,
3730 client_id, TRUE, NULL);
3731 if (!client && server->server_type == SILC_ROUTER)
3735 if (!client && server->standalone)
3738 if (!client || !client->nickname || !client->username) {
3739 SilcBuffer buffer, idp;
3741 client->data.status |= SILC_IDLIST_STATUS_RESOLVING;
3742 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
3743 client->resolve_cmd_ident = ++server->cmd_ident;
3745 idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3746 buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
3747 server->cmd_ident, 1,
3748 3, idp->data, idp->len);
3749 silc_server_packet_send(server, client ? client->router->connection :
3750 server->router->connection,
3751 SILC_PACKET_COMMAND, 0,
3752 buffer->data, buffer->len, FALSE);
3753 silc_buffer_free(idp);
3754 silc_buffer_free(buffer);
3761 /* A timeout callback for the re-key. We will be the initiator of the
3764 SILC_TASK_CALLBACK(silc_server_rekey_callback)
3766 SilcSocketConnection sock = (SilcSocketConnection)context;
3767 SilcIDListData idata = (SilcIDListData)sock->user_data;
3768 SilcServer server = (SilcServer)idata->rekey->context;
3769 SilcProtocol protocol;
3770 SilcServerRekeyInternalContext *proto_ctx;
3772 SILC_LOG_DEBUG(("Start"));
3774 /* Allocate internal protocol context. This is sent as context
3776 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3777 proto_ctx->server = (void *)server;
3778 proto_ctx->sock = sock;
3779 proto_ctx->responder = FALSE;
3780 proto_ctx->pfs = idata->rekey->pfs;
3782 /* Perform rekey protocol. Will call the final callback after the
3783 protocol is over. */
3784 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3785 &protocol, proto_ctx, silc_server_rekey_final);
3786 sock->protocol = protocol;
3788 /* Run the protocol */
3789 silc_protocol_execute(protocol, server->schedule, 0, 0);
3791 /* Re-register re-key timeout */
3792 silc_schedule_task_add(server->schedule, sock->sock,
3793 silc_server_rekey_callback,
3794 context, idata->rekey->timeout, 0,
3795 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
3798 /* The final callback for the REKEY protocol. This will actually take the
3799 new key material into use. */
3801 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final)
3803 SilcProtocol protocol = (SilcProtocol)context;
3804 SilcServerRekeyInternalContext *ctx =
3805 (SilcServerRekeyInternalContext *)protocol->context;
3806 SilcServer server = (SilcServer)ctx->server;
3807 SilcSocketConnection sock = ctx->sock;
3809 SILC_LOG_DEBUG(("Start"));
3811 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
3812 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
3813 /* Error occured during protocol */
3814 SILC_LOG_ERROR(("Error occurred during rekey protocol"));
3815 silc_protocol_cancel(protocol, server->schedule);
3816 silc_protocol_free(protocol);
3817 sock->protocol = NULL;
3819 silc_packet_context_free(ctx->packet);
3821 silc_ske_free(ctx->ske);
3826 /* Purge the outgoing data queue to assure that all rekey packets really
3827 go to the network before we quit the protocol. */
3828 silc_server_packet_queue_purge(server, sock);
3831 silc_protocol_free(protocol);
3832 sock->protocol = NULL;
3834 silc_packet_context_free(ctx->packet);
3836 silc_ske_free(ctx->ske);