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;
321 /* Register the ID Cache purge task. This periodically purges the ID cache
322 and removes the expired cache entries. */
324 /* Clients local list */
325 purge = silc_calloc(1, sizeof(*purge));
326 purge->cache = server->local_list->clients;
327 purge->schedule = server->schedule;
328 silc_schedule_task_add(purge->schedule, 0,
330 (void *)purge, 600, 0,
331 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
333 /* Clients global list */
334 purge = silc_calloc(1, sizeof(*purge));
335 purge->cache = server->global_list->clients;
336 purge->schedule = server->schedule;
337 silc_schedule_task_add(purge->schedule, 0,
339 (void *)purge, 300, 0,
340 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
342 SILC_LOG_DEBUG(("Server initialized"));
344 /* We are done here, return succesfully */
348 for (i = 0; i < sock_count; i++)
349 silc_net_close_server(sock[i]);
354 /* Fork server to background and set gid+uid to non-root.
355 Silcd will not run as root, so trying to set either user or group to
356 root will cause silcd to exit. */
358 void silc_server_daemonise(SilcServer server)
360 /* Are we executing silcd as root or a regular user? */
367 if (!server->config->identity || !server->config->identity->user ||
368 !server->config->identity->group) {
369 fprintf(stderr, "Error:"
370 "\tSILC server must not be run as root. For the security of your\n"
371 "\tsystem it is strongly suggested that you run SILC under dedicated\n"
372 "\tuser account. Modify the [Identity] configuration section to run\n"
373 "\tthe server as non-root user.\n");
377 /* Get the values given for user and group in configuration file */
378 user=server->config->identity->user;
379 group=server->config->identity->group;
381 /* Check whether the user/group information is text */
382 if (atoi(user)!=0 || atoi(group)!=0) {
383 SILC_LOG_DEBUG(("Invalid user and/or group information"));
384 SILC_LOG_DEBUG(("User and/or group given as number"));
385 fprintf(stderr, "Invalid user and/or group information\n");
386 fprintf(stderr, "Please assign them as names, not numbers\n");
390 /* Catch the nasty incident of string "0" returning 0 from atoi */
391 if (strcmp("0", user)==0 || strcmp("0", group)==0) {
392 SILC_LOG_DEBUG(("User and/or group configured to 0. Unacceptable"));
393 fprintf(stderr, "User and/or group configured to 0. Exiting\n");
401 fprintf(stderr, "No such user %s found\n", user);
406 fprintf(stderr, "No such group %s found\n", group);
410 /* Check whether user and/or group is set to root. If yes, exit
411 immediately. Otherwise, setgid and setuid server to user.group */
412 if (gr->gr_gid==0 || pw->pw_uid==0) {
413 fprintf(stderr, "Error:"
414 "\tSILC server must not be run as root. For the security of your\n"
415 "\tsystem it is strongly suggested that you run SILC under dedicated\n"
416 "\tuser account. Modify the [Identity] configuration section to run\n"
417 "\tthe server as non-root user.\n");
420 /* Fork server to background, making it a daemon */
422 SILC_LOG_DEBUG(("Server started as root. Dropping privileges."));
423 SILC_LOG_DEBUG(("Forking SILC server to background"));
428 SILC_LOG_DEBUG(("Changing to group %s", group));
429 if(setgid(gr->gr_gid)==0) {
430 SILC_LOG_DEBUG(("Setgid to %s", group));
432 SILC_LOG_DEBUG(("Setgid to %s failed", group));
433 fprintf(stderr, "Tried to setgid %s but no such group. Exiting\n",
437 SILC_LOG_DEBUG(("Changing to user nobody"));
438 if(setuid(pw->pw_uid)==0) {
439 SILC_LOG_DEBUG(("Setuid to %s", user));
441 SILC_LOG_DEBUG(("Setuid to %s failed", user));
442 fprintf(stderr, "Tried to setuid %s but no such user. Exiting\n",
448 /* Fork server to background, making it a daemon */
450 SILC_LOG_DEBUG(("Server started as user"));
451 SILC_LOG_DEBUG(("Forking SILC server to background"));
458 /* The heart of the server. This runs the scheduler thus runs the server.
459 When this returns the server has been stopped and the program will
462 void silc_server_run(SilcServer server)
464 SILC_LOG_DEBUG(("Running server"));
466 SILC_LOG_INFO(("SILC Server started"));
468 /* Start the scheduler, the heart of the SILC server. When this returns
469 the program will be terminated. */
470 silc_schedule(server->schedule);
473 /* Stops the SILC server. This function is used to shutdown the server.
474 This is usually called after the scheduler has returned. After stopping
475 the server one should call silc_server_free. */
477 void silc_server_stop(SilcServer server)
479 SILC_LOG_DEBUG(("Stopping server"));
481 silc_schedule_stop(server->schedule);
482 silc_schedule_uninit(server->schedule);
484 silc_server_protocols_unregister();
486 SILC_LOG_DEBUG(("Server stopped"));
489 /* Function that is called when the network connection to a router has
490 been established. This will continue with the key exchange protocol
491 with the remote router. */
493 void silc_server_start_key_exchange(SilcServer server,
494 SilcServerConnection sconn,
497 SilcSocketConnection newsocket;
498 SilcProtocol protocol;
499 SilcServerKEInternalContext *proto_ctx;
502 /* Set socket options */
503 silc_net_set_socket_nonblock(sock);
504 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
506 /* Create socket connection for the connection. Even though we
507 know that we are connecting to a router we will mark the socket
508 to be unknown connection until we have executed authentication
510 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
511 server->sockets[sock] = newsocket;
512 newsocket->hostname = strdup(sconn->remote_host);
513 newsocket->ip = strdup(sconn->remote_host);
514 newsocket->port = sconn->remote_port;
515 sconn->sock = newsocket;
517 /* Allocate internal protocol context. This is sent as context
519 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
520 proto_ctx->server = (void *)server;
521 proto_ctx->context = (void *)sconn;
522 proto_ctx->sock = newsocket;
523 proto_ctx->rng = server->rng;
524 proto_ctx->responder = FALSE;
526 /* Perform key exchange protocol. silc_server_connect_to_router_second
527 will be called after the protocol is finished. */
528 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
529 &protocol, proto_ctx,
530 silc_server_connect_to_router_second);
531 newsocket->protocol = protocol;
533 /* Register a timeout task that will be executed if the protocol
534 is not executed within set limit. */
535 proto_ctx->timeout_task =
536 silc_schedule_task_add(server->schedule, sock,
537 silc_server_timeout_remote,
538 server, server->params->protocol_timeout,
539 server->params->protocol_timeout_usec,
543 /* Register the connection for network input and output. This sets
544 that scheduler will listen for incoming packets for this connection
545 and sets that outgoing packets may be sent to this connection as
546 well. However, this doesn't set the scheduler for outgoing traffic,
547 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
548 later when outgoing data is available. */
549 context = (void *)server;
550 SILC_REGISTER_CONNECTION_FOR_IO(sock);
552 /* Run the protocol */
553 silc_protocol_execute(protocol, server->schedule, 0, 0);
556 /* Timeout callback that will be called to retry connecting to remote
557 router. This is used by both normal and router server. This will wait
558 before retrying the connecting. The timeout is generated by exponential
559 backoff algorithm. */
561 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry)
563 SilcServerConnection sconn = (SilcServerConnection)context;
564 SilcServer server = sconn->server;
566 SILC_LOG_INFO(("Retrying connecting to a router"));
568 /* Calculate next timeout */
569 if (sconn->retry_count >= 1) {
570 sconn->retry_timeout = sconn->retry_timeout * SILC_SERVER_RETRY_MULTIPLIER;
571 if (sconn->retry_timeout > SILC_SERVER_RETRY_INTERVAL_MAX)
572 sconn->retry_timeout = SILC_SERVER_RETRY_INTERVAL_MAX;
574 sconn->retry_timeout = server->params->retry_interval_min;
576 sconn->retry_count++;
577 sconn->retry_timeout = sconn->retry_timeout +
578 silc_rng_get_rn32(server->rng) % SILC_SERVER_RETRY_RANDOMIZER;
580 /* If we've reached max retry count, give up. */
581 if (sconn->retry_count > server->params->retry_count &&
582 server->params->retry_keep_trying == FALSE) {
583 SILC_LOG_ERROR(("Could not connect to router, giving up"));
587 /* Wait one before retrying */
588 silc_schedule_task_add(server->schedule, fd, silc_server_connect_router,
589 context, sconn->retry_timeout,
590 server->params->retry_interval_min_usec,
591 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
594 /* Generic routine to use connect to a router. */
596 SILC_TASK_CALLBACK(silc_server_connect_router)
598 SilcServerConnection sconn = (SilcServerConnection)context;
599 SilcServer server = sconn->server;
602 SILC_LOG_INFO(("Connecting to the %s %s on port %d",
603 (sconn->backup ? "backup router" : "router"),
604 sconn->remote_host, sconn->remote_port));
606 server->router_connect = time(0);
608 /* Connect to remote host */
609 sock = silc_net_create_connection(server->config->listen_port->local_ip,
613 SILC_LOG_ERROR(("Could not connect to router"));
614 silc_schedule_task_add(server->schedule, fd,
615 silc_server_connect_to_router_retry,
616 context, 0, 1, SILC_TASK_TIMEOUT,
617 SILC_TASK_PRI_NORMAL);
621 /* Continue with key exchange protocol */
622 silc_server_start_key_exchange(server, sconn, sock);
625 /* This function connects to our primary router or if we are a router this
626 establishes all our primary routes. This is called at the start of the
627 server to do authentication and key exchange with our router - called
630 SILC_TASK_CALLBACK(silc_server_connect_to_router)
632 SilcServer server = (SilcServer)context;
633 SilcServerConnection sconn;
634 SilcServerConfigSectionServerConnection *ptr;
636 SILC_LOG_DEBUG(("Connecting to router(s)"));
638 if (server->server_type == SILC_SERVER) {
639 SILC_LOG_DEBUG(("We are normal server"));
640 } else if (server->server_type == SILC_ROUTER) {
641 SILC_LOG_DEBUG(("We are router"));
643 SILC_LOG_DEBUG(("We are backup router/normal server"));
646 /* Create the connections to all our routes */
647 ptr = server->config->routers;
650 SILC_LOG_DEBUG(("%s connection [%s] %s:%d",
651 ptr->backup_router ? "Backup router" : "Router",
652 ptr->initiator ? "Initiator" : "Responder",
653 ptr->host, ptr->port));
655 if (ptr->initiator) {
656 /* Allocate connection object for hold connection specific stuff. */
657 sconn = silc_calloc(1, sizeof(*sconn));
658 sconn->server = server;
659 sconn->remote_host = strdup(ptr->host);
660 sconn->remote_port = ptr->port;
661 sconn->backup = ptr->backup_router;
663 silc_schedule_task_add(server->schedule, fd,
664 silc_server_connect_router,
665 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
666 SILC_TASK_PRI_NORMAL);
675 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
677 /* There wasn't a configured router, we will continue but we don't
678 have a connection to outside world. We will be standalone server. */
679 server->standalone = TRUE;
682 /* Second part of connecting to router(s). Key exchange protocol has been
683 executed and now we will execute authentication protocol. */
685 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
687 SilcProtocol protocol = (SilcProtocol)context;
688 SilcServerKEInternalContext *ctx =
689 (SilcServerKEInternalContext *)protocol->context;
690 SilcServer server = (SilcServer)ctx->server;
691 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
692 SilcSocketConnection sock = ctx->sock;
693 SilcServerConnAuthInternalContext *proto_ctx;
694 SilcServerConfigSectionServerConnection *conn = NULL;
696 SILC_LOG_DEBUG(("Start"));
698 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
699 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
700 /* Error occured during protocol */
701 silc_protocol_free(protocol);
702 sock->protocol = NULL;
703 silc_ske_free_key_material(ctx->keymat);
705 silc_packet_context_free(ctx->packet);
707 silc_ske_free(ctx->ske);
708 silc_free(ctx->dest_id);
710 silc_schedule_task_del_by_callback(server->schedule,
711 silc_server_failure_callback);
712 silc_server_disconnect_remote(server, sock, "Server closed connection: "
713 "Key exchange failed");
717 /* We now have the key material as the result of the key exchange
718 protocol. Take the key material into use. Free the raw key material
719 as soon as we've set them into use. */
720 if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
721 ctx->ske->prop->cipher,
722 ctx->ske->prop->pkcs,
723 ctx->ske->prop->hash,
724 ctx->ske->prop->hmac,
725 ctx->ske->prop->group,
727 silc_protocol_free(protocol);
728 sock->protocol = NULL;
729 silc_ske_free_key_material(ctx->keymat);
731 silc_packet_context_free(ctx->packet);
733 silc_ske_free(ctx->ske);
734 silc_free(ctx->dest_id);
736 silc_schedule_task_del_by_callback(server->schedule,
737 silc_server_failure_callback);
738 silc_server_disconnect_remote(server, sock, "Server closed connection: "
739 "Key exchange failed");
742 silc_ske_free_key_material(ctx->keymat);
744 /* Allocate internal context for the authentication protocol. This
745 is sent as context for the protocol. */
746 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
747 proto_ctx->server = (void *)server;
748 proto_ctx->context = (void *)sconn;
749 proto_ctx->sock = sock;
750 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
751 proto_ctx->dest_id_type = ctx->dest_id_type;
752 proto_ctx->dest_id = ctx->dest_id;
754 /* Resolve the authentication method used in this connection. Check if
755 we find a match from user configured connections */
756 conn = silc_server_config_find_router_conn(server->config,
760 /* Match found. Use the configured authentication method */
761 proto_ctx->auth_meth = conn->auth_meth;
762 if (conn->auth_data) {
763 proto_ctx->auth_data = strdup(conn->auth_data);
764 proto_ctx->auth_data_len = strlen(conn->auth_data);
767 SILC_LOG_ERROR(("Could not find connection data for %s (%s) on port",
768 sock->hostname, sock->ip, sock->port));
769 silc_protocol_free(protocol);
770 sock->protocol = NULL;
772 silc_packet_context_free(ctx->packet);
774 silc_ske_free(ctx->ske);
775 silc_free(ctx->dest_id);
777 silc_schedule_task_del_by_callback(server->schedule,
778 silc_server_failure_callback);
779 silc_server_disconnect_remote(server, sock, "Server closed connection: "
780 "Key exchange failed");
784 /* Free old protocol as it is finished now */
785 silc_protocol_free(protocol);
787 silc_packet_context_free(ctx->packet);
789 sock->protocol = NULL;
791 /* Allocate the authentication protocol. This is allocated here
792 but we won't start it yet. We will be receiving party of this
793 protocol thus we will wait that connecting party will make
795 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
796 &sock->protocol, proto_ctx,
797 silc_server_connect_to_router_final);
799 /* Register timeout task. If the protocol is not executed inside
800 this timelimit the connection will be terminated. Currently
801 this is 15 seconds and is hard coded limit (XXX). */
802 proto_ctx->timeout_task =
803 silc_schedule_task_add(server->schedule, sock->sock,
804 silc_server_timeout_remote,
805 (void *)server, 15, 0,
809 /* Run the protocol */
810 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
813 /* Finalizes the connection to router. Registers a server task to the
814 queue so that we can accept new connections. */
816 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
818 SilcProtocol protocol = (SilcProtocol)context;
819 SilcServerConnAuthInternalContext *ctx =
820 (SilcServerConnAuthInternalContext *)protocol->context;
821 SilcServer server = (SilcServer)ctx->server;
822 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
823 SilcSocketConnection sock = ctx->sock;
824 SilcServerEntry id_entry;
826 SilcServerHBContext hb_context;
827 unsigned char *id_string;
829 SilcIDListData idata;
831 SILC_LOG_DEBUG(("Start"));
833 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
834 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
835 /* Error occured during protocol */
836 silc_free(ctx->dest_id);
837 silc_server_disconnect_remote(server, sock, "Server closed connection: "
838 "Authentication failed");
842 /* Add a task to the queue. This task receives new connections to the
843 server. This task remains on the queue until the end of the program. */
844 if (!server->listenning && !sconn->backup) {
845 silc_schedule_task_add(server->schedule, server->sock,
846 silc_server_accept_new_connection,
847 (void *)server, 0, 0,
849 SILC_TASK_PRI_NORMAL);
850 server->listenning = TRUE;
853 /* Send NEW_SERVER packet to the router. We will become registered
854 to the SILC network after sending this packet. */
855 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
856 id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
857 packet = silc_buffer_alloc(2 + 2 + id_len + strlen(server->server_name));
858 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
859 silc_buffer_format(packet,
860 SILC_STR_UI_SHORT(id_len),
861 SILC_STR_UI_XNSTRING(id_string, id_len),
862 SILC_STR_UI_SHORT(strlen(server->server_name)),
863 SILC_STR_UI_XNSTRING(server->server_name,
864 strlen(server->server_name)),
867 /* Send the packet */
868 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
869 packet->data, packet->len, TRUE);
870 silc_buffer_free(packet);
871 silc_free(id_string);
873 SILC_LOG_INFO(("Connected to router %s", sock->hostname));
875 /* Add the connected router to local server list */
876 id_entry = silc_idlist_add_server(server->local_list, strdup(sock->hostname),
877 SILC_ROUTER, ctx->dest_id, NULL, sock);
879 silc_free(ctx->dest_id);
880 silc_server_disconnect_remote(server, sock, "Server closed connection: "
881 "Authentication failed");
885 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
886 silc_free(sock->user_data);
887 sock->user_data = (void *)id_entry;
888 sock->type = SILC_SOCKET_TYPE_ROUTER;
889 idata = (SilcIDListData)sock->user_data;
890 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
892 /* Perform keepalive. The `hb_context' will be freed automatically
893 when finally calling the silc_socket_free function. XXX hardcoded
895 hb_context = silc_calloc(1, sizeof(*hb_context));
896 hb_context->server = server;
897 silc_socket_set_heartbeat(sock, 600, hb_context,
898 silc_server_perform_heartbeat,
901 /* Register re-key timeout */
902 idata->rekey->timeout = 3600; /* XXX hardcoded */
903 idata->rekey->context = (void *)server;
904 silc_schedule_task_add(server->schedule, sock->sock,
905 silc_server_rekey_callback,
906 (void *)sock, idata->rekey->timeout, 0,
907 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
909 if (!sconn->backup) {
910 /* Mark this router our primary router if we're still standalone */
911 if (server->standalone) {
912 server->id_entry->router = id_entry;
913 server->router = id_entry;
914 server->standalone = FALSE;
916 /* If we are router then announce our possible servers. */
917 if (server->server_type == SILC_ROUTER)
918 silc_server_announce_servers(server, FALSE, 0,
919 server->router->connection);
921 /* Announce our clients and channels to the router */
922 silc_server_announce_clients(server, 0, server->router->connection);
923 silc_server_announce_channels(server, 0, server->router->connection);
926 /* Add this server to be our backup router */
927 silc_server_backup_add(server, id_entry, FALSE);
930 sock->protocol = NULL;
932 /* Call the completion callback to indicate that we've connected to
935 (*sconn->callback)(server, id_entry, sconn->callback_context);
938 /* Free the temporary connection data context */
940 silc_free(sconn->remote_host);
944 /* Free the protocol object */
945 if (sock->protocol == protocol)
946 sock->protocol = NULL;
947 silc_protocol_free(protocol);
949 silc_packet_context_free(ctx->packet);
951 silc_ske_free(ctx->ske);
955 /* Host lookup callbcak that is called after the incoming connection's
956 IP and FQDN lookup is performed. This will actually check the acceptance
957 of the incoming connection and will register the key exchange protocol
958 for this connection. */
961 silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
964 SilcServer server = (SilcServer)context;
965 SilcServerKEInternalContext *proto_ctx;
966 void *cconfig, *sconfig, *rconfig;
967 SilcServerConfigSectionDenyConnection *deny;
970 SILC_LOG_DEBUG(("Start"));
972 /* Check whether we could resolve both IP and FQDN. */
973 if (!sock->ip || (!strcmp(sock->ip, sock->hostname) &&
974 server->params->require_reverse_mapping)) {
975 SILC_LOG_ERROR(("IP/DNS lookup failed %s",
976 sock->hostname ? sock->hostname :
977 sock->ip ? sock->ip : ""));
978 server->stat.conn_failures++;
979 silc_server_disconnect_remote(server, sock,
980 "Server closed connection: Unknown host");
984 /* Register the connection for network input and output. This sets
985 that scheduler will listen for incoming packets for this connection
986 and sets that outgoing packets may be sent to this connection as well.
987 However, this doesn't set the scheduler for outgoing traffic, it
988 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
989 later when outgoing data is available. */
990 SILC_REGISTER_CONNECTION_FOR_IO(sock->sock);
992 SILC_LOG_INFO(("Incoming connection from %s (%s)", sock->hostname,
995 port = server->sockets[server->sock]->port; /* Listenning port */
997 /* Check whether this connection is denied to connect to us. */
998 deny = silc_server_config_denied_conn(server->config, sock->ip, port);
1000 deny = silc_server_config_denied_conn(server->config, sock->hostname,
1003 /* The connection is denied */
1004 SILC_LOG_INFO(("Connection %s (%s) is denied",
1005 sock->hostname, sock->ip));
1006 silc_server_disconnect_remote(server, sock, deny->comment ?
1008 "Server closed connection: "
1009 "Connection refused");
1010 server->stat.conn_failures++;
1014 /* Check whether we have configred this sort of connection at all. We
1015 have to check all configurations since we don't know what type of
1016 connection this is. */
1017 if (!(cconfig = silc_server_config_find_client_conn(server->config,
1019 cconfig = silc_server_config_find_client_conn(server->config,
1022 if (!(sconfig = silc_server_config_find_server_conn(server->config,
1025 sconfig = silc_server_config_find_server_conn(server->config,
1028 if (!(rconfig = silc_server_config_find_router_conn(server->config,
1030 rconfig = silc_server_config_find_router_conn(server->config,
1033 if (!cconfig && !sconfig && !rconfig) {
1034 silc_server_disconnect_remote(server, sock,
1035 "Server closed connection: "
1036 "Connection refused");
1037 server->stat.conn_failures++;
1041 /* The connection is allowed */
1043 /* Allocate internal context for key exchange protocol. This is
1044 sent as context for the protocol. */
1045 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1046 proto_ctx->server = context;
1047 proto_ctx->sock = sock;
1048 proto_ctx->rng = server->rng;
1049 proto_ctx->responder = TRUE;
1050 proto_ctx->cconfig = cconfig;
1051 proto_ctx->sconfig = sconfig;
1052 proto_ctx->rconfig = rconfig;
1054 /* Prepare the connection for key exchange protocol. We allocate the
1055 protocol but will not start it yet. The connector will be the
1056 initiator of the protocol thus we will wait for initiation from
1057 there before we start the protocol. */
1058 server->stat.auth_attempts++;
1059 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1060 &sock->protocol, proto_ctx,
1061 silc_server_accept_new_connection_second);
1063 /* Register a timeout task that will be executed if the connector
1064 will not start the key exchange protocol within 60 seconds. For
1065 now, this is a hard coded limit. After 60 secs the connection will
1066 be closed if the key exchange protocol has not been started. */
1067 proto_ctx->timeout_task =
1068 silc_schedule_task_add(server->schedule, sock->sock,
1069 silc_server_timeout_remote,
1075 /* Accepts new connections to the server. Accepting new connections are
1076 done in three parts to make it async. */
1078 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
1080 SilcServer server = (SilcServer)context;
1081 SilcSocketConnection newsocket;
1084 SILC_LOG_DEBUG(("Accepting new connection"));
1086 server->stat.conn_attempts++;
1088 sock = silc_net_accept_connection(server->sock);
1090 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
1091 server->stat.conn_failures++;
1095 /* Check max connections */
1096 if (sock > SILC_SERVER_MAX_CONNECTIONS) {
1097 SILC_LOG_ERROR(("Refusing connection, server is full"));
1098 server->stat.conn_failures++;
1102 /* Set socket options */
1103 silc_net_set_socket_nonblock(sock);
1104 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
1106 /* We don't create a ID yet, since we don't know what type of connection
1107 this is yet. But, we do add the connection to the socket table. */
1108 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
1109 server->sockets[sock] = newsocket;
1111 /* Perform asynchronous host lookup. This will lookup the IP and the
1112 FQDN of the remote connection. After the lookup is done the connection
1113 is accepted further. */
1114 silc_socket_host_lookup(newsocket, TRUE,
1115 silc_server_accept_new_connection_lookup, context,
1119 /* Second part of accepting new connection. Key exchange protocol has been
1120 performed and now it is time to do little connection authentication
1121 protocol to figure out whether this connection is client or server
1122 and whether it has right to access this server (especially server
1123 connections needs to be authenticated). */
1125 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
1127 SilcProtocol protocol = (SilcProtocol)context;
1128 SilcServerKEInternalContext *ctx =
1129 (SilcServerKEInternalContext *)protocol->context;
1130 SilcServer server = (SilcServer)ctx->server;
1131 SilcSocketConnection sock = ctx->sock;
1132 SilcServerConnAuthInternalContext *proto_ctx;
1134 SILC_LOG_DEBUG(("Start"));
1136 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1137 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1138 /* Error occured during protocol */
1139 silc_protocol_free(protocol);
1140 sock->protocol = NULL;
1141 silc_ske_free_key_material(ctx->keymat);
1143 silc_packet_context_free(ctx->packet);
1145 silc_ske_free(ctx->ske);
1146 silc_free(ctx->dest_id);
1148 silc_schedule_task_del_by_callback(server->schedule,
1149 silc_server_failure_callback);
1150 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1151 "Key exchange failed");
1152 server->stat.auth_failures++;
1156 /* We now have the key material as the result of the key exchange
1157 protocol. Take the key material into use. Free the raw key material
1158 as soon as we've set them into use. */
1159 if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
1160 ctx->ske->prop->cipher,
1161 ctx->ske->prop->pkcs,
1162 ctx->ske->prop->hash,
1163 ctx->ske->prop->hmac,
1164 ctx->ske->prop->group,
1166 silc_protocol_free(protocol);
1167 sock->protocol = NULL;
1168 silc_ske_free_key_material(ctx->keymat);
1170 silc_packet_context_free(ctx->packet);
1172 silc_ske_free(ctx->ske);
1173 silc_free(ctx->dest_id);
1175 silc_schedule_task_del_by_callback(server->schedule,
1176 silc_server_failure_callback);
1177 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1178 "Key exchange failed");
1179 server->stat.auth_failures++;
1182 silc_ske_free_key_material(ctx->keymat);
1184 /* Allocate internal context for the authentication protocol. This
1185 is sent as context for the protocol. */
1186 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1187 proto_ctx->server = (void *)server;
1188 proto_ctx->sock = sock;
1189 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
1190 proto_ctx->responder = TRUE;
1191 proto_ctx->dest_id_type = ctx->dest_id_type;
1192 proto_ctx->dest_id = ctx->dest_id;
1193 proto_ctx->cconfig = ctx->cconfig;
1194 proto_ctx->sconfig = ctx->sconfig;
1195 proto_ctx->rconfig = ctx->rconfig;
1197 /* Free old protocol as it is finished now */
1198 silc_protocol_free(protocol);
1200 silc_packet_context_free(ctx->packet);
1202 sock->protocol = NULL;
1204 /* Allocate the authentication protocol. This is allocated here
1205 but we won't start it yet. We will be receiving party of this
1206 protocol thus we will wait that connecting party will make
1207 their first move. */
1208 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1209 &sock->protocol, proto_ctx,
1210 silc_server_accept_new_connection_final);
1212 /* Register timeout task. If the protocol is not executed inside
1213 this timelimit the connection will be terminated. Currently
1214 this is 60 seconds and is hard coded limit (XXX). */
1215 proto_ctx->timeout_task =
1216 silc_schedule_task_add(server->schedule, sock->sock,
1217 silc_server_timeout_remote,
1218 (void *)server, 60, 0,
1223 /* Final part of accepting new connection. The connection has now
1224 been authenticated and keys has been exchanged. We also know whether
1225 this is client or server connection. */
1227 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
1229 SilcProtocol protocol = (SilcProtocol)context;
1230 SilcServerConnAuthInternalContext *ctx =
1231 (SilcServerConnAuthInternalContext *)protocol->context;
1232 SilcServer server = (SilcServer)ctx->server;
1233 SilcSocketConnection sock = ctx->sock;
1234 SilcServerHBContext hb_context;
1235 SilcUnknownEntry entry = (SilcUnknownEntry)sock->user_data;
1236 void *id_entry = NULL;
1238 SILC_LOG_DEBUG(("Start"));
1240 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1241 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1242 /* Error occured during protocol */
1243 silc_protocol_free(protocol);
1244 sock->protocol = NULL;
1246 silc_packet_context_free(ctx->packet);
1248 silc_ske_free(ctx->ske);
1249 silc_free(ctx->dest_id);
1252 sock->protocol = NULL;
1253 silc_schedule_task_del_by_callback(server->schedule,
1254 silc_server_failure_callback);
1255 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1256 "Authentication failed");
1257 server->stat.auth_failures++;
1261 entry->data.last_receive = time(NULL);
1263 switch (ctx->conn_type) {
1264 case SILC_SOCKET_TYPE_CLIENT:
1266 SilcClientEntry client;
1268 SILC_LOG_DEBUG(("Remote host is client"));
1269 SILC_LOG_INFO(("Connection from %s (%s) is client", sock->hostname,
1272 /* Add the client to the client ID cache. The nickname and Client ID
1273 and other information is created after we have received NEW_CLIENT
1274 packet from client. */
1275 client = silc_idlist_add_client(server->local_list,
1276 NULL, NULL, NULL, NULL, NULL, sock);
1278 SILC_LOG_ERROR(("Could not add new client to cache"));
1279 silc_free(sock->user_data);
1280 silc_server_disconnect_remote(server, sock,
1281 "Server closed connection: "
1282 "Authentication failed");
1283 server->stat.auth_failures++;
1288 server->stat.my_clients++;
1289 server->stat.clients++;
1290 if (server->server_type == SILC_ROUTER)
1291 server->stat.cell_clients++;
1293 id_entry = (void *)client;
1296 case SILC_SOCKET_TYPE_SERVER:
1297 case SILC_SOCKET_TYPE_ROUTER:
1299 SilcServerEntry new_server;
1300 SilcServerConfigSectionServerConnection *conn =
1301 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1302 ctx->sconfig : ctx->rconfig;
1304 SILC_LOG_DEBUG(("Remote host is %s",
1305 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1306 "server" : (conn->backup_router ?
1307 "backup router" : "router")));
1308 SILC_LOG_INFO(("Connection from %s (%s) is %s", sock->hostname,
1309 sock->ip, ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1310 "server" : (conn->backup_router ?
1311 "backup router" : "router")));
1313 /* Add the server into server cache. The server name and Server ID
1314 is updated after we have received NEW_SERVER packet from the
1315 server. We mark ourselves as router for this server if we really
1318 silc_idlist_add_server(server->local_list, NULL,
1319 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1320 SILC_SERVER : SILC_ROUTER, NULL,
1321 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1323 (conn->backup_router ? server->id_entry :
1326 SILC_LOG_ERROR(("Could not add new server to cache"));
1327 silc_free(sock->user_data);
1328 silc_server_disconnect_remote(server, sock,
1329 "Server closed connection: "
1330 "Authentication failed");
1331 server->stat.auth_failures++;
1336 if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER)
1337 server->stat.my_servers++;
1339 server->stat.my_routers++;
1340 server->stat.servers++;
1342 id_entry = (void *)new_server;
1344 /* If the incoming connection is router and marked as backup router
1345 then add it to be one of our backups */
1346 if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER && conn->backup_router) {
1347 silc_server_backup_add(server, new_server, conn->backup_local);
1349 /* Change it back to SERVER type since that's what it really is. */
1350 if (conn->backup_local) {
1351 ctx->conn_type = SILC_SOCKET_TYPE_SERVER;
1352 new_server->server_type = SILC_BACKUP_ROUTER;
1356 /* Check whether this connection is to be our primary router connection
1357 if we do not already have the primary route. */
1358 if (server->standalone && ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1359 if (silc_server_config_is_primary_route(server->config) &&
1363 SILC_LOG_DEBUG(("We are not standalone server anymore"));
1364 server->standalone = FALSE;
1365 if (!server->id_entry->router) {
1366 server->id_entry->router = id_entry;
1367 server->router = id_entry;
1377 sock->type = ctx->conn_type;
1379 /* Add the common data structure to the ID entry. */
1381 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1383 /* Add to sockets internal pointer for fast referencing */
1384 silc_free(sock->user_data);
1385 sock->user_data = id_entry;
1387 /* Connection has been fully established now. Everything is ok. */
1388 SILC_LOG_DEBUG(("New connection authenticated"));
1390 /* Perform keepalive. The `hb_context' will be freed automatically
1391 when finally calling the silc_socket_free function. XXX hardcoded
1393 hb_context = silc_calloc(1, sizeof(*hb_context));
1394 hb_context->server = server;
1395 silc_socket_set_heartbeat(sock, 600, hb_context,
1396 silc_server_perform_heartbeat,
1400 silc_schedule_task_del_by_callback(server->schedule,
1401 silc_server_failure_callback);
1402 silc_protocol_free(protocol);
1404 silc_packet_context_free(ctx->packet);
1406 silc_ske_free(ctx->ske);
1407 silc_free(ctx->dest_id);
1409 sock->protocol = NULL;
1412 /* This function is used to read packets from network and send packets to
1413 network. This is usually a generic task. */
1415 SILC_TASK_CALLBACK(silc_server_packet_process)
1417 SilcServer server = (SilcServer)context;
1418 SilcSocketConnection sock = server->sockets[fd];
1419 SilcIDListData idata;
1420 SilcCipher cipher = NULL;
1421 SilcHmac hmac = NULL;
1427 SILC_LOG_DEBUG(("Processing packet"));
1429 /* Packet sending */
1431 if (type == SILC_TASK_WRITE) {
1432 /* Do not send data to disconnected connection */
1433 if (SILC_IS_DISCONNECTED(sock))
1436 server->stat.packets_sent++;
1438 if (sock->outbuf->data - sock->outbuf->head)
1439 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
1441 /* Send the packet */
1442 ret = silc_packet_send(sock, TRUE);
1444 /* If returned -2 could not write to connection now, will do
1450 SILC_LOG_ERROR(("Error sending packet to connection "
1451 "%s:%d [%s]", sock->hostname, sock->port,
1452 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1453 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1454 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1459 /* The packet has been sent and now it is time to set the connection
1460 back to only for input. When there is again some outgoing data
1461 available for this connection it will be set for output as well.
1462 This call clears the output setting and sets it only for input. */
1463 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, fd);
1464 SILC_UNSET_OUTBUF_PENDING(sock);
1466 silc_buffer_clear(sock->outbuf);
1470 /* Packet receiving */
1472 /* Read some data from connection */
1473 ret = silc_packet_receive(sock);
1477 SILC_LOG_ERROR(("Error receiving packet from connection "
1478 "%s:%d [%s]", sock->hostname, sock->port,
1479 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1480 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1481 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1488 SILC_LOG_DEBUG(("Read EOF"));
1490 /* If connection is disconnecting already we will finally
1491 close the connection */
1492 if (SILC_IS_DISCONNECTING(sock)) {
1493 if (sock->user_data)
1494 silc_server_free_sock_user_data(server, sock);
1495 silc_server_close_connection(server, sock);
1499 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1500 SILC_SET_DISCONNECTING(sock);
1502 if (sock->user_data)
1503 silc_server_free_sock_user_data(server, sock);
1504 silc_server_close_connection(server, sock);
1508 /* If connection is disconnecting or disconnected we will ignore
1510 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1511 SILC_LOG_DEBUG(("Ignoring read data from disonnected connection"));
1515 server->stat.packets_received++;
1517 /* Get keys and stuff from ID entry */
1518 idata = (SilcIDListData)sock->user_data;
1520 cipher = idata->receive_key;
1521 hmac = idata->hmac_receive;
1524 /* Process the packet. This will call the parser that will then
1525 decrypt and parse the packet. */
1526 silc_packet_receive_process(sock, cipher, hmac, silc_server_packet_parse,
1530 /* Callback function that the silc_packet_decrypt will call to make the
1531 decision whether the packet is normal or special packet. We will
1532 return TRUE if it is normal and FALSE if it is special */
1534 static int silc_server_packet_decrypt_check(SilcPacketType packet_type,
1536 SilcPacketContext *packet,
1539 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1540 SilcServer server = (SilcServer)parse_ctx->context;
1542 /* Packet is normal packet, if:
1544 1) packet is private message packet and does not have private key set
1545 2) is other packet than channel message packet
1546 3) is channel message packet and remote is router and we are router
1548 all other packets are special packets
1551 if (packet_type == SILC_PACKET_PRIVATE_MESSAGE &&
1552 (buffer->data[2] & SILC_PACKET_FLAG_PRIVMSG_KEY))
1555 if (packet_type != SILC_PACKET_CHANNEL_MESSAGE ||
1556 (packet_type == SILC_PACKET_CHANNEL_MESSAGE &&
1557 parse_ctx->sock->type == SILC_SOCKET_TYPE_ROUTER &&
1558 server->server_type == SILC_ROUTER))
1564 /* Parses whole packet, received earlier. */
1566 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1568 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1569 SilcServer server = (SilcServer)parse_ctx->context;
1570 SilcSocketConnection sock = parse_ctx->sock;
1571 SilcPacketContext *packet = parse_ctx->packet;
1572 SilcIDListData idata = (SilcIDListData)sock->user_data;
1575 SILC_LOG_DEBUG(("Start"));
1577 /* Decrypt the received packet */
1578 ret = silc_packet_decrypt(idata ? idata->receive_key : NULL,
1579 idata ? idata->hmac_receive : NULL,
1580 packet->buffer, packet,
1581 silc_server_packet_decrypt_check, parse_ctx);
1583 SILC_LOG_WARNING(("Packet decryption failed for connection "
1584 "%s:%d [%s]", sock->hostname, sock->port,
1585 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1586 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1587 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1593 /* Parse the packet. Packet type is returned. */
1594 ret = silc_packet_parse(packet);
1596 /* Parse the packet header in special way as this is "special"
1598 ret = silc_packet_parse_special(packet);
1601 /* If entry is disabled ignore what we got. */
1602 if (ret != SILC_PACKET_RESUME_ROUTER &&
1603 idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
1606 if (ret == SILC_PACKET_NONE)
1609 /* Check that the the current client ID is same as in the client's packet. */
1610 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
1611 SilcClientEntry client = (SilcClientEntry)sock->user_data;
1612 if (client && client->id) {
1613 void *id = silc_id_str2id(packet->src_id, packet->src_id_len,
1614 packet->src_id_type);
1615 if (!id || !SILC_ID_CLIENT_COMPARE(client->id, id)) {
1623 if (server->server_type == SILC_ROUTER) {
1624 /* Route the packet if it is not destined to us. Other ID types but
1625 server are handled separately after processing them. */
1626 if (!(packet->flags & SILC_PACKET_FLAG_BROADCAST) &&
1627 packet->dst_id_type == SILC_ID_SERVER &&
1628 sock->type != SILC_SOCKET_TYPE_CLIENT &&
1629 memcmp(packet->dst_id, server->id_string, packet->dst_id_len)) {
1631 /* Route the packet to fastest route for the destination ID */
1632 void *id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1633 packet->dst_id_type);
1636 silc_server_packet_route(server,
1637 silc_server_route_get(server, id,
1638 packet->dst_id_type),
1645 /* Parse the incoming packet type */
1646 silc_server_packet_parse_type(server, sock, packet);
1648 if (server->server_type == SILC_ROUTER) {
1649 /* Broadcast packet if it is marked as broadcast packet and it is
1650 originated from router and we are router. */
1651 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1652 packet->flags & SILC_PACKET_FLAG_BROADCAST &&
1653 !server->standalone) {
1654 /* Broadcast to our primary route */
1655 silc_server_packet_broadcast(server, server->router->connection, packet);
1657 /* If we have backup routers then we need to feed all broadcast
1658 data to those servers. */
1659 silc_server_backup_broadcast(server, sock, packet);
1664 silc_packet_context_free(packet);
1665 silc_free(parse_ctx);
1668 /* Parser callback called by silc_packet_receive_process. This merely
1669 registers timeout that will handle the actual parsing when appropriate. */
1671 void silc_server_packet_parse(SilcPacketParserContext *parser_context)
1673 SilcServer server = (SilcServer)parser_context->context;
1674 SilcSocketConnection sock = parser_context->sock;
1676 switch (sock->type) {
1677 case SILC_SOCKET_TYPE_UNKNOWN:
1678 case SILC_SOCKET_TYPE_CLIENT:
1679 /* Parse the packet with timeout */
1680 silc_schedule_task_add(server->schedule, sock->sock,
1681 silc_server_packet_parse_real,
1682 (void *)parser_context, 0, 100000,
1684 SILC_TASK_PRI_NORMAL);
1686 case SILC_SOCKET_TYPE_SERVER:
1687 case SILC_SOCKET_TYPE_ROUTER:
1688 /* Packets from servers are parsed as soon as possible */
1689 silc_schedule_task_add(server->schedule, sock->sock,
1690 silc_server_packet_parse_real,
1691 (void *)parser_context, 0, 1,
1693 SILC_TASK_PRI_NORMAL);
1700 /* Parses the packet type and calls what ever routines the packet type
1701 requires. This is done for all incoming packets. */
1703 void silc_server_packet_parse_type(SilcServer server,
1704 SilcSocketConnection sock,
1705 SilcPacketContext *packet)
1707 SilcPacketType type = packet->type;
1708 SilcIDListData idata = (SilcIDListData)sock->user_data;
1710 SILC_LOG_DEBUG(("Parsing packet type %d", type));
1712 /* Parse the packet type */
1714 case SILC_PACKET_DISCONNECT:
1715 SILC_LOG_DEBUG(("Disconnect packet"));
1716 if (packet->flags & SILC_PACKET_FLAG_LIST)
1720 case SILC_PACKET_SUCCESS:
1722 * Success received for something. For now we can have only
1723 * one protocol for connection executing at once hence this
1724 * success message is for whatever protocol is executing currently.
1726 SILC_LOG_DEBUG(("Success packet"));
1727 if (packet->flags & SILC_PACKET_FLAG_LIST)
1730 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1733 case SILC_PACKET_FAILURE:
1735 * Failure received for something. For now we can have only
1736 * one protocol for connection executing at once hence this
1737 * failure message is for whatever protocol is executing currently.
1739 SILC_LOG_DEBUG(("Failure packet"));
1740 if (packet->flags & SILC_PACKET_FLAG_LIST)
1742 if (sock->protocol) {
1743 SilcServerFailureContext f;
1744 f = silc_calloc(1, sizeof(*f));
1748 /* We will wait 5 seconds to process this failure packet */
1749 silc_schedule_task_add(server->schedule, sock->sock,
1750 silc_server_failure_callback, (void *)f, 5, 0,
1751 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1755 case SILC_PACKET_REJECT:
1756 SILC_LOG_DEBUG(("Reject packet"));
1757 if (packet->flags & SILC_PACKET_FLAG_LIST)
1762 case SILC_PACKET_NOTIFY:
1764 * Received notify packet. Server can receive notify packets from
1765 * router. Server then relays the notify messages to clients if needed.
1767 SILC_LOG_DEBUG(("Notify packet"));
1768 if (packet->flags & SILC_PACKET_FLAG_LIST)
1769 silc_server_notify_list(server, sock, packet);
1771 silc_server_notify(server, sock, packet);
1777 case SILC_PACKET_CHANNEL_MESSAGE:
1779 * Received channel message. Channel messages are special packets
1780 * (although probably most common ones) thus they are handled
1783 SILC_LOG_DEBUG(("Channel Message packet"));
1784 if (packet->flags & SILC_PACKET_FLAG_LIST)
1786 idata->last_receive = time(NULL);
1787 silc_server_channel_message(server, sock, packet);
1790 case SILC_PACKET_CHANNEL_KEY:
1792 * Received key for channel. As channels are created by the router
1793 * the keys are as well. We will distribute the key to all of our
1794 * locally connected clients on the particular channel. Router
1795 * never receives this channel and thus is ignored.
1797 SILC_LOG_DEBUG(("Channel Key packet"));
1798 if (packet->flags & SILC_PACKET_FLAG_LIST)
1800 silc_server_channel_key(server, sock, packet);
1806 case SILC_PACKET_COMMAND:
1808 * Recived command. Processes the command request and allocates the
1809 * command context and calls the command.
1811 SILC_LOG_DEBUG(("Command packet"));
1812 if (packet->flags & SILC_PACKET_FLAG_LIST)
1814 silc_server_command_process(server, sock, packet);
1817 case SILC_PACKET_COMMAND_REPLY:
1819 * Received command reply packet. Received command reply to command. It
1820 * may be reply to command sent by us or reply to command sent by client
1821 * that we've routed further.
1823 SILC_LOG_DEBUG(("Command Reply packet"));
1824 if (packet->flags & SILC_PACKET_FLAG_LIST)
1826 silc_server_command_reply(server, sock, packet);
1830 * Private Message packets
1832 case SILC_PACKET_PRIVATE_MESSAGE:
1834 * Received private message packet. The packet is coming from either
1837 SILC_LOG_DEBUG(("Private Message packet"));
1838 if (packet->flags & SILC_PACKET_FLAG_LIST)
1840 idata->last_receive = time(NULL);
1841 silc_server_private_message(server, sock, packet);
1844 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1846 * Private message key packet.
1848 if (packet->flags & SILC_PACKET_FLAG_LIST)
1850 silc_server_private_message_key(server, sock, packet);
1854 * Key Exchange protocol packets
1856 case SILC_PACKET_KEY_EXCHANGE:
1857 SILC_LOG_DEBUG(("KE packet"));
1858 if (packet->flags & SILC_PACKET_FLAG_LIST)
1861 if (sock->protocol && sock->protocol->protocol &&
1862 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1864 SilcServerKEInternalContext *proto_ctx =
1865 (SilcServerKEInternalContext *)sock->protocol->context;
1867 proto_ctx->packet = silc_packet_context_dup(packet);
1869 /* Let the protocol handle the packet */
1870 silc_protocol_execute(sock->protocol, server->schedule, 0, 100000);
1872 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1873 "protocol active, packet dropped."));
1877 case SILC_PACKET_KEY_EXCHANGE_1:
1878 SILC_LOG_DEBUG(("KE 1 packet"));
1879 if (packet->flags & SILC_PACKET_FLAG_LIST)
1882 if (sock->protocol && sock->protocol->protocol &&
1883 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1884 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1886 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
1887 SilcServerRekeyInternalContext *proto_ctx =
1888 (SilcServerRekeyInternalContext *)sock->protocol->context;
1890 if (proto_ctx->packet)
1891 silc_packet_context_free(proto_ctx->packet);
1893 proto_ctx->packet = silc_packet_context_dup(packet);
1895 /* Let the protocol handle the packet */
1896 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1898 SilcServerKEInternalContext *proto_ctx =
1899 (SilcServerKEInternalContext *)sock->protocol->context;
1901 if (proto_ctx->packet)
1902 silc_packet_context_free(proto_ctx->packet);
1904 proto_ctx->packet = silc_packet_context_dup(packet);
1905 proto_ctx->dest_id_type = packet->src_id_type;
1906 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1907 packet->src_id_type);
1908 if (!proto_ctx->dest_id)
1911 /* Let the protocol handle the packet */
1912 silc_protocol_execute(sock->protocol, server->schedule,
1916 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1917 "protocol active, packet dropped."));
1921 case SILC_PACKET_KEY_EXCHANGE_2:
1922 SILC_LOG_DEBUG(("KE 2 packet"));
1923 if (packet->flags & SILC_PACKET_FLAG_LIST)
1926 if (sock->protocol && sock->protocol->protocol &&
1927 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1928 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1930 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
1931 SilcServerRekeyInternalContext *proto_ctx =
1932 (SilcServerRekeyInternalContext *)sock->protocol->context;
1934 if (proto_ctx->packet)
1935 silc_packet_context_free(proto_ctx->packet);
1937 proto_ctx->packet = silc_packet_context_dup(packet);
1939 /* Let the protocol handle the packet */
1940 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1942 SilcServerKEInternalContext *proto_ctx =
1943 (SilcServerKEInternalContext *)sock->protocol->context;
1945 if (proto_ctx->packet)
1946 silc_packet_context_free(proto_ctx->packet);
1948 proto_ctx->packet = silc_packet_context_dup(packet);
1949 proto_ctx->dest_id_type = packet->src_id_type;
1950 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1951 packet->src_id_type);
1952 if (!proto_ctx->dest_id)
1955 /* Let the protocol handle the packet */
1956 silc_protocol_execute(sock->protocol, server->schedule,
1960 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1961 "protocol active, packet dropped."));
1965 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1967 * Connection authentication request packet. When we receive this packet
1968 * we will send to the other end information about our mandatory
1969 * authentication method for the connection. This packet maybe received
1972 SILC_LOG_DEBUG(("Connection authentication request packet"));
1973 if (packet->flags & SILC_PACKET_FLAG_LIST)
1975 silc_server_connection_auth_request(server, sock, packet);
1979 * Connection Authentication protocol packets
1981 case SILC_PACKET_CONNECTION_AUTH:
1982 /* Start of the authentication protocol. We receive here the
1983 authentication data and will verify it. */
1984 SILC_LOG_DEBUG(("Connection auth packet"));
1985 if (packet->flags & SILC_PACKET_FLAG_LIST)
1988 if (sock->protocol && sock->protocol->protocol->type
1989 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
1991 SilcServerConnAuthInternalContext *proto_ctx =
1992 (SilcServerConnAuthInternalContext *)sock->protocol->context;
1994 proto_ctx->packet = silc_packet_context_dup(packet);
1996 /* Let the protocol handle the packet */
1997 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1999 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
2000 "protocol active, packet dropped."));
2004 case SILC_PACKET_NEW_ID:
2006 * Received New ID packet. This includes some new ID that has been
2007 * created. It may be for client, server or channel. This is the way
2008 * to distribute information about new registered entities in the
2011 SILC_LOG_DEBUG(("New ID packet"));
2012 if (packet->flags & SILC_PACKET_FLAG_LIST)
2013 silc_server_new_id_list(server, sock, packet);
2015 silc_server_new_id(server, sock, packet);
2018 case SILC_PACKET_NEW_CLIENT:
2020 * Received new client packet. This includes client information that
2021 * we will use to create initial client ID. After creating new
2022 * ID we will send it to the client.
2024 SILC_LOG_DEBUG(("New Client packet"));
2025 if (packet->flags & SILC_PACKET_FLAG_LIST)
2027 silc_server_new_client(server, sock, packet);
2030 case SILC_PACKET_NEW_SERVER:
2032 * Received new server packet. This includes Server ID and some other
2033 * information that we may save. This is received after server has
2036 SILC_LOG_DEBUG(("New Server packet"));
2037 if (packet->flags & SILC_PACKET_FLAG_LIST)
2039 silc_server_new_server(server, sock, packet);
2042 case SILC_PACKET_NEW_CHANNEL:
2044 * Received new channel packet. Information about new channel in the
2045 * network are distributed using this packet.
2047 SILC_LOG_DEBUG(("New Channel packet"));
2048 if (packet->flags & SILC_PACKET_FLAG_LIST)
2049 silc_server_new_channel_list(server, sock, packet);
2051 silc_server_new_channel(server, sock, packet);
2054 case SILC_PACKET_HEARTBEAT:
2056 * Received heartbeat.
2058 SILC_LOG_DEBUG(("Heartbeat packet"));
2059 if (packet->flags & SILC_PACKET_FLAG_LIST)
2063 case SILC_PACKET_KEY_AGREEMENT:
2065 * Received heartbeat.
2067 SILC_LOG_DEBUG(("Key agreement packet"));
2068 if (packet->flags & SILC_PACKET_FLAG_LIST)
2070 silc_server_key_agreement(server, sock, packet);
2073 case SILC_PACKET_REKEY:
2075 * Received re-key packet. The sender wants to regenerate the session
2078 SILC_LOG_DEBUG(("Re-key packet"));
2079 if (packet->flags & SILC_PACKET_FLAG_LIST)
2081 silc_server_rekey(server, sock, packet);
2084 case SILC_PACKET_REKEY_DONE:
2086 * The re-key is done.
2088 SILC_LOG_DEBUG(("Re-key done packet"));
2089 if (packet->flags & SILC_PACKET_FLAG_LIST)
2092 if (sock->protocol && sock->protocol->protocol &&
2093 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2095 SilcServerRekeyInternalContext *proto_ctx =
2096 (SilcServerRekeyInternalContext *)sock->protocol->context;
2098 if (proto_ctx->packet)
2099 silc_packet_context_free(proto_ctx->packet);
2101 proto_ctx->packet = silc_packet_context_dup(packet);
2103 /* Let the protocol handle the packet */
2104 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2106 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
2107 "protocol active, packet dropped."));
2111 case SILC_PACKET_FTP:
2115 case SILC_PACKET_RESUME_ROUTER:
2116 /* Resume router packet received. This packet is received for backup
2117 router resuming protocol. */
2118 SILC_LOG_DEBUG(("Resume router packet"));
2119 if (packet->flags & SILC_PACKET_FLAG_LIST)
2121 silc_server_backup_resume_router(server, sock, packet);
2125 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
2131 /* Creates connection to a remote router. */
2133 void silc_server_create_connection(SilcServer server,
2134 char *remote_host, uint32 port)
2136 SilcServerConnection sconn;
2138 /* Allocate connection object for hold connection specific stuff. */
2139 sconn = silc_calloc(1, sizeof(*sconn));
2140 sconn->server = server;
2141 sconn->remote_host = strdup(remote_host);
2142 sconn->remote_port = port;
2144 silc_schedule_task_add(server->schedule, 0,
2145 silc_server_connect_router,
2146 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
2147 SILC_TASK_PRI_NORMAL);
2150 SILC_TASK_CALLBACK(silc_server_close_connection_final)
2152 silc_socket_free((SilcSocketConnection)context);
2155 /* Closes connection to socket connection */
2157 void silc_server_close_connection(SilcServer server,
2158 SilcSocketConnection sock)
2160 SILC_LOG_INFO(("Closing connection %s:%d [%s]", sock->hostname,
2162 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2163 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2164 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2167 /* We won't listen for this connection anymore */
2168 silc_schedule_unset_listen_fd(server->schedule, sock->sock);
2170 /* Unregister all tasks */
2171 silc_schedule_task_del_by_fd(server->schedule, sock->sock);
2173 /* Close the actual connection */
2174 silc_net_close_connection(sock->sock);
2175 server->sockets[sock->sock] = NULL;
2177 /* If sock->user_data is NULL then we'll check for active protocols
2178 here since the silc_server_free_sock_user_data has not been called
2179 for this connection. */
2180 if (!sock->user_data) {
2181 /* If any protocol is active cancel its execution. It will call
2182 the final callback which will finalize the disconnection. */
2183 if (sock->protocol) {
2184 silc_protocol_cancel(sock->protocol, server->schedule);
2185 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2186 silc_protocol_execute_final(sock->protocol, server->schedule);
2187 sock->protocol = NULL;
2192 silc_schedule_task_add(server->schedule, 0,
2193 silc_server_close_connection_final,
2194 (void *)sock, 0, 1, SILC_TASK_TIMEOUT,
2195 SILC_TASK_PRI_NORMAL);
2198 /* Sends disconnect message to remote connection and disconnects the
2201 void silc_server_disconnect_remote(SilcServer server,
2202 SilcSocketConnection sock,
2203 const char *fmt, ...)
2206 unsigned char buf[4096];
2211 memset(buf, 0, sizeof(buf));
2213 vsprintf(buf, fmt, ap);
2216 SILC_LOG_DEBUG(("Disconnecting remote host"));
2218 /* Notify remote end that the conversation is over. The notify message
2219 is tried to be sent immediately. */
2220 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2221 buf, strlen(buf), TRUE);
2223 /* Mark the connection to be disconnected */
2224 SILC_SET_DISCONNECTED(sock);
2225 silc_server_close_connection(server, sock);
2230 SilcClientEntry client;
2231 } *FreeClientInternal;
2233 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout)
2235 FreeClientInternal i = (FreeClientInternal)context;
2237 silc_idlist_del_data(i->client);
2238 silc_idcache_purge_by_context(i->server->local_list->clients, i->client);
2242 /* Frees client data and notifies about client's signoff. */
2244 void silc_server_free_client_data(SilcServer server,
2245 SilcSocketConnection sock,
2246 SilcClientEntry client,
2250 FreeClientInternal i = silc_calloc(1, sizeof(*i));
2252 /* If there is pending outgoing data for the client then purge it
2253 to the network before removing the client entry. */
2254 silc_server_packet_queue_purge(server, sock);
2256 /* Send SIGNOFF notify to routers. */
2257 if (notify && !server->standalone && server->router)
2258 silc_server_send_notify_signoff(server, server->router->connection,
2259 server->server_type == SILC_SERVER ?
2260 FALSE : TRUE, client->id, signoff);
2262 /* Remove client from all channels */
2264 silc_server_remove_from_channels(server, NULL, client,
2265 TRUE, signoff, TRUE);
2267 silc_server_remove_from_channels(server, NULL, client,
2268 FALSE, NULL, FALSE);
2270 /* We will not delete the client entry right away. We will take it
2271 into history (for WHOWAS command) for 5 minutes */
2274 silc_schedule_task_add(server->schedule, 0,
2275 silc_server_free_client_data_timeout,
2277 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2278 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
2279 client->router = NULL;
2280 client->connection = NULL;
2282 /* Free the client entry and everything in it */
2283 server->stat.my_clients--;
2284 server->stat.clients--;
2285 if (server->server_type == SILC_ROUTER)
2286 server->stat.cell_clients--;
2289 /* Frees user_data pointer from socket connection object. This also sends
2290 appropriate notify packets to the network to inform about leaving
2293 void silc_server_free_sock_user_data(SilcServer server,
2294 SilcSocketConnection sock)
2296 SILC_LOG_DEBUG(("Start"));
2298 switch (sock->type) {
2299 case SILC_SOCKET_TYPE_CLIENT:
2301 SilcClientEntry user_data = (SilcClientEntry)sock->user_data;
2302 silc_server_free_client_data(server, sock, user_data, TRUE, NULL);
2305 case SILC_SOCKET_TYPE_SERVER:
2306 case SILC_SOCKET_TYPE_ROUTER:
2308 SilcServerEntry user_data = (SilcServerEntry)sock->user_data;
2309 SilcServerEntry backup_router = NULL;
2311 /* If this was our primary router connection then we're lost to
2312 the outside world. */
2313 if (server->router == user_data) {
2314 backup_router = silc_server_backup_get(server);
2316 /* Check whether we have a backup router connection */
2317 if (!backup_router || backup_router == user_data) {
2318 silc_schedule_task_add(server->schedule, 0,
2319 silc_server_connect_to_router,
2322 SILC_TASK_PRI_NORMAL);
2324 server->id_entry->router = NULL;
2325 server->router = NULL;
2326 server->standalone = TRUE;
2327 backup_router = NULL;
2329 SILC_LOG_INFO(("New primary router is backup router %s",
2330 backup_router->server_name));
2331 SILC_LOG_DEBUG(("New primary router is backup router %s",
2332 backup_router->server_name));
2333 server->id_entry->router = backup_router;
2334 server->router = backup_router;
2335 server->router_connect = time(0);
2336 if (server->server_type == SILC_BACKUP_ROUTER) {
2337 server->server_type = SILC_ROUTER;
2339 /* We'll need to constantly try to reconnect to the primary
2340 router so that we'll see when it comes back online. */
2341 silc_server_backup_reconnect(server, sock->ip, sock->port,
2342 silc_server_backup_connected,
2346 /* Mark this connection as replaced */
2347 silc_server_backup_replaced_add(server, user_data->id,
2352 if (!backup_router) {
2353 /* Free all client entries that this server owns as they will
2354 become invalid now as well. */
2356 silc_server_remove_clients_by_server(server, user_data, TRUE);
2358 /* Update the client entries of this server to the new backup
2359 router. This also removes the clients that *really* was owned
2360 by the primary router and went down with the router. */
2361 silc_server_update_clients_by_server(server, user_data, backup_router,
2365 /* Free the server entry */
2366 silc_server_backup_del(server, user_data);
2367 silc_server_backup_replaced_del(server, user_data);
2368 silc_idlist_del_data(user_data);
2369 silc_idlist_del_server(server->local_list, user_data);
2370 server->stat.my_servers--;
2371 server->stat.servers--;
2372 if (server->server_type == SILC_ROUTER)
2373 server->stat.cell_servers--;
2375 if (backup_router) {
2376 /* Announce all of our stuff that was created about 5 minutes ago.
2377 The backup router knows all the other stuff already. */
2378 if (server->server_type == SILC_ROUTER)
2379 silc_server_announce_servers(server, FALSE, time(0) - 300,
2380 server->router->connection);
2382 /* Announce our clients and channels to the router */
2383 silc_server_announce_clients(server, time(0) - 300,
2384 server->router->connection);
2385 silc_server_announce_channels(server, time(0) - 300,
2386 server->router->connection);
2392 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2394 silc_idlist_del_data(user_data);
2395 silc_free(user_data);
2400 /* If any protocol is active cancel its execution */
2401 if (sock->protocol) {
2402 silc_protocol_cancel(sock->protocol, server->schedule);
2403 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2404 silc_protocol_execute_final(sock->protocol, server->schedule);
2405 sock->protocol = NULL;
2408 sock->user_data = NULL;
2411 /* Removes client from all channels it has joined. This is used when client
2412 connection is disconnected. If the client on a channel is last, the
2413 channel is removed as well. This sends the SIGNOFF notify types. */
2415 void silc_server_remove_from_channels(SilcServer server,
2416 SilcSocketConnection sock,
2417 SilcClientEntry client,
2419 char *signoff_message,
2422 SilcChannelEntry channel;
2423 SilcChannelClientEntry chl;
2424 SilcHashTableList htl;
2427 SILC_LOG_DEBUG(("Start"));
2429 if (!client || !client->id)
2432 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2434 /* Remove the client from all channels. The client is removed from
2435 the channels' user list. */
2436 silc_hash_table_list(client->channels, &htl);
2437 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
2438 channel = chl->channel;
2440 /* Remove channel from client's channel list */
2441 silc_hash_table_del(client->channels, channel);
2443 /* Remove channel if there is no users anymore */
2444 if (server->server_type == SILC_ROUTER &&
2445 silc_hash_table_count(channel->user_list) < 2) {
2447 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2448 if (!silc_idlist_del_channel(server->local_list, channel))
2449 silc_idlist_del_channel(server->global_list, channel);
2450 server->stat.my_channels--;
2454 /* Remove client from channel's client list */
2455 silc_hash_table_del(channel->user_list, chl->client);
2457 /* If there is no global users on the channel anymore mark the channel
2458 as local channel. Do not check if the removed client is local client. */
2459 if (server->server_type != SILC_ROUTER && channel->global_users &&
2460 chl->client->router && !silc_server_channel_has_global(channel))
2461 channel->global_users = FALSE;
2464 server->stat.my_chanclients--;
2466 /* If there is not at least one local user on the channel then we don't
2467 need the channel entry anymore, we can remove it safely. */
2468 if (server->server_type != SILC_ROUTER &&
2469 !silc_server_channel_has_local(channel)) {
2470 /* Notify about leaving client if this channel has global users. */
2471 if (notify && channel->global_users)
2472 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2473 SILC_NOTIFY_TYPE_SIGNOFF,
2474 signoff_message ? 2 : 1,
2475 clidp->data, clidp->len,
2476 signoff_message, signoff_message ?
2477 strlen(signoff_message) : 0);
2480 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2482 if (channel->founder_key) {
2483 /* The founder auth data exists, do not remove the channel entry */
2484 SilcChannelClientEntry chl2;
2485 SilcHashTableList htl2;
2489 silc_hash_table_list(channel->user_list, &htl2);
2490 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
2491 silc_hash_table_del(chl2->client->channels, channel);
2492 silc_hash_table_del(channel->user_list, chl2->client);
2498 /* Remove the channel entry */
2499 if (!silc_idlist_del_channel(server->local_list, channel))
2500 silc_idlist_del_channel(server->global_list, channel);
2501 server->stat.my_channels--;
2505 /* Send notify to channel about client leaving SILC and thus
2506 the entire channel. */
2508 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2509 SILC_NOTIFY_TYPE_SIGNOFF,
2510 signoff_message ? 2 : 1,
2511 clidp->data, clidp->len,
2512 signoff_message, signoff_message ?
2513 strlen(signoff_message) : 0);
2515 if (keygen && !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2516 /* Re-generate channel key */
2517 if (!silc_server_create_channel_key(server, channel, 0))
2520 /* Send the channel key to the channel. The key of course is not sent
2521 to the client who was removed from the channel. */
2522 silc_server_send_channel_key(server, client->connection, channel,
2523 server->server_type == SILC_ROUTER ?
2524 FALSE : !server->standalone);
2528 silc_buffer_free(clidp);
2531 /* Removes client from one channel. This is used for example when client
2532 calls LEAVE command to remove itself from the channel. Returns TRUE
2533 if channel still exists and FALSE if the channel is removed when
2534 last client leaves the channel. If `notify' is FALSE notify messages
2537 int silc_server_remove_from_one_channel(SilcServer server,
2538 SilcSocketConnection sock,
2539 SilcChannelEntry channel,
2540 SilcClientEntry client,
2543 SilcChannelClientEntry chl;
2546 SILC_LOG_DEBUG(("Start"));
2548 /* Get the entry to the channel, if this client is not on the channel
2550 if (!silc_hash_table_find(client->channels, channel, NULL, (void *)&chl))
2553 /* Remove the client from the channel. The client is removed from
2554 the channel's user list. */
2556 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2558 /* Remove channel from client's channel list */
2559 silc_hash_table_del(client->channels, chl->channel);
2561 /* Remove channel if there is no users anymore */
2562 if (server->server_type == SILC_ROUTER &&
2563 silc_hash_table_count(channel->user_list) < 2) {
2565 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2566 if (!silc_idlist_del_channel(server->local_list, channel))
2567 silc_idlist_del_channel(server->global_list, channel);
2568 silc_buffer_free(clidp);
2569 server->stat.my_channels--;
2573 /* Remove client from channel's client list */
2574 silc_hash_table_del(channel->user_list, chl->client);
2576 /* If there is no global users on the channel anymore mark the channel
2577 as local channel. Do not check if the client is local client. */
2578 if (server->server_type != SILC_ROUTER && channel->global_users &&
2579 chl->client->router && !silc_server_channel_has_global(channel))
2580 channel->global_users = FALSE;
2583 server->stat.my_chanclients--;
2585 /* If there is not at least one local user on the channel then we don't
2586 need the channel entry anymore, we can remove it safely. */
2587 if (server->server_type != SILC_ROUTER &&
2588 !silc_server_channel_has_local(channel)) {
2589 /* Notify about leaving client if this channel has global users. */
2590 if (notify && channel->global_users)
2591 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2592 SILC_NOTIFY_TYPE_LEAVE, 1,
2593 clidp->data, clidp->len);
2595 silc_buffer_free(clidp);
2598 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2600 if (channel->founder_key) {
2601 /* The founder auth data exists, do not remove the channel entry */
2602 SilcChannelClientEntry chl2;
2603 SilcHashTableList htl2;
2607 silc_hash_table_list(channel->user_list, &htl2);
2608 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
2609 silc_hash_table_del(chl2->client->channels, channel);
2610 silc_hash_table_del(channel->user_list, chl2->client);
2616 /* Remove the channel entry */
2617 if (!silc_idlist_del_channel(server->local_list, channel))
2618 silc_idlist_del_channel(server->global_list, channel);
2619 server->stat.my_channels--;
2623 /* Send notify to channel about client leaving the channel */
2625 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2626 SILC_NOTIFY_TYPE_LEAVE, 1,
2627 clidp->data, clidp->len);
2629 silc_buffer_free(clidp);
2633 /* Timeout callback. This is called if connection is idle or for some
2634 other reason is not responding within some period of time. This
2635 disconnects the remote end. */
2637 SILC_TASK_CALLBACK(silc_server_timeout_remote)
2639 SilcServer server = (SilcServer)context;
2640 SilcSocketConnection sock = server->sockets[fd];
2642 SILC_LOG_DEBUG(("Start"));
2647 /* If we have protocol active we must assure that we call the protocol's
2648 final callback so that all the memory is freed. */
2649 if (sock->protocol) {
2650 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2651 silc_protocol_execute_final(sock->protocol, server->schedule);
2655 if (sock->user_data)
2656 silc_server_free_sock_user_data(server, sock);
2658 silc_server_disconnect_remote(server, sock, "Server closed connection: "
2659 "Connection timeout");
2662 /* Creates new channel. Sends NEW_CHANNEL packet to primary route. This
2663 function may be used only by router. In real SILC network all channels
2664 are created by routers thus this function is never used by normal
2667 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
2668 SilcServerID *router_id,
2674 SilcChannelID *channel_id;
2675 SilcChannelEntry entry;
2679 SILC_LOG_DEBUG(("Creating new channel"));
2682 cipher = SILC_DEFAULT_CIPHER;
2684 hmac = SILC_DEFAULT_HMAC;
2686 /* Allocate cipher */
2687 if (!silc_cipher_alloc(cipher, &key))
2691 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2692 silc_cipher_free(key);
2696 channel_name = strdup(channel_name);
2698 /* Create the channel */
2699 if (!silc_id_create_channel_id(server, router_id, server->rng,
2701 silc_free(channel_name);
2702 silc_cipher_free(key);
2703 silc_hmac_free(newhmac);
2706 entry = silc_idlist_add_channel(server->local_list, channel_name,
2707 SILC_CHANNEL_MODE_NONE, channel_id,
2708 NULL, key, newhmac);
2710 silc_free(channel_name);
2711 silc_cipher_free(key);
2712 silc_hmac_free(newhmac);
2716 entry->cipher = strdup(cipher);
2717 entry->hmac_name = strdup(hmac);
2719 /* Now create the actual key material */
2720 if (!silc_server_create_channel_key(server, entry,
2721 silc_cipher_get_key_len(key) / 8)) {
2722 silc_free(channel_name);
2723 silc_cipher_free(key);
2724 silc_hmac_free(newhmac);
2725 silc_free(entry->cipher);
2726 silc_free(entry->hmac_name);
2730 /* Notify other routers about the new channel. We send the packet
2731 to our primary route. */
2732 if (broadcast && server->standalone == FALSE)
2733 silc_server_send_new_channel(server, server->router->connection, TRUE,
2734 channel_name, entry->id,
2735 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
2738 server->stat.my_channels++;
2743 /* Same as above but creates the channel with Channel ID `channel_id. */
2746 silc_server_create_new_channel_with_id(SilcServer server,
2750 SilcChannelID *channel_id,
2753 SilcChannelEntry entry;
2757 SILC_LOG_DEBUG(("Creating new channel"));
2760 cipher = SILC_DEFAULT_CIPHER;
2762 hmac = SILC_DEFAULT_HMAC;
2764 /* Allocate cipher */
2765 if (!silc_cipher_alloc(cipher, &key))
2769 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2770 silc_cipher_free(key);
2774 channel_name = strdup(channel_name);
2776 /* Create the channel */
2777 entry = silc_idlist_add_channel(server->local_list, channel_name,
2778 SILC_CHANNEL_MODE_NONE, channel_id,
2779 NULL, key, newhmac);
2781 silc_free(channel_name);
2785 /* Now create the actual key material */
2786 if (!silc_server_create_channel_key(server, entry,
2787 silc_cipher_get_key_len(key) / 8)) {
2788 silc_free(channel_name);
2792 /* Notify other routers about the new channel. We send the packet
2793 to our primary route. */
2794 if (broadcast && server->standalone == FALSE)
2795 silc_server_send_new_channel(server, server->router->connection, TRUE,
2796 channel_name, entry->id,
2797 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
2800 server->stat.my_channels++;
2805 /* Channel's key re-key timeout callback. */
2807 SILC_TASK_CALLBACK(silc_server_channel_key_rekey)
2809 SilcServerChannelRekey rekey = (SilcServerChannelRekey)context;
2810 SilcServer server = (SilcServer)rekey->context;
2814 if (!silc_server_create_channel_key(server, rekey->channel, rekey->key_len))
2817 silc_server_send_channel_key(server, NULL, rekey->channel, FALSE);
2820 /* Generates new channel key. This is used to create the initial channel key
2821 but also to re-generate new key for channel. If `key_len' is provided
2822 it is the bytes of the key length. */
2824 bool silc_server_create_channel_key(SilcServer server,
2825 SilcChannelEntry channel,
2829 unsigned char channel_key[32], hash[32];
2832 SILC_LOG_DEBUG(("Generating channel key"));
2834 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2835 SILC_LOG_DEBUG(("Channel has private keys, will not generate new key"));
2839 if (!channel->channel_key)
2840 if (!silc_cipher_alloc(SILC_DEFAULT_CIPHER, &channel->channel_key))
2845 else if (channel->key_len)
2846 len = channel->key_len / 8;
2848 len = silc_cipher_get_key_len(channel->channel_key) / 8;
2850 /* Create channel key */
2851 for (i = 0; i < len; i++) channel_key[i] = silc_rng_get_byte(server->rng);
2854 silc_cipher_set_key(channel->channel_key, channel_key, len * 8);
2856 /* Remove old key if exists */
2858 memset(channel->key, 0, channel->key_len / 8);
2859 silc_free(channel->key);
2863 channel->key_len = len * 8;
2864 channel->key = silc_calloc(len, sizeof(*channel->key));
2865 memcpy(channel->key, channel_key, len);
2866 memset(channel_key, 0, sizeof(channel_key));
2868 /* Generate HMAC key from the channel key data and set it */
2870 silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
2871 silc_hash_make(channel->hmac->hash, channel->key, len, hash);
2872 silc_hmac_set_key(channel->hmac, hash, silc_hash_len(channel->hmac->hash));
2873 memset(hash, 0, sizeof(hash));
2875 if (server->server_type == SILC_ROUTER) {
2876 if (!channel->rekey)
2877 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
2878 channel->rekey->context = (void *)server;
2879 channel->rekey->channel = channel;
2880 channel->rekey->key_len = key_len;
2881 if (channel->rekey->task)
2882 silc_schedule_task_del(server->schedule, channel->rekey->task);
2884 channel->rekey->task =
2885 silc_schedule_task_add(server->schedule, 0,
2886 silc_server_channel_key_rekey,
2887 (void *)channel->rekey, 3600, 0,
2889 SILC_TASK_PRI_NORMAL);
2895 /* Saves the channel key found in the encoded `key_payload' buffer. This
2896 function is used when we receive Channel Key Payload and also when we're
2897 processing JOIN command reply. Returns entry to the channel. */
2899 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
2900 SilcBuffer key_payload,
2901 SilcChannelEntry channel)
2903 SilcChannelKeyPayload payload = NULL;
2904 SilcChannelID *id = NULL;
2905 unsigned char *tmp, hash[32];
2909 SILC_LOG_DEBUG(("Start"));
2911 /* Decode channel key payload */
2912 payload = silc_channel_key_payload_parse(key_payload);
2914 SILC_LOG_ERROR(("Bad channel key payload, dropped"));
2919 /* Get the channel entry */
2922 /* Get channel ID */
2923 tmp = silc_channel_key_get_id(payload, &tmp_len);
2924 id = silc_id_str2id(tmp, tmp_len, SILC_ID_CHANNEL);
2930 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
2932 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
2934 SILC_LOG_ERROR(("Received key for non-existent channel"));
2940 tmp = silc_channel_key_get_key(payload, &tmp_len);
2946 cipher = silc_channel_key_get_cipher(payload, NULL);
2952 /* Remove old key if exists */
2954 memset(channel->key, 0, channel->key_len / 8);
2955 silc_free(channel->key);
2956 silc_cipher_free(channel->channel_key);
2959 /* Create new cipher */
2960 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
2965 if (channel->cipher)
2966 silc_free(channel->cipher);
2967 channel->cipher = strdup(cipher);
2970 channel->key_len = tmp_len * 8;
2971 channel->key = silc_calloc(tmp_len, sizeof(unsigned char));
2972 memcpy(channel->key, tmp, tmp_len);
2973 silc_cipher_set_key(channel->channel_key, tmp, channel->key_len);
2975 /* Generate HMAC key from the channel key data and set it */
2977 silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
2978 silc_hash_make(channel->hmac->hash, tmp, tmp_len, hash);
2979 silc_hmac_set_key(channel->hmac, hash, silc_hash_len(channel->hmac->hash));
2981 memset(hash, 0, sizeof(hash));
2982 memset(tmp, 0, tmp_len);
2984 if (server->server_type == SILC_ROUTER) {
2985 if (!channel->rekey)
2986 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
2987 channel->rekey->context = (void *)server;
2988 channel->rekey->channel = channel;
2989 if (channel->rekey->task)
2990 silc_schedule_task_del(server->schedule, channel->rekey->task);
2992 channel->rekey->task =
2993 silc_schedule_task_add(server->schedule, 0,
2994 silc_server_channel_key_rekey,
2995 (void *)channel->rekey, 3600, 0,
2997 SILC_TASK_PRI_NORMAL);
3003 silc_channel_key_payload_free(payload);
3008 /* Heartbeat callback. This function is set as argument for the
3009 silc_socket_set_heartbeat function. The library will call this function
3010 at the set time interval. */
3012 void silc_server_perform_heartbeat(SilcSocketConnection sock,
3015 SilcServerHBContext hb = (SilcServerHBContext)hb_context;
3017 SILC_LOG_DEBUG(("Sending heartbeat to %s (%s)", sock->hostname,
3020 /* Send the heartbeat */
3021 silc_server_send_heartbeat(hb->server, sock);
3024 /* Returns assembled of all servers in the given ID list. The packet's
3025 form is dictated by the New ID payload. */
3027 static void silc_server_announce_get_servers(SilcServer server,
3028 SilcServerEntry remote,
3030 SilcBuffer *servers,
3031 unsigned long creation_time)
3033 SilcIDCacheList list;
3034 SilcIDCacheEntry id_cache;
3035 SilcServerEntry entry;
3038 /* Go through all clients in the list */
3039 if (silc_idcache_get_all(id_list->servers, &list)) {
3040 if (silc_idcache_list_first(list, &id_cache)) {
3042 entry = (SilcServerEntry)id_cache->context;
3044 /* Do not announce the one we've sending our announcements and
3045 do not announce ourself. Also check the creation time if it's
3047 if ((entry == remote) || (entry == server->id_entry) ||
3048 (creation_time && entry->data.created < creation_time)) {
3049 if (!silc_idcache_list_next(list, &id_cache))
3054 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
3056 *servers = silc_buffer_realloc(*servers,
3058 (*servers)->truelen + idp->len :
3060 silc_buffer_pull_tail(*servers, ((*servers)->end - (*servers)->data));
3061 silc_buffer_put(*servers, idp->data, idp->len);
3062 silc_buffer_pull(*servers, idp->len);
3063 silc_buffer_free(idp);
3065 if (!silc_idcache_list_next(list, &id_cache))
3070 silc_idcache_list_free(list);
3074 /* This function is used by router to announce existing servers to our
3075 primary router when we've connected to it. If `creation_time' is non-zero
3076 then only the servers that has been created after the `creation_time'
3077 will be announced. */
3079 void silc_server_announce_servers(SilcServer server, bool global,
3080 unsigned long creation_time,
3081 SilcSocketConnection remote)
3083 SilcBuffer servers = NULL;
3085 SILC_LOG_DEBUG(("Announcing servers"));
3087 /* Get servers in local list */
3088 silc_server_announce_get_servers(server, remote->user_data,
3089 server->local_list, &servers,
3093 /* Get servers in global list */
3094 silc_server_announce_get_servers(server, remote->user_data,
3095 server->global_list, &servers,
3099 silc_buffer_push(servers, servers->data - servers->head);
3100 SILC_LOG_HEXDUMP(("servers"), servers->data, servers->len);
3102 /* Send the packet */
3103 silc_server_packet_send(server, remote,
3104 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3105 servers->data, servers->len, TRUE);
3107 silc_buffer_free(servers);
3111 /* Returns assembled packet of all clients in the given ID list. The
3112 packet's form is dictated by the New ID Payload. */
3114 static void silc_server_announce_get_clients(SilcServer server,
3116 SilcBuffer *clients,
3117 unsigned long creation_time)
3119 SilcIDCacheList list;
3120 SilcIDCacheEntry id_cache;
3121 SilcClientEntry client;
3124 /* Go through all clients in the list */
3125 if (silc_idcache_get_all(id_list->clients, &list)) {
3126 if (silc_idcache_list_first(list, &id_cache)) {
3128 client = (SilcClientEntry)id_cache->context;
3130 if (creation_time && client->data.created < creation_time) {
3131 if (!silc_idcache_list_next(list, &id_cache))
3136 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3138 *clients = silc_buffer_realloc(*clients,
3140 (*clients)->truelen + idp->len :
3142 silc_buffer_pull_tail(*clients, ((*clients)->end - (*clients)->data));
3143 silc_buffer_put(*clients, idp->data, idp->len);
3144 silc_buffer_pull(*clients, idp->len);
3145 silc_buffer_free(idp);
3147 if (!silc_idcache_list_next(list, &id_cache))
3152 silc_idcache_list_free(list);
3156 /* This function is used to announce our existing clients to our router
3157 when we've connected to it. If `creation_time' is non-zero then only
3158 the clients that has been created after the `creation_time' will be
3161 void silc_server_announce_clients(SilcServer server,
3162 unsigned long creation_time,
3163 SilcSocketConnection remote)
3165 SilcBuffer clients = NULL;
3167 SILC_LOG_DEBUG(("Announcing clients"));
3169 /* Get clients in local list */
3170 silc_server_announce_get_clients(server, server->local_list,
3171 &clients, creation_time);
3173 /* As router we announce our global list as well */
3174 if (server->server_type == SILC_ROUTER)
3175 silc_server_announce_get_clients(server, server->global_list,
3176 &clients, creation_time);
3179 silc_buffer_push(clients, clients->data - clients->head);
3180 SILC_LOG_HEXDUMP(("clients"), clients->data, clients->len);
3182 /* Send the packet */
3183 silc_server_packet_send(server, remote,
3184 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3185 clients->data, clients->len, TRUE);
3187 silc_buffer_free(clients);
3192 silc_server_announce_encode_notify(SilcNotifyType notify, uint32 argc, ...)
3198 p = silc_notify_payload_encode(notify, argc, ap);
3204 /* Returns assembled packets for channel users of the `channel'. */
3206 void silc_server_announce_get_channel_users(SilcServer server,
3207 SilcChannelEntry channel,
3208 SilcBuffer *channel_users,
3209 SilcBuffer *channel_users_modes)
3211 SilcChannelClientEntry chl;
3212 SilcHashTableList htl;
3213 SilcBuffer chidp, clidp;
3216 unsigned char mode[4];
3218 SILC_LOG_DEBUG(("Start"));
3220 /* Now find all users on the channel */
3221 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3222 silc_hash_table_list(channel->user_list, &htl);
3223 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3224 clidp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3227 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_JOIN, 2,
3228 clidp->data, clidp->len,
3229 chidp->data, chidp->len);
3232 silc_buffer_realloc(*channel_users,
3234 (*channel_users)->truelen + len : len));
3235 silc_buffer_pull_tail(*channel_users,
3236 ((*channel_users)->end -
3237 (*channel_users)->data));
3239 silc_buffer_put(*channel_users, tmp->data, tmp->len);
3240 silc_buffer_pull(*channel_users, len);
3241 silc_buffer_free(tmp);
3243 /* CUMODE notify for mode change on the channel */
3244 SILC_PUT32_MSB(chl->mode, mode);
3245 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CUMODE_CHANGE,
3246 3, clidp->data, clidp->len,
3248 clidp->data, clidp->len);
3250 *channel_users_modes =
3251 silc_buffer_realloc(*channel_users_modes,
3252 (*channel_users_modes ?
3253 (*channel_users_modes)->truelen + len : len));
3254 silc_buffer_pull_tail(*channel_users_modes,
3255 ((*channel_users_modes)->end -
3256 (*channel_users_modes)->data));
3258 silc_buffer_put(*channel_users_modes, tmp->data, tmp->len);
3259 silc_buffer_pull(*channel_users_modes, len);
3260 silc_buffer_free(tmp);
3262 silc_buffer_free(clidp);
3264 silc_buffer_free(chidp);
3267 /* Returns assembled packets for all channels and users on those channels
3268 from the given ID List. The packets are in the form dictated by the
3269 New Channel and New Channel User payloads. */
3271 void silc_server_announce_get_channels(SilcServer server,
3273 SilcBuffer *channels,
3274 SilcBuffer *channel_users,
3275 SilcBuffer **channel_users_modes,
3276 uint32 *channel_users_modes_c,
3277 SilcChannelID ***channel_ids,
3278 unsigned long creation_time)
3280 SilcIDCacheList list;
3281 SilcIDCacheEntry id_cache;
3282 SilcChannelEntry channel;
3287 int i = *channel_users_modes_c;
3290 SILC_LOG_DEBUG(("Start"));
3292 /* Go through all channels in the list */
3293 if (silc_idcache_get_all(id_list->channels, &list)) {
3294 if (silc_idcache_list_first(list, &id_cache)) {
3296 channel = (SilcChannelEntry)id_cache->context;
3298 if (creation_time && channel->created < creation_time)
3303 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3304 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3305 name_len = strlen(channel->channel_name);
3308 len = 4 + name_len + id_len + 4;
3310 silc_buffer_realloc(*channels,
3311 (*channels ? (*channels)->truelen +
3313 silc_buffer_pull_tail(*channels,
3314 ((*channels)->end - (*channels)->data));
3315 silc_buffer_format(*channels,
3316 SILC_STR_UI_SHORT(name_len),
3317 SILC_STR_UI_XNSTRING(channel->channel_name,
3319 SILC_STR_UI_SHORT(id_len),
3320 SILC_STR_UI_XNSTRING(cid, id_len),
3321 SILC_STR_UI_INT(channel->mode),
3323 silc_buffer_pull(*channels, len);
3326 *channel_users_modes = silc_realloc(*channel_users_modes,
3327 sizeof(**channel_users_modes) *
3329 (*channel_users_modes)[i] = NULL;
3330 *channel_ids = silc_realloc(*channel_ids,
3331 sizeof(**channel_ids) * (i + 1));
3332 (*channel_ids)[i] = NULL;
3333 silc_server_announce_get_channel_users(server, channel,
3335 channel_users_modes[i]);
3336 (*channel_ids)[i] = channel->id;
3339 if (!silc_idcache_list_next(list, &id_cache))
3343 *channel_users_modes_c += i;
3346 silc_idcache_list_free(list);
3350 /* This function is used to announce our existing channels to our router
3351 when we've connected to it. This also announces the users on the
3352 channels to the router. If the `creation_time' is non-zero only the
3353 channels that was created after the `creation_time' are announced.
3354 Note that the channel users are still announced even if the `creation_time'
3357 void silc_server_announce_channels(SilcServer server,
3358 unsigned long creation_time,
3359 SilcSocketConnection remote)
3361 SilcBuffer channels = NULL, channel_users = NULL;
3362 SilcBuffer *channel_users_modes = NULL;
3363 uint32 channel_users_modes_c = 0;
3364 SilcChannelID **channel_ids = NULL;
3366 SILC_LOG_DEBUG(("Announcing channels and channel users"));
3368 /* Get channels and channel users in local list */
3369 silc_server_announce_get_channels(server, server->local_list,
3370 &channels, &channel_users,
3371 &channel_users_modes,
3372 &channel_users_modes_c,
3373 &channel_ids, creation_time);
3375 /* Get channels and channel users in global list */
3376 silc_server_announce_get_channels(server, server->global_list,
3377 &channels, &channel_users,
3378 &channel_users_modes,
3379 &channel_users_modes_c,
3380 &channel_ids, creation_time);
3383 silc_buffer_push(channels, channels->data - channels->head);
3384 SILC_LOG_HEXDUMP(("channels"), channels->data, channels->len);
3386 /* Send the packet */
3387 silc_server_packet_send(server, remote,
3388 SILC_PACKET_NEW_CHANNEL, SILC_PACKET_FLAG_LIST,
3389 channels->data, channels->len,
3392 silc_buffer_free(channels);
3395 if (channel_users) {
3396 silc_buffer_push(channel_users, channel_users->data - channel_users->head);
3397 SILC_LOG_HEXDUMP(("channel users"), channel_users->data,
3398 channel_users->len);
3400 /* Send the packet */
3401 silc_server_packet_send(server, remote,
3402 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3403 channel_users->data, channel_users->len,
3406 silc_buffer_free(channel_users);
3409 if (channel_users_modes) {
3412 for (i = 0; i < channel_users_modes_c; i++) {
3413 silc_buffer_push(channel_users_modes[i],
3414 channel_users_modes[i]->data -
3415 channel_users_modes[i]->head);
3416 SILC_LOG_HEXDUMP(("channel users modes"), channel_users_modes[i]->data,
3417 channel_users_modes[i]->len);
3418 silc_server_packet_send_dest(server, remote,
3419 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3420 channel_ids[i], SILC_ID_CHANNEL,
3421 channel_users_modes[i]->data,
3422 channel_users_modes[i]->len,
3424 silc_buffer_free(channel_users_modes[i]);
3426 silc_free(channel_users_modes);
3427 silc_free(channel_ids);
3431 /* Failure timeout callback. If this is called then we will immediately
3432 process the received failure. We always process the failure with timeout
3433 since we do not want to blindly trust to received failure packets.
3434 This won't be called (the timeout is cancelled) if the failure was
3435 bogus (it is bogus if remote does not close the connection after sending
3438 SILC_TASK_CALLBACK(silc_server_failure_callback)
3440 SilcServerFailureContext f = (SilcServerFailureContext)context;
3442 if (f->sock->protocol) {
3443 f->sock->protocol->state = SILC_PROTOCOL_STATE_FAILURE;
3444 silc_protocol_execute(f->sock->protocol, f->server->schedule, 0, 0);
3450 /* Assembles user list and users mode list from the `channel'. */
3452 void silc_server_get_users_on_channel(SilcServer server,
3453 SilcChannelEntry channel,
3454 SilcBuffer *user_list,
3455 SilcBuffer *mode_list,
3458 SilcChannelClientEntry chl;
3459 SilcHashTableList htl;
3460 SilcBuffer client_id_list;
3461 SilcBuffer client_mode_list;
3463 uint32 list_count = 0, len = 0;
3465 silc_hash_table_list(channel->user_list, &htl);
3466 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
3467 len += (silc_id_get_len(chl->client->id, SILC_ID_CLIENT) + 4);
3469 client_id_list = silc_buffer_alloc(len);
3471 silc_buffer_alloc(4 * silc_hash_table_count(channel->user_list));
3472 silc_buffer_pull_tail(client_id_list, SILC_BUFFER_END(client_id_list));
3473 silc_buffer_pull_tail(client_mode_list, SILC_BUFFER_END(client_mode_list));
3475 silc_hash_table_list(channel->user_list, &htl);
3476 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3478 idp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3479 silc_buffer_put(client_id_list, idp->data, idp->len);
3480 silc_buffer_pull(client_id_list, idp->len);
3481 silc_buffer_free(idp);
3483 /* Client's mode on channel */
3484 SILC_PUT32_MSB(chl->mode, client_mode_list->data);
3485 silc_buffer_pull(client_mode_list, 4);
3489 silc_buffer_push(client_id_list,
3490 client_id_list->data - client_id_list->head);
3491 silc_buffer_push(client_mode_list,
3492 client_mode_list->data - client_mode_list->head);
3494 *user_list = client_id_list;
3495 *mode_list = client_mode_list;
3496 *user_count = list_count;
3499 /* Saves users and their modes to the `channel'. */
3501 void silc_server_save_users_on_channel(SilcServer server,
3502 SilcSocketConnection sock,
3503 SilcChannelEntry channel,
3504 SilcClientID *noadd,
3505 SilcBuffer user_list,
3506 SilcBuffer mode_list,
3511 for (i = 0; i < user_count; i++) {
3514 SilcClientID *client_id;
3515 SilcClientEntry client;
3518 SILC_GET16_MSB(idp_len, user_list->data + 2);
3520 client_id = silc_id_payload_parse_id(user_list->data, idp_len);
3521 silc_buffer_pull(user_list, idp_len);
3526 SILC_GET32_MSB(mode, mode_list->data);
3527 silc_buffer_pull(mode_list, 4);
3529 if (noadd && SILC_ID_CLIENT_COMPARE(client_id, noadd)) {
3530 silc_free(client_id);
3534 /* Check if we have this client cached already. */
3535 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3536 server->server_type, NULL);
3538 client = silc_idlist_find_client_by_id(server->global_list,
3539 client_id, server->server_type,
3542 /* If router did not find such Client ID in its lists then this must
3543 be bogus client or some router in the net is buggy. */
3544 if (server->server_type == SILC_ROUTER) {
3545 silc_free(client_id);
3549 /* We don't have that client anywhere, add it. The client is added
3550 to global list since server didn't have it in the lists so it must be
3552 client = silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
3553 silc_id_dup(client_id, SILC_ID_CLIENT),
3554 sock->user_data, NULL);
3556 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
3557 silc_free(client_id);
3561 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3564 silc_free(client_id);
3566 if (!silc_server_client_on_channel(client, channel)) {
3567 /* Client was not on the channel, add it. */
3568 SilcChannelClientEntry chl = silc_calloc(1, sizeof(*chl));
3569 chl->client = client;
3571 chl->channel = channel;
3572 silc_hash_table_add(channel->user_list, chl->client, chl);
3573 silc_hash_table_add(client->channels, chl->channel, chl);
3578 /* Lookups route to the client indicated by the `id_data'. The connection
3579 object and internal data object is returned. Returns NULL if route
3580 could not be found to the client. If the `client_id' is specified then
3581 it is used and the `id_data' is ignored. */
3583 SilcSocketConnection silc_server_get_client_route(SilcServer server,
3584 unsigned char *id_data,
3586 SilcClientID *client_id,
3587 SilcIDListData *idata)
3590 SilcClientEntry client;
3592 SILC_LOG_DEBUG(("Start"));
3594 /* Decode destination Client ID */
3596 id = silc_id_str2id(id_data, id_len, SILC_ID_CLIENT);
3598 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
3602 id = silc_id_dup(client_id, SILC_ID_CLIENT);
3605 /* If the destination belongs to our server we don't have to route
3606 the packet anywhere but to send it to the local destination. */
3607 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
3611 /* If we are router and the client has router then the client is in
3612 our cell but not directly connected to us. */
3613 if (server->server_type == SILC_ROUTER && client->router) {
3614 /* We are of course in this case the client's router thus the route
3615 to the client is the server who owns the client. So, we will send
3616 the packet to that server. */
3618 *idata = (SilcIDListData)client->router;
3619 return client->router->connection;
3622 /* Seems that client really is directly connected to us */
3624 *idata = (SilcIDListData)client;
3625 return client->connection;
3628 /* Destination belongs to someone not in this server. If we are normal
3629 server our action is to send the packet to our router. */
3630 if (server->server_type != SILC_ROUTER && !server->standalone) {
3633 *idata = (SilcIDListData)server->router;
3634 return server->router->connection;
3637 /* We are router and we will perform route lookup for the destination
3638 and send the packet to fastest route. */
3639 if (server->server_type == SILC_ROUTER && !server->standalone) {
3640 /* Check first that the ID is valid */
3641 client = silc_idlist_find_client_by_id(server->global_list, id,
3644 SilcSocketConnection dst_sock;
3646 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
3650 *idata = (SilcIDListData)dst_sock->user_data;
3659 /* Encodes and returns channel list of channels the `client' has joined.
3660 Secret channels are not put to the list. */
3662 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
3663 SilcClientEntry client)
3665 SilcBuffer buffer = NULL;
3666 SilcChannelEntry channel;
3667 SilcChannelClientEntry chl;
3668 SilcHashTableList htl;
3674 silc_hash_table_list(client->channels, &htl);
3675 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3676 channel = chl->channel;
3678 if (channel->mode & SILC_CHANNEL_MODE_SECRET ||
3679 channel->mode & SILC_CHANNEL_MODE_PRIVATE)
3682 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3683 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3684 name_len = strlen(channel->channel_name);
3686 len = 4 + name_len + id_len + 4;
3687 buffer = silc_buffer_realloc(buffer,
3688 (buffer ? (buffer)->truelen + len : len));
3689 silc_buffer_pull_tail(buffer, ((buffer)->end - (buffer)->data));
3690 silc_buffer_format(buffer,
3691 SILC_STR_UI_SHORT(name_len),
3692 SILC_STR_UI_XNSTRING(channel->channel_name,
3694 SILC_STR_UI_SHORT(id_len),
3695 SILC_STR_UI_XNSTRING(cid, id_len),
3696 SILC_STR_UI_INT(chl->mode), /* Client's mode */
3698 silc_buffer_pull(buffer, len);
3703 silc_buffer_push(buffer, buffer->data - buffer->head);
3708 /* Finds client entry by Client ID and if it is not found then resolves
3709 it using WHOIS command. */
3711 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
3712 SilcClientID *client_id)
3714 SilcClientEntry client;
3716 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3719 client = silc_idlist_find_client_by_id(server->global_list,
3720 client_id, TRUE, NULL);
3721 if (!client && server->server_type == SILC_ROUTER)
3725 if (!client && server->standalone)
3728 if (!client || !client->nickname || !client->username) {
3729 SilcBuffer buffer, idp;
3731 client->data.status |= SILC_IDLIST_STATUS_RESOLVING;
3732 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
3733 client->resolve_cmd_ident = ++server->cmd_ident;
3735 idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3736 buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
3737 server->cmd_ident, 1,
3738 3, idp->data, idp->len);
3739 silc_server_packet_send(server, client ? client->router->connection :
3740 server->router->connection,
3741 SILC_PACKET_COMMAND, 0,
3742 buffer->data, buffer->len, FALSE);
3743 silc_buffer_free(idp);
3744 silc_buffer_free(buffer);
3751 /* A timeout callback for the re-key. We will be the initiator of the
3754 SILC_TASK_CALLBACK(silc_server_rekey_callback)
3756 SilcSocketConnection sock = (SilcSocketConnection)context;
3757 SilcIDListData idata = (SilcIDListData)sock->user_data;
3758 SilcServer server = (SilcServer)idata->rekey->context;
3759 SilcProtocol protocol;
3760 SilcServerRekeyInternalContext *proto_ctx;
3762 SILC_LOG_DEBUG(("Start"));
3764 /* Allocate internal protocol context. This is sent as context
3766 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3767 proto_ctx->server = (void *)server;
3768 proto_ctx->sock = sock;
3769 proto_ctx->responder = FALSE;
3770 proto_ctx->pfs = idata->rekey->pfs;
3772 /* Perform rekey protocol. Will call the final callback after the
3773 protocol is over. */
3774 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3775 &protocol, proto_ctx, silc_server_rekey_final);
3776 sock->protocol = protocol;
3778 /* Run the protocol */
3779 silc_protocol_execute(protocol, server->schedule, 0, 0);
3781 /* Re-register re-key timeout */
3782 silc_schedule_task_add(server->schedule, sock->sock,
3783 silc_server_rekey_callback,
3784 context, idata->rekey->timeout, 0,
3785 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
3788 /* The final callback for the REKEY protocol. This will actually take the
3789 new key material into use. */
3791 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final)
3793 SilcProtocol protocol = (SilcProtocol)context;
3794 SilcServerRekeyInternalContext *ctx =
3795 (SilcServerRekeyInternalContext *)protocol->context;
3796 SilcServer server = (SilcServer)ctx->server;
3797 SilcSocketConnection sock = ctx->sock;
3799 SILC_LOG_DEBUG(("Start"));
3801 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
3802 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
3803 /* Error occured during protocol */
3804 SILC_LOG_ERROR(("Error occurred during rekey protocol"));
3805 silc_protocol_cancel(protocol, server->schedule);
3806 silc_protocol_free(protocol);
3807 sock->protocol = NULL;
3809 silc_packet_context_free(ctx->packet);
3811 silc_ske_free(ctx->ske);
3816 /* Purge the outgoing data queue to assure that all rekey packets really
3817 go to the network before we quit the protocol. */
3818 silc_server_packet_queue_purge(server, sock);
3821 silc_protocol_free(protocol);
3822 sock->protocol = NULL;
3824 silc_packet_context_free(ctx->packet);
3826 silc_ske_free(ctx->ske);