5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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.
28 * Revision 1.10 2000/07/17 11:47:30 priikone
29 * Added command lagging support. Added idle counting support.
31 * Revision 1.9 2000/07/14 06:15:47 priikone
32 * Moved all the generic packet sending, encryption, reception,
33 * decryption and processing functions to library as they were
34 * duplicated code. Now server uses the generic routine which is
35 * a lot cleaner. Channel message sending uses now also generic
36 * routines instead of duplicating the packet sending for every
37 * channel message sending function. Same was done for private
38 * message sending as well.
40 * Revision 1.8 2000/07/12 05:59:41 priikone
41 * Major rewrite of ID Cache system. Support added for the new
42 * ID cache system. Major rewrite of ID List stuff on server. All
43 * SilcXXXList's are now called SilcXXXEntry's and they are pointers
44 * by default. A lot rewritten ID list functions.
46 * Revision 1.7 2000/07/10 05:43:00 priikone
47 * Removed command packet processing from server.c and added it to
49 * Implemented INFO command. Added support for testing that
50 * connections are registered before executing commands.
52 * Revision 1.6 2000/07/07 06:55:59 priikone
53 * Added SILC style public key support and made server to use
56 * Revision 1.5 2000/07/06 13:18:07 priikone
57 * Check for NULL in client_on_channel.
59 * Revision 1.4 2000/07/05 06:14:01 priikone
60 * Global costemic changes.
62 * Revision 1.3 2000/07/04 08:13:53 priikone
63 * Changed message route discovery to use silc_server_get_route.
64 * Added silc_server_client_on_channel function.
66 * Revision 1.1.1.1 2000/06/27 11:36:56 priikone
67 * Imported from internal CVS/Added Log headers.
72 #include "serverincludes.h"
73 #include "server_internal.h"
75 /* Static prototypes */
76 SILC_TASK_CALLBACK(silc_server_connect_to_router);
77 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
78 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
79 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
80 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
81 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
82 SILC_TASK_CALLBACK(silc_server_packet_process);
83 SILC_TASK_CALLBACK(silc_server_packet_parse_real);
84 SILC_TASK_CALLBACK(silc_server_timeout_remote);
86 extern char server_version[];
88 /* Allocates a new SILC server object. This has to be done before the server
89 can be used. After allocation one must call silc_server_init to initialize
90 the server. The new allocated server object is returned to the new_server
93 int silc_server_alloc(SilcServer *new_server)
97 SILC_LOG_DEBUG(("Allocating new server object"));
99 server = silc_calloc(1, sizeof(*server));
100 server->server_type = SILC_SERVER;
101 server->standalone = FALSE;
102 server->local_list = silc_calloc(1, sizeof(*server->local_list));
103 server->global_list = silc_calloc(1, sizeof(*server->global_list));
105 *new_server = server;
110 /* Free's the SILC server object. This is called at the very end before
113 void silc_server_free(SilcServer server)
116 if (server->local_list)
117 silc_free(server->local_list);
118 if (server->global_list)
119 silc_free(server->global_list);
121 silc_rng_free(server->rng);
123 silc_math_primegen_uninit(); /* XXX */
128 /* Initializes the entire SILC server. This is called always before running
129 the server. This is called only once at the initialization of the program.
130 This binds the server to its listenning port. After this function returns
131 one should call silc_server_run to start the server. This returns TRUE
132 when everything is ok to run the server. Configuration file must be
133 read and parsed before calling this. */
135 int silc_server_init(SilcServer server)
137 int *sock = NULL, sock_count = 0, i;
139 SilcServerEntry id_entry;
141 SILC_LOG_DEBUG(("Initializing server"));
143 assert(server->config);
145 /* Set log files where log message should be saved. */
146 server->config->server = server;
147 silc_config_server_setlogfiles(server->config);
149 /* Register all configured ciphers, PKCS and hash functions. */
150 silc_config_server_register_ciphers(server->config);
151 silc_config_server_register_pkcs(server->config);
152 silc_config_server_register_hashfuncs(server->config);
154 /* Initialize random number generator for the server. */
155 server->rng = silc_rng_alloc();
156 silc_rng_init(server->rng);
157 silc_math_primegen_init(); /* XXX */
159 /* Initialize hash functions for server to use */
160 silc_hash_alloc("md5", &server->md5hash);
161 silc_hash_alloc("sha1", &server->sha1hash);
163 /* Initialize none cipher */
164 silc_cipher_alloc("none", &server->none_cipher);
166 /* XXXXX Generate RSA key pair */
168 unsigned char *public_key;
169 unsigned char *private_key;
170 unsigned int pk_len, prv_len;
173 if (stat("pubkey.pub", &st) < 0 && stat("privkey.prv", &st) < 0) {
175 if (silc_pkcs_alloc("rsa", &server->pkcs) == FALSE) {
176 SILC_LOG_ERROR(("Could not create RSA key pair"));
180 if (server->pkcs->pkcs->init(server->pkcs->context,
181 1024, server->rng) == FALSE) {
182 SILC_LOG_ERROR(("Could not generate RSA key pair"));
186 public_key = server->pkcs->pkcs->get_public_key(server->pkcs->context,
188 private_key = server->pkcs->pkcs->get_private_key(server->pkcs->context,
191 SILC_LOG_HEXDUMP(("public key"), public_key, pk_len);
192 SILC_LOG_HEXDUMP(("private key"), private_key, prv_len);
195 silc_pkcs_public_key_alloc("rsa", "UN=root, HN=dummy",
197 server->private_key =
198 silc_pkcs_private_key_alloc("rsa", private_key, prv_len);
201 silc_pkcs_save_public_key("pubkey.pub", server->public_key,
203 silc_pkcs_save_private_key("privkey.prv", server->private_key, NULL,
206 memset(public_key, 0, pk_len);
207 memset(private_key, 0, prv_len);
208 silc_free(public_key);
209 silc_free(private_key);
211 silc_pkcs_load_public_key("pubkey.pub", &server->public_key,
213 silc_pkcs_load_private_key("privkey.prv", &server->private_key,
218 /* Create a listening server. Note that our server can listen on
219 multiple ports. All listeners are created here and now. */
220 /* XXX Still check this whether to use server_info or listen_port. */
222 while(server->config->listen_port) {
225 tmp = silc_net_create_server(server->config->listen_port->port,
226 server->config->listen_port->host);
230 sock = silc_realloc(sock, (sizeof(int *) * (sock_count + 1)));
231 sock[sock_count] = tmp;
232 server->config->listen_port = server->config->listen_port->next;
236 /* Initialize ID caches */
237 server->local_list->clients = silc_idcache_alloc(0);
238 server->local_list->servers = silc_idcache_alloc(0);
239 server->local_list->channels = silc_idcache_alloc(0);
241 if (server->server_type == SILC_ROUTER) {
242 server->global_list->clients = silc_idcache_alloc(0);
243 server->global_list->servers = silc_idcache_alloc(0);
244 server->global_list->channels = silc_idcache_alloc(0);
247 /* Allocate the entire socket list that is used in server. Eventually
248 all connections will have entry in this table (it is a table of
249 pointers to the actual object that is allocated individually
251 server->sockets = silc_calloc(SILC_SERVER_MAX_CONNECTIONS,
252 sizeof(*server->sockets));
254 for (i = 0; i < sock_count; i++) {
255 SilcSocketConnection newsocket = NULL;
257 /* Set socket to non-blocking mode */
258 silc_net_set_socket_nonblock(sock[i]);
259 server->sock = sock[i];
261 /* Create a Server ID for the server. */
262 silc_id_create_server_id(sock[i], server->rng, &id);
268 server->id_type = SILC_ID_SERVER;
269 server->server_name = server->config->server_info->server_name;
271 /* Add ourselves to the server list. We don't have a router yet
272 beacuse we haven't established a route yet. It will be done later.
273 For now, NULL is sent as router. This allocates new entry to
276 silc_idlist_add_server(server->local_list,
277 server->config->server_info->server_name,
278 server->server_type, server->id, NULL,
279 server->send_key, server->receive_key,
280 NULL, NULL, NULL, NULL);
282 SILC_LOG_ERROR(("Could not add ourselves to cache"));
286 /* Add ourselves also to the socket table. The entry allocated above
287 is sent as argument for fast referencing in the future. */
288 silc_socket_alloc(sock[i], SILC_SOCKET_TYPE_SERVER, id_entry,
293 server->sockets[sock[i]] = newsocket;
295 /* Put the allocated socket pointer also to the entry allocated above
296 for fast back-referencing to the socket list. */
297 id_entry->connection = (void *)server->sockets[sock[i]];
298 server->id_entry = id_entry;
301 /* Register the task queues. In SILC we have by default three task queues.
302 One task queue for non-timeout tasks which perform different kind of
303 I/O on file descriptors, timeout task queue for timeout tasks, and,
304 generic non-timeout task queue whose tasks apply to all connections. */
305 silc_task_queue_alloc(&server->io_queue, TRUE);
306 if (!server->io_queue) {
309 silc_task_queue_alloc(&server->timeout_queue, TRUE);
310 if (!server->timeout_queue) {
313 silc_task_queue_alloc(&server->generic_queue, TRUE);
314 if (!server->generic_queue) {
318 /* Initialize the scheduler */
319 silc_schedule_init(server->io_queue, server->timeout_queue,
320 server->generic_queue,
321 SILC_SERVER_MAX_CONNECTIONS);
323 /* Add the first task to the queue. This is task that is executed by
324 timeout. It expires as soon as the caller calls silc_server_run. This
325 task performs authentication protocol and key exchange with our
327 if (silc_task_register(server->timeout_queue, sock[0],
328 silc_server_connect_to_router,
329 (void *)server, 0, 1,
331 SILC_TASK_PRI_NORMAL) == NULL) {
335 /* If server connections has been configured then we must be router as
336 normal server cannot have server connections, only router connections. */
337 if (server->config->servers)
338 server->server_type = SILC_ROUTER;
340 SILC_LOG_DEBUG(("Server initialized"));
342 /* We are done here, return succesfully */
346 silc_task_queue_free(server->timeout_queue);
348 silc_task_queue_free(server->io_queue);
350 for (i = 0; i < sock_count; i++)
351 silc_net_close_server(sock[i]);
356 /* Stops the SILC server. This function is used to shutdown the server.
357 This is usually called after the scheduler has returned. After stopping
358 the server one should call silc_server_free. */
360 void silc_server_stop(SilcServer server)
362 SILC_LOG_DEBUG(("Stopping server"));
364 /* Stop the scheduler, although it might be already stopped. This
365 doesn't hurt anyone. This removes all the tasks and task queues,
367 silc_schedule_stop();
368 silc_schedule_uninit();
370 SILC_LOG_DEBUG(("Server stopped"));
373 /* The heart of the server. This runs the scheduler thus runs the server. */
375 void silc_server_run(SilcServer server)
377 SILC_LOG_DEBUG(("Running server"));
379 /* Start the scheduler, the heart of the SILC server. When this returns
380 the program will be terminated. */
384 /* This function connects to our primary router or if we are a router this
385 establishes all our primary routes. This is called at the start of the
386 server to do authentication and key exchange with our router - called
389 SILC_TASK_CALLBACK(silc_server_connect_to_router)
391 SilcServer server = (SilcServer)context;
392 SilcSocketConnection newsocket;
395 SILC_LOG_DEBUG(("Connecting to router(s)"));
397 /* if we are normal SILC server we need to connect to our cell's
399 if (server->server_type == SILC_SERVER) {
400 SilcProtocol protocol;
401 SilcServerKEInternalContext *proto_ctx;
403 /* Create connection to the router, if configured. */
404 if (server->config->routers) {
405 sock = silc_net_create_connection(server->config->routers->port,
406 server->config->routers->host);
408 SILC_LOG_ERROR(("Could not connect to router"));
409 silc_schedule_stop();
413 /* Set socket options */
414 silc_net_set_socket_nonblock(sock);
415 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
417 /* Create socket connection for the connection. Even though we
418 know that we are connecting to a router we will mark the socket
419 to be unknown connection until we have executed authentication
421 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
422 server->sockets[sock] = newsocket;
423 newsocket->hostname = server->config->routers->host;
424 newsocket->port = server->config->routers->port;
426 /* Allocate internal protocol context. This is sent as context
428 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
429 proto_ctx->server = context;
430 proto_ctx->sock = newsocket;
431 proto_ctx->rng = server->rng;
432 proto_ctx->responder = FALSE;
434 /* Perform key exchange protocol. silc_server_connect_to_router_second
435 will be called after the protocol is finished. */
436 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
437 &protocol, proto_ctx,
438 silc_server_connect_to_router_second);
439 newsocket->protocol = protocol;
441 /* Register a timeout task that will be executed if the protocol
442 is not executed within 60 seconds. For now, this is a hard coded
443 limit. After 60 secs the connection will be closed if the key
444 exchange protocol has not been executed. */
445 proto_ctx->timeout_task =
446 silc_task_register(server->timeout_queue, sock,
447 silc_server_timeout_remote,
452 /* Register the connection for network input and output. This sets
453 that scheduler will listen for incoming packets for this connection
454 and sets that outgoing packets may be sent to this connection as
455 well. However, this doesn't set the scheduler for outgoing traffic,
456 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
457 later when outgoing data is available. */
458 SILC_REGISTER_CONNECTION_FOR_IO(sock);
460 /* Run the protocol */
461 protocol->execute(server->timeout_queue, 0, protocol, sock, 0, 0);
466 /* if we are a SILC router we need to establish all of our primary
468 if (server->server_type == SILC_ROUTER) {
469 SilcConfigServerSectionServerConnection *ptr;
471 /* Create the connections to all our routes */
472 ptr = server->config->routers;
474 SilcProtocol protocol;
475 SilcServerKEInternalContext *proto_ctx;
477 /* Create the connection to the remote end */
478 sock = silc_net_create_connection(ptr->port, ptr->host);
480 SILC_LOG_ERROR(("Could not connect to router"));
481 silc_schedule_stop();
485 /* Set socket options */
486 silc_net_set_socket_nonblock(sock);
487 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
489 /* Create socket connection for the connection. Even though we
490 know that we are connecting to a router we will mark the socket
491 to be unknown connection until we have executed authentication
493 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
494 server->sockets[sock] = newsocket;
495 newsocket->hostname = ptr->host;
496 newsocket->port = ptr->port;
498 /* Allocate internal protocol context. This is sent as context
500 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
501 proto_ctx->server = context;
502 proto_ctx->sock = newsocket;
503 proto_ctx->rng = server->rng;
504 proto_ctx->responder = FALSE;
506 /* Perform key exchange protocol. silc_server_connect_to_router_final
507 will be called after the protocol is finished. */
508 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
509 &protocol, proto_ctx,
510 silc_server_connect_to_router_second);
511 newsocket->protocol = protocol;
513 /* Register a timeout task that will be executed if the protocol
514 is not executed within 60 seconds. For now, this is a hard coded
515 limit. After 60 secs the connection will be closed if the key
516 exchange protocol has not been executed. */
517 proto_ctx->timeout_task =
518 silc_task_register(server->timeout_queue, sock,
519 silc_server_timeout_remote,
524 /* Register the connection for network input and output. This sets
525 that scheduler will listen for incoming packets for this connection
526 and sets that outgoing packets may be sent to this connection as
527 well. However, this doesn't set the scheduler for outgoing traffic,
528 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
529 later when outgoing data is available. */
530 SILC_REGISTER_CONNECTION_FOR_IO(sock);
532 /* Run the protocol */
533 protocol->execute(server->timeout_queue, 0, protocol, sock, 0, 0);
542 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
544 /* There wasn't a configured router, we will continue but we don't
545 have a connection to outside world. We will be standalone server. */
546 server->standalone = TRUE;
548 /* Add a task to the queue. This task receives new connections to the
549 server. This task remains on the queue until the end of the program. */
550 if (silc_task_register(server->io_queue, fd,
551 silc_server_accept_new_connection,
552 (void *)server, 0, 0,
554 SILC_TASK_PRI_NORMAL) == NULL) {
555 silc_schedule_stop();
560 /* Second part of connecting to router(s). Key exchange protocol has been
561 executed and now we will execute authentication protocol. */
563 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
565 SilcProtocol protocol = (SilcProtocol)context;
566 SilcServerKEInternalContext *ctx =
567 (SilcServerKEInternalContext *)protocol->context;
568 SilcServer server = (SilcServer)ctx->server;
569 SilcSocketConnection sock = NULL;
570 SilcServerConnAuthInternalContext *proto_ctx;
572 SILC_LOG_DEBUG(("Start"));
574 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
575 /* Error occured during protocol */
576 silc_protocol_free(protocol);
578 silc_buffer_free(ctx->packet);
580 silc_ske_free(ctx->ske);
582 silc_free(ctx->dest_id);
584 sock->protocol = NULL;
585 silc_server_disconnect_remote(server, sock, "Server closed connection: "
586 "Key exchange failed");
590 /* Allocate internal context for the authentication protocol. This
591 is sent as context for the protocol. */
592 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
593 proto_ctx->server = (void *)server;
594 proto_ctx->sock = sock = server->sockets[fd];
595 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
596 proto_ctx->dest_id_type = ctx->dest_id_type;
597 proto_ctx->dest_id = ctx->dest_id;
599 /* Resolve the authentication method used in this connection */
600 proto_ctx->auth_meth = SILC_PROTOCOL_CONN_AUTH_PASSWORD;
601 if (server->config->routers) {
602 SilcConfigServerSectionServerConnection *conn = NULL;
604 /* Check if we find a match from user configured connections */
605 conn = silc_config_server_find_router_conn(server->config,
609 /* Match found. Use the configured authentication method */
610 proto_ctx->auth_meth = conn->auth_meth;
611 if (conn->auth_data) {
612 proto_ctx->auth_data = strdup(conn->auth_data);
613 proto_ctx->auth_data_len = strlen(conn->auth_data);
616 /* No match found. */
623 /* Free old protocol as it is finished now */
624 silc_protocol_free(protocol);
626 silc_buffer_free(ctx->packet);
628 sock->protocol = NULL;
630 /* Allocate the authentication protocol. This is allocated here
631 but we won't start it yet. We will be receiving party of this
632 protocol thus we will wait that connecting party will make
634 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
635 &sock->protocol, proto_ctx,
636 silc_server_connect_to_router_final);
638 /* Register timeout task. If the protocol is not executed inside
639 this timelimit the connection will be terminated. Currently
640 this is 15 seconds and is hard coded limit (XXX). */
641 proto_ctx->timeout_task =
642 silc_task_register(server->timeout_queue, sock->sock,
643 silc_server_timeout_remote,
644 (void *)server, 15, 0,
648 /* Run the protocol */
649 sock->protocol->execute(server->timeout_queue, 0,
650 sock->protocol, sock->sock, 0, 0);
653 /* Finalizes the connection to router. Registers a server task to the
654 queue so that we can accept new connections. */
656 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
658 SilcProtocol protocol = (SilcProtocol)context;
659 SilcServerConnAuthInternalContext *ctx =
660 (SilcServerConnAuthInternalContext *)protocol->context;
661 SilcServer server = (SilcServer)ctx->server;
662 SilcSocketConnection sock = ctx->sock;
663 SilcServerEntry id_entry;
664 SilcUnknownEntry conn_data;
666 unsigned char *id_string;
668 SILC_LOG_DEBUG(("Start"));
670 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
671 /* Error occured during protocol */
672 silc_protocol_free(protocol);
674 silc_buffer_free(ctx->packet);
676 silc_ske_free(ctx->ske);
678 silc_free(ctx->dest_id);
680 sock->protocol = NULL;
681 silc_server_disconnect_remote(server, sock, "Server closed connection: "
682 "Authentication failed");
686 /* Add a task to the queue. This task receives new connections to the
687 server. This task remains on the queue until the end of the program. */
688 if (!server->listenning) {
689 if (silc_task_register(server->io_queue, server->sock,
690 silc_server_accept_new_connection,
691 (void *)server, 0, 0,
693 SILC_TASK_PRI_NORMAL) == NULL) {
694 silc_schedule_stop();
697 server->listenning = TRUE;
701 /* Send NEW_SERVER packet to the router. We will become registered
702 to the SILC network after sending this packet. */
703 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
704 packet = silc_buffer_alloc(2 + 2 + SILC_ID_SERVER_LEN +
705 strlen(server->server_name));
706 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
707 silc_buffer_format(packet,
708 SILC_STR_UI_SHORT(SILC_ID_SERVER_LEN),
709 SILC_STR_UI_XNSTRING(id_string, SILC_ID_SERVER_LEN),
710 SILC_STR_UI_SHORT(strlen(server->server_name)),
711 SILC_STR_UI_XNSTRING(server->server_name,
712 strlen(server->server_name)),
715 /* Send the packet */
716 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
717 packet->data, packet->len, TRUE);
718 silc_buffer_free(packet);
719 silc_free(id_string);
721 SILC_LOG_DEBUG(("Connected to router %s", sock->hostname));
723 /* Add the connected router to local server list */
724 server->standalone = FALSE;
725 conn_data = (SilcUnknownEntry)sock->user_data;
727 silc_idlist_add_server(server->local_list,
728 sock->hostname ? sock->hostname : sock->ip,
729 SILC_ROUTER, ctx->dest_id, NULL,
730 conn_data->send_key, conn_data->receive_key,
731 conn_data->pkcs, conn_data->hmac, NULL, sock);
733 id_entry->hmac_key = conn_data->hmac_key;
734 id_entry->hmac_key_len = conn_data->hmac_key_len;
735 sock->user_data = (void *)id_entry;
736 sock->type = SILC_SOCKET_TYPE_ROUTER;
737 server->id_entry->router = id_entry;
740 /* Free the temporary connection data context from key exchange */
741 silc_free(conn_data);
743 /* Free the protocol object */
744 silc_protocol_free(protocol);
746 silc_buffer_free(ctx->packet);
748 silc_ske_free(ctx->ske);
750 sock->protocol = NULL;
753 /* Accepts new connections to the server. Accepting new connections are
754 done in three parts to make it async. */
756 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
758 SilcServer server = (SilcServer)context;
759 SilcSocketConnection newsocket;
760 SilcServerKEInternalContext *proto_ctx;
763 SILC_LOG_DEBUG(("Accepting new connection"));
765 sock = silc_net_accept_connection(server->sock);
767 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
771 /* Check max connections */
772 if (sock > SILC_SERVER_MAX_CONNECTIONS) {
773 if (server->config->redirect) {
774 /* XXX Redirecting connection to somewhere else now?? */
775 /*silc_server_send_notify("Server is full, trying to redirect..."); */
777 SILC_LOG_ERROR(("Refusing connection, server is full"));
782 /* Set socket options */
783 silc_net_set_socket_nonblock(sock);
784 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
786 /* We don't create a ID yet, since we don't know what type of connection
787 this is yet. But, we do add the connection to the socket table. */
788 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
789 server->sockets[sock] = newsocket;
791 /* XXX This MUST be done async as this will block the entire server
792 process. Either we have to do our own resolver stuff or in the future
793 we can use threads. */
794 /* Perform mandatory name and address lookups for the remote host. */
795 silc_net_check_host_by_sock(sock, &newsocket->hostname, &newsocket->ip);
796 if (!newsocket->ip || !newsocket->hostname) {
797 SILC_LOG_DEBUG(("IP lookup/DNS lookup failed"));
798 SILC_LOG_ERROR(("IP lookup/DNS lookup failed"));
802 SILC_LOG_INFO(("Incoming connection from %s (%s)", newsocket->hostname,
805 /* Allocate internal context for key exchange protocol. This is
806 sent as context for the protocol. */
807 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
808 proto_ctx->server = context;
809 proto_ctx->sock = newsocket;
810 proto_ctx->rng = server->rng;
811 proto_ctx->responder = TRUE;
813 /* Prepare the connection for key exchange protocol. We allocate the
814 protocol but will not start it yet. The connector will be the
815 initiator of the protocol thus we will wait for initiation from
816 there before we start the protocol. */
817 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
818 &newsocket->protocol, proto_ctx,
819 silc_server_accept_new_connection_second);
821 /* Register a timeout task that will be executed if the connector
822 will not start the key exchange protocol within 60 seconds. For
823 now, this is a hard coded limit. After 60 secs the connection will
824 be closed if the key exchange protocol has not been started. */
825 proto_ctx->timeout_task =
826 silc_task_register(server->timeout_queue, newsocket->sock,
827 silc_server_timeout_remote,
832 /* Register the connection for network input and output. This sets
833 that scheduler will listen for incoming packets for this connection
834 and sets that outgoing packets may be sent to this connection as well.
835 However, this doesn't set the scheduler for outgoing traffic, it
836 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
837 later when outgoing data is available. */
838 SILC_REGISTER_CONNECTION_FOR_IO(sock);
841 /* Second part of accepting new connection. Key exchange protocol has been
842 performed and now it is time to do little connection authentication
843 protocol to figure out whether this connection is client or server
844 and whether it has right to access this server (especially server
845 connections needs to be authenticated). */
847 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
849 SilcProtocol protocol = (SilcProtocol)context;
850 SilcServerKEInternalContext *ctx =
851 (SilcServerKEInternalContext *)protocol->context;
852 SilcServer server = (SilcServer)ctx->server;
853 SilcSocketConnection sock = NULL;
854 SilcServerConnAuthInternalContext *proto_ctx;
856 SILC_LOG_DEBUG(("Start"));
858 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
859 /* Error occured during protocol */
860 silc_protocol_free(protocol);
862 silc_buffer_free(ctx->packet);
864 silc_ske_free(ctx->ske);
866 silc_free(ctx->dest_id);
869 sock->protocol = NULL;
870 silc_server_disconnect_remote(server, sock, "Server closed connection: "
871 "Key exchange failed");
875 /* Allocate internal context for the authentication protocol. This
876 is sent as context for the protocol. */
877 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
878 proto_ctx->server = (void *)server;
879 proto_ctx->sock = sock = server->sockets[fd];
880 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
881 proto_ctx->responder = TRUE;
882 proto_ctx->dest_id_type = ctx->dest_id_type;
883 proto_ctx->dest_id = ctx->dest_id;
885 /* Free old protocol as it is finished now */
886 silc_protocol_free(protocol);
888 silc_buffer_free(ctx->packet);
890 sock->protocol = NULL;
892 /* Allocate the authentication protocol. This is allocated here
893 but we won't start it yet. We will be receiving party of this
894 protocol thus we will wait that connecting party will make
896 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
897 &sock->protocol, proto_ctx,
898 silc_server_accept_new_connection_final);
900 /* Register timeout task. If the protocol is not executed inside
901 this timelimit the connection will be terminated. Currently
902 this is 60 seconds and is hard coded limit (XXX). */
903 proto_ctx->timeout_task =
904 silc_task_register(server->timeout_queue, sock->sock,
905 silc_server_timeout_remote,
906 (void *)server, 60, 0,
911 /* Final part of accepting new connection. The connection has now
912 been authenticated and keys has been exchanged. We also know whether
913 this is client or server connection. */
915 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
917 SilcProtocol protocol = (SilcProtocol)context;
918 SilcServerConnAuthInternalContext *ctx =
919 (SilcServerConnAuthInternalContext *)protocol->context;
920 SilcServer server = (SilcServer)ctx->server;
921 SilcSocketConnection sock = ctx->sock;
923 SILC_LOG_DEBUG(("Start"));
925 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
926 /* Error occured during protocol */
927 silc_protocol_free(protocol);
929 silc_buffer_free(ctx->packet);
931 silc_ske_free(ctx->ske);
933 silc_free(ctx->dest_id);
936 sock->protocol = NULL;
937 silc_server_disconnect_remote(server, sock, "Server closed connection: "
938 "Authentication failed");
942 sock->type = ctx->conn_type;
944 case SILC_SOCKET_TYPE_CLIENT:
946 SilcClientEntry client;
947 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
949 SILC_LOG_DEBUG(("Remote host is client"));
950 SILC_LOG_INFO(("Connection from %s (%s) is client", sock->hostname,
953 /* Add the client to the client ID cache. The nickname and Client ID
954 and other information is created after we have received NEW_CLIENT
955 packet from client. */
957 silc_idlist_add_client(server->local_list, NULL, NULL, NULL, NULL,
958 NULL, conn_data->send_key,
959 conn_data->receive_key, conn_data->pkcs,
960 conn_data->hmac, NULL, sock);
962 SILC_LOG_ERROR(("Could not add new client to cache"));
963 silc_free(conn_data);
967 /* Free the temporary connection data context from key exchange */
968 silc_free(conn_data);
970 /* Add to sockets internal pointer for fast referencing */
971 sock->user_data = (void *)client;
974 case SILC_SOCKET_TYPE_SERVER:
975 case SILC_SOCKET_TYPE_ROUTER:
977 SilcServerEntry new_server;
978 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
980 SILC_LOG_DEBUG(("Remote host is %s",
981 sock->type == SILC_SOCKET_TYPE_SERVER ?
982 "server" : "router"));
983 SILC_LOG_INFO(("Connection from %s (%s) is %s", sock->hostname,
984 sock->ip, sock->type == SILC_SOCKET_TYPE_SERVER ?
985 "server" : "router"));
987 /* Add the server into server cache. The server name and Server ID
988 is updated after we have received NEW_SERVER packet from the
991 silc_idlist_add_server(server->local_list, NULL,
992 sock->type == SILC_SOCKET_TYPE_SERVER ?
993 SILC_SERVER : SILC_ROUTER, NULL, NULL,
994 conn_data->send_key, conn_data->receive_key,
995 conn_data->pkcs, conn_data->hmac, NULL, sock);
997 SILC_LOG_ERROR(("Could not add new server to cache"));
998 silc_free(conn_data);
1002 new_server->registered = TRUE;
1003 new_server->hmac_key = conn_data->hmac_key;
1004 new_server->hmac_key_len = conn_data->hmac_key_len;
1006 /* Free the temporary connection data context from protocols */
1007 silc_free(conn_data);
1009 /* Add to sockets internal pointer for fast referencing */
1010 sock->user_data = (void *)new_server;
1012 /* There is connection to other server now, if it is router then
1013 we will have connection to outside world. If we are router but
1014 normal server connected to us then we will remain standalone,
1015 if we are standlone. */
1016 if (server->standalone && sock->type == SILC_SOCKET_TYPE_ROUTER) {
1017 SILC_LOG_DEBUG(("We are not standalone server anymore"));
1018 server->standalone = FALSE;
1026 /* Connection has been fully established now. Everything is ok. */
1027 SILC_LOG_DEBUG(("New connection authenticated"));
1029 silc_protocol_free(protocol);
1031 silc_buffer_free(ctx->packet);
1033 silc_ske_free(ctx->ske);
1035 silc_free(ctx->dest_id);
1037 sock->protocol = NULL;
1040 /* Internal routine that sends packet or marks packet to be sent. This
1041 is used directly only in special cases. Normal cases should use
1042 silc_server_packet_send. Returns < 0 error. */
1044 static int silc_server_packet_send_real(SilcServer server,
1045 SilcSocketConnection sock,
1050 /* Send the packet */
1051 ret = silc_packet_send(sock, force_send);
1055 /* Mark that there is some outgoing data available for this connection.
1056 This call sets the connection both for input and output (the input
1057 is set always and this call keeps the input setting, actually).
1058 Actual data sending is performed by silc_server_packet_process. */
1059 SILC_SET_CONNECTION_FOR_OUTPUT(sock->sock);
1061 /* Mark to socket that data is pending in outgoing buffer. This flag
1062 is needed if new data is added to the buffer before the earlier
1063 put data is sent to the network. */
1064 SILC_SET_OUTBUF_PENDING(sock);
1070 SilcPacketContext *packetdata;
1072 SilcSocketConnection sock;
1075 } SilcServerInternalPacket;
1077 /* This function is used to read packets from network and send packets to
1078 network. This is usually a generic task. */
1080 SILC_TASK_CALLBACK(silc_server_packet_process)
1082 SilcServer server = (SilcServer)context;
1083 SilcSocketConnection sock = server->sockets[fd];
1084 SilcCipher cipher = NULL;
1085 SilcHmac hmac = NULL;
1088 SILC_LOG_DEBUG(("Processing packet"));
1090 /* Packet sending */
1091 if (type == SILC_TASK_WRITE) {
1092 SILC_LOG_DEBUG(("Writing data to connection"));
1094 if (sock->outbuf->data - sock->outbuf->head)
1095 silc_buffer_push(sock->outbuf,
1096 sock->outbuf->data - sock->outbuf->head);
1098 ret = silc_server_packet_send_real(server, sock, TRUE);
1100 /* If returned -2 could not write to connection now, will do
1105 /* The packet has been sent and now it is time to set the connection
1106 back to only for input. When there is again some outgoing data
1107 available for this connection it will be set for output as well.
1108 This call clears the output setting and sets it only for input. */
1109 SILC_SET_CONNECTION_FOR_INPUT(fd);
1110 SILC_UNSET_OUTBUF_PENDING(sock);
1112 silc_buffer_clear(sock->outbuf);
1116 /* Packet receiving */
1117 if (type == SILC_TASK_READ) {
1118 SILC_LOG_DEBUG(("Reading data from connection"));
1120 /* Read some data from connection */
1121 ret = silc_packet_receive(sock);
1127 SILC_LOG_DEBUG(("Read EOF"));
1129 /* If connection is disconnecting already we will finally
1130 close the connection */
1131 if (SILC_IS_DISCONNECTING(sock)) {
1132 if (sock->user_data)
1133 silc_server_free_sock_user_data(server, sock);
1134 silc_server_close_connection(server, sock);
1138 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1140 if (sock->user_data)
1141 silc_server_free_sock_user_data(server, sock);
1142 silc_server_close_connection(server, sock);
1146 /* If connection is disconnecting or disconnected we will ignore
1148 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1149 SILC_LOG_DEBUG(("Ignoring read data from invalid connection"));
1153 switch (sock->type) {
1154 case SILC_SOCKET_TYPE_CLIENT:
1156 SilcClientEntry clnt = (SilcClientEntry)sock->user_data;
1160 clnt->last_receive = time(NULL);
1162 cipher = clnt->receive_key;
1166 case SILC_SOCKET_TYPE_SERVER:
1167 case SILC_SOCKET_TYPE_ROUTER:
1169 SilcServerEntry srvr = (SilcServerEntry)sock->user_data;
1173 srvr->last_receive = time(NULL);
1175 cipher = srvr->receive_key;
1179 case SILC_SOCKET_TYPE_UNKNOWN:
1181 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
1185 cipher = conn_data->receive_key;
1186 hmac = conn_data->hmac;
1193 /* Process the packet. This will call the parser that will then
1194 decrypt and parse the packet. */
1195 if (!silc_packet_receive_process(sock, cipher, hmac,
1196 silc_server_packet_parse, server)) {
1197 silc_buffer_clear(sock->inbuf);
1203 /* Parses whole packet, received earlier. */
1205 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1207 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1208 SilcServer server = (SilcServer)parse_ctx->context;
1209 SilcSocketConnection sock = parse_ctx->sock;
1210 SilcPacketContext *packet = parse_ctx->packet;
1211 SilcBuffer buffer = packet->buffer;
1214 SILC_LOG_DEBUG(("Start"));
1216 /* Decrypt the received packet */
1217 ret = silc_packet_decrypt(parse_ctx->cipher, parse_ctx->hmac,
1223 /* Parse the packet. Packet type is returned. */
1224 ret = silc_packet_parse(packet);
1226 /* Parse the packet header in special way as this is "special"
1228 ret = silc_packet_parse_special(packet);
1231 if (ret == SILC_PACKET_NONE)
1234 /* Parse the incoming packet type */
1235 silc_server_packet_parse_type(server, sock, packet);
1238 silc_buffer_clear(sock->inbuf);
1240 silc_free(parse_ctx);
1243 /* Parser callback called by silc_packet_receive_process. This merely
1244 registers timeout that will handle the actual parsing whem appropriate. */
1246 void silc_server_packet_parse(SilcPacketParserContext *parser_context)
1248 SilcServer server = (SilcServer)parser_context->context;
1249 SilcSocketConnection sock = parser_context->sock;
1251 switch (sock->type) {
1252 case SILC_SOCKET_TYPE_CLIENT:
1253 case SILC_SOCKET_TYPE_UNKNOWN:
1254 /* Parse the packet with timeout */
1255 silc_task_register(server->timeout_queue, sock->sock,
1256 silc_server_packet_parse_real,
1257 (void *)parser_context, 0, 100000,
1259 SILC_TASK_PRI_NORMAL);
1261 case SILC_SOCKET_TYPE_SERVER:
1262 case SILC_SOCKET_TYPE_ROUTER:
1263 /* Packets from servers are parsed as soon as possible */
1264 silc_task_register(server->timeout_queue, sock->sock,
1265 silc_server_packet_parse_real,
1266 (void *)parser_context, 0, 1,
1268 SILC_TASK_PRI_NORMAL);
1275 /* Parses the packet type and calls what ever routines the packet type
1276 requires. This is done for all incoming packets. */
1278 void silc_server_packet_parse_type(SilcServer server,
1279 SilcSocketConnection sock,
1280 SilcPacketContext *packet)
1282 SilcBuffer buffer = packet->buffer;
1283 SilcPacketType type = packet->type;
1285 SILC_LOG_DEBUG(("Parsing packet type %d", type));
1287 /* Parse the packet type */
1289 case SILC_PACKET_DISCONNECT:
1290 SILC_LOG_DEBUG(("Disconnect packet"));
1292 case SILC_PACKET_SUCCESS:
1294 * Success received for something. For now we can have only
1295 * one protocol for connection executing at once hence this
1296 * success message is for whatever protocol is executing currently.
1298 SILC_LOG_DEBUG(("Success packet"));
1299 if (sock->protocol) {
1300 sock->protocol->execute(server->timeout_queue, 0,
1301 sock->protocol, sock->sock, 0, 0);
1304 case SILC_PACKET_FAILURE:
1305 SILC_LOG_DEBUG(("Failure packet"));
1307 case SILC_PACKET_REJECT:
1308 SILC_LOG_DEBUG(("Reject packet"));
1315 case SILC_PACKET_CHANNEL_MESSAGE:
1317 * Received channel message. Channel messages are special packets
1318 * (although probably most common ones) hence they are handled
1321 SILC_LOG_DEBUG(("Channel Message packet"));
1322 silc_server_channel_message(server, sock, packet);
1325 case SILC_PACKET_CHANNEL_KEY:
1327 * Received key for channel. As channels are created by the router
1328 * the keys are as well. We will distribute the key to all of our
1329 * locally connected clients on the particular channel. Router
1330 * never receives this channel and thus is ignored.
1332 SILC_LOG_DEBUG(("Channel Key packet"));
1333 silc_server_channel_key(server, sock, packet);
1339 case SILC_PACKET_COMMAND:
1341 * Recived command. Allocate command context and execute the command.
1343 SILC_LOG_DEBUG(("Command packet"));
1344 silc_server_command_process(server, sock, packet);
1347 case SILC_PACKET_COMMAND_REPLY:
1349 * Received command reply packet. Servers never send commands thus
1350 * they don't receive command reply packets either, except in cases
1351 * where server has forwarded command packet coming from client.
1352 * This must be the case here or we will ignore the packet.
1354 SILC_LOG_DEBUG(("Command Reply packet"));
1355 silc_server_packet_relay_command_reply(server, sock, packet);
1359 * Private Message packets
1361 case SILC_PACKET_PRIVATE_MESSAGE:
1363 * Received private message packet. The packet is coming from either
1366 SILC_LOG_DEBUG(("Private Message packet"));
1367 silc_server_private_message(server, sock, packet);
1370 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1374 * Key Exchange protocol packets
1376 case SILC_PACKET_KEY_EXCHANGE:
1377 SILC_LOG_DEBUG(("KE packet"));
1378 if (sock->protocol && sock->protocol->protocol->type
1379 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1381 SilcServerKEInternalContext *proto_ctx =
1382 (SilcServerKEInternalContext *)sock->protocol->context;
1384 proto_ctx->packet = buffer;
1386 /* Let the protocol handle the packet */
1387 sock->protocol->execute(server->timeout_queue, 0,
1388 sock->protocol, sock->sock, 0, 100000);
1390 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1391 "protocol active, packet dropped."));
1393 /* XXX Trigger KE protocol?? Rekey actually, maybe. */
1397 case SILC_PACKET_KEY_EXCHANGE_1:
1398 SILC_LOG_DEBUG(("KE 1 packet"));
1399 if (sock->protocol && sock->protocol->protocol->type
1400 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1402 SilcServerKEInternalContext *proto_ctx =
1403 (SilcServerKEInternalContext *)sock->protocol->context;
1405 if (proto_ctx->packet)
1406 silc_buffer_free(proto_ctx->packet);
1408 proto_ctx->packet = buffer;
1409 proto_ctx->dest_id_type = packet->src_id_type;
1410 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
1412 /* Let the protocol handle the packet */
1413 sock->protocol->execute(server->timeout_queue, 0,
1414 sock->protocol, sock->sock,
1417 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1418 "protocol active, packet dropped."));
1422 case SILC_PACKET_KEY_EXCHANGE_2:
1423 SILC_LOG_DEBUG(("KE 2 packet"));
1424 if (sock->protocol && sock->protocol->protocol->type
1425 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1427 SilcServerKEInternalContext *proto_ctx =
1428 (SilcServerKEInternalContext *)sock->protocol->context;
1430 if (proto_ctx->packet)
1431 silc_buffer_free(proto_ctx->packet);
1433 proto_ctx->packet = buffer;
1434 proto_ctx->dest_id_type = packet->src_id_type;
1435 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
1437 /* Let the protocol handle the packet */
1438 sock->protocol->execute(server->timeout_queue, 0,
1439 sock->protocol, sock->sock,
1442 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1443 "protocol active, packet dropped."));
1447 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1448 /* If we receive this packet we will send to the other end information
1449 about our mandatory authentication method for the connection.
1450 This packet maybe received at any time. */
1453 * Connection Authentication protocol packets
1455 case SILC_PACKET_CONNECTION_AUTH:
1456 /* Start of the authentication protocol. We receive here the
1457 authentication data and will verify it. */
1458 SILC_LOG_DEBUG(("Connection auth packet"));
1459 if (sock->protocol && sock->protocol->protocol->type
1460 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
1462 SilcServerConnAuthInternalContext *proto_ctx =
1463 (SilcServerConnAuthInternalContext *)sock->protocol->context;
1465 proto_ctx->packet = buffer;
1467 /* Let the protocol handle the packet */
1468 sock->protocol->execute(server->timeout_queue, 0,
1469 sock->protocol, sock->sock, 0, 0);
1471 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
1472 "protocol active, packet dropped."));
1476 case SILC_PACKET_NEW_ID:
1478 * Received New ID packet. This includes some new ID that has been
1479 * created. It may be for client, server or channel. This is the way
1480 * to distribute information about new registered entities in the
1483 SILC_LOG_DEBUG(("New ID packet"));
1484 silc_server_new_id(server, sock, packet);
1487 case SILC_PACKET_NEW_CLIENT:
1489 * Received new client packet. This includes client information that
1490 * we will use to create initial client ID. After creating new
1491 * ID we will send it to the client.
1493 SILC_LOG_DEBUG(("New Client packet"));
1494 silc_server_new_client(server, sock, packet);
1497 case SILC_PACKET_NEW_SERVER:
1499 * Received new server packet. This includes Server ID and some other
1500 * information that we may save. This is after server as connected to us.
1502 SILC_LOG_DEBUG(("New Server packet"));
1503 silc_server_new_server(server, sock, packet);
1507 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
1513 /* Assembles a new packet to be sent out to network. This doesn't actually
1514 send the packet but creates the packet and fills the outgoing data
1515 buffer and marks the packet ready to be sent to network. However, If
1516 argument force_send is TRUE the packet is sent immediately and not put
1517 to queue. Normal case is that the packet is not sent immediately. */
1519 void silc_server_packet_send(SilcServer server,
1520 SilcSocketConnection sock,
1521 SilcPacketType type,
1522 SilcPacketFlags flags,
1523 unsigned char *data,
1524 unsigned int data_len,
1527 void *dst_id = NULL;
1528 SilcIdType dst_id_type = SILC_ID_NONE;
1533 /* Get data used in the packet sending, keys and stuff */
1534 switch(sock->type) {
1535 case SILC_SOCKET_TYPE_CLIENT:
1536 if (((SilcClientEntry)sock->user_data)->id) {
1537 dst_id = ((SilcClientEntry)sock->user_data)->id;
1538 dst_id_type = SILC_ID_CLIENT;
1541 case SILC_SOCKET_TYPE_SERVER:
1542 case SILC_SOCKET_TYPE_ROUTER:
1543 if (((SilcServerEntry)sock->user_data)->id) {
1544 dst_id = ((SilcServerEntry)sock->user_data)->id;
1545 dst_id_type = SILC_ID_SERVER;
1552 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
1553 dst_id_type, data, data_len, force_send);
1556 /* Assembles a new packet to be sent out to network. This doesn't actually
1557 send the packet but creates the packet and fills the outgoing data
1558 buffer and marks the packet ready to be sent to network. However, If
1559 argument force_send is TRUE the packet is sent immediately and not put
1560 to queue. Normal case is that the packet is not sent immediately.
1561 Destination information is sent as argument for this function. */
1563 void silc_server_packet_send_dest(SilcServer server,
1564 SilcSocketConnection sock,
1565 SilcPacketType type,
1566 SilcPacketFlags flags,
1568 SilcIdType dst_id_type,
1569 unsigned char *data,
1570 unsigned int data_len,
1573 SilcPacketContext packetdata;
1574 SilcCipher cipher = NULL;
1575 SilcHmac hmac = NULL;
1576 unsigned char *dst_id_data = NULL;
1577 unsigned int dst_id_len = 0;
1579 SILC_LOG_DEBUG(("Sending packet, type %d", type));
1581 /* Get data used in the packet sending, keys and stuff */
1582 switch(sock->type) {
1583 case SILC_SOCKET_TYPE_CLIENT:
1584 if (sock->user_data) {
1585 cipher = ((SilcClientEntry)sock->user_data)->send_key;
1586 hmac = ((SilcClientEntry)sock->user_data)->hmac;
1589 case SILC_SOCKET_TYPE_SERVER:
1590 case SILC_SOCKET_TYPE_ROUTER:
1591 if (sock->user_data) {
1592 cipher = ((SilcServerEntry)sock->user_data)->send_key;
1593 hmac = ((SilcServerEntry)sock->user_data)->hmac;
1597 if (sock->user_data) {
1598 /* We don't know what type of connection this is thus it must
1599 be in authentication phase. */
1600 cipher = ((SilcUnknownEntry)sock->user_data)->send_key;
1601 hmac = ((SilcUnknownEntry)sock->user_data)->hmac;
1607 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
1608 dst_id_len = silc_id_get_len(dst_id_type);
1611 /* Set the packet context pointers */
1612 packetdata.type = type;
1613 packetdata.flags = flags;
1614 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
1615 packetdata.src_id_len = SILC_ID_SERVER_LEN;
1616 packetdata.src_id_type = server->id_type;
1617 packetdata.dst_id = dst_id_data;
1618 packetdata.dst_id_len = dst_id_len;
1619 packetdata.dst_id_type = dst_id_type;
1620 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1621 packetdata.src_id_len + dst_id_len;
1622 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1623 packetdata.rng = server->rng;
1625 /* Prepare outgoing data buffer for packet sending */
1626 silc_packet_send_prepare(sock,
1627 SILC_PACKET_HEADER_LEN +
1628 packetdata.src_id_len +
1629 packetdata.dst_id_len,
1633 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
1635 packetdata.buffer = sock->outbuf;
1637 /* Put the data to the buffer */
1638 if (data && data_len)
1639 silc_buffer_put(sock->outbuf, data, data_len);
1641 /* Create the outgoing packet */
1642 silc_packet_assemble(&packetdata);
1644 /* Encrypt the packet */
1646 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
1648 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
1649 sock->outbuf->data, sock->outbuf->len);
1651 /* Now actually send the packet */
1652 silc_server_packet_send_real(server, sock, force_send);
1654 if (packetdata.src_id)
1655 silc_free(packetdata.src_id);
1656 if (packetdata.dst_id)
1657 silc_free(packetdata.dst_id);
1660 /* Forwards packet. Packets sent with this function will be marked as
1661 forwarded (in the SILC header flags) so that the receiver knows that
1662 we have forwarded the packet to it. Forwarded packets are handled
1663 specially by the receiver as they are not destined to the receiver
1664 originally. However, the receiver knows this because the forwarded
1665 flag has been set (and the flag is authenticated). */
1667 void silc_server_packet_forward(SilcServer server,
1668 SilcSocketConnection sock,
1669 unsigned char *data, unsigned int data_len,
1672 SilcCipher cipher = NULL;
1673 SilcHmac hmac = NULL;
1675 SILC_LOG_DEBUG(("Forwarding packet"));
1677 /* Get data used in the packet sending, keys and stuff */
1678 switch(sock->type) {
1679 case SILC_SOCKET_TYPE_CLIENT:
1680 if (sock->user_data) {
1681 cipher = ((SilcClientEntry )sock->user_data)->send_key;
1682 hmac = ((SilcClientEntry )sock->user_data)->hmac;
1685 case SILC_SOCKET_TYPE_SERVER:
1686 case SILC_SOCKET_TYPE_ROUTER:
1687 if (sock->user_data) {
1688 cipher = ((SilcServerEntry )sock->user_data)->send_key;
1689 hmac = ((SilcServerEntry )sock->user_data)->hmac;
1693 /* We won't forward to unknown destination - keys must exist with
1694 the destination before forwarding. */
1698 /* Prepare outgoing data buffer for packet sending */
1699 silc_packet_send_prepare(sock, 0, 0, data_len);
1701 /* Mungle the packet flags and add the FORWARDED flag */
1703 data[2] |= (unsigned char)SILC_PACKET_FLAG_FORWARDED;
1705 /* Put the data to the buffer */
1706 if (data && data_len)
1707 silc_buffer_put(sock->outbuf, data, data_len);
1709 /* Encrypt the packet */
1711 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
1713 SILC_LOG_HEXDUMP(("Forwarded packet, len %d", sock->outbuf->len),
1714 sock->outbuf->data, sock->outbuf->len);
1716 /* Now actually send the packet */
1717 silc_server_packet_send_real(server, sock, force_send);
1720 /* Internal routine to actually create the channel packet and send it
1721 to network. This is common function in channel message sending. */
1724 silc_server_packet_send_to_channel_real(SilcServer server,
1725 SilcSocketConnection sock,
1726 SilcPacketContext *packet,
1729 unsigned char *data,
1730 unsigned int data_len,
1733 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
1734 packet->src_id_len + packet->dst_id_len;
1736 /* Prepare outgoing data buffer for packet sending */
1737 silc_packet_send_prepare(sock,
1738 SILC_PACKET_HEADER_LEN +
1739 packet->src_id_len +
1744 packet->buffer = sock->outbuf;
1746 /* Put the data to buffer, assemble and encrypt the packet. The packet
1747 is encrypted with normal session key shared with the client. */
1748 silc_buffer_put(sock->outbuf, data, data_len);
1749 silc_packet_assemble(packet);
1750 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
1751 packet->src_id_len + packet->dst_id_len +
1754 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
1755 sock->outbuf->data, sock->outbuf->len);
1757 /* Now actually send the packet */
1758 silc_server_packet_send_real(server, sock, force_send);
1761 /* This routine is used by the server to send packets to channel. The
1762 packet sent with this function is distributed to all clients on
1763 the channel. Usually this is used to send notify messages to the
1764 channel, things like notify about new user joining to the channel. */
1766 void silc_server_packet_send_to_channel(SilcServer server,
1767 SilcChannelEntry channel,
1768 unsigned char *data,
1769 unsigned int data_len,
1773 SilcSocketConnection sock = NULL;
1774 SilcPacketContext packetdata;
1775 SilcClientEntry client = NULL;
1776 SilcServerEntry *routed = NULL;
1777 unsigned int routed_count = 0;
1782 SILC_LOG_DEBUG(("Sending packet to channel"));
1785 for (i = 0; i < 16; i++)
1786 channel->iv[i] = silc_rng_get_byte(server->rng);
1788 /* Encode the channel payload */
1789 payload = silc_channel_encode_payload(0, "", data_len, data,
1790 16, channel->iv, server->rng);
1792 SILC_LOG_ERROR(("Could not encode channel payload, message not sent"));
1796 /* Encrypt payload of the packet. This is encrypted with the
1798 channel->channel_key->cipher->encrypt(channel->channel_key->context,
1799 payload->data, payload->data,
1800 payload->len - 16, /* -IV_LEN */
1803 /* Set the packet context pointers. */
1804 packetdata.flags = 0;
1805 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
1806 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1807 packetdata.src_id_len = SILC_ID_SERVER_LEN;
1808 packetdata.src_id_type = SILC_ID_SERVER;
1809 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1810 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
1811 packetdata.dst_id_type = SILC_ID_CHANNEL;
1812 packetdata.rng = server->rng;
1813 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
1814 packetdata.src_id_len +
1815 packetdata.dst_id_len));
1817 /* If there are global users in the channel we will send the message
1818 first to our router for further routing. */
1819 if (server->server_type == SILC_SERVER && !server->standalone &&
1820 channel->global_users) {
1821 SilcServerEntry router;
1823 /* Get data used in packet header encryption, keys and stuff. */
1824 router = server->id_entry->router;
1825 sock = (SilcSocketConnection)router->connection;
1826 cipher = router->send_key;
1827 hmac = router->hmac;
1829 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
1831 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1832 cipher, hmac, payload->data,
1833 payload->len, force_send);
1836 /* Send the message to clients on the channel's client list. */
1837 for (i = 0; i < channel->user_list_count; i++) {
1838 client = channel->user_list[i].client;
1840 /* If client has router set it is not locally connected client and
1841 we will route the message to the router set in the client. */
1842 if (client && client->router && server->server_type == SILC_ROUTER) {
1845 /* Check if we have sent the packet to this route already */
1846 for (k = 0; k < routed_count; k++)
1847 if (routed[k] == client->router)
1850 /* Get data used in packet header encryption, keys and stuff. */
1851 sock = (SilcSocketConnection)client->router->connection;
1852 cipher = client->router->send_key;
1853 hmac = client->router->hmac;
1855 /* Send the packet */
1856 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1857 cipher, hmac, payload->data,
1858 payload->len, force_send);
1860 /* We want to make sure that the packet is routed to same router
1861 only once. Mark this route as sent route. */
1863 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1864 routed[k] = client->router;
1870 /* Send to locally connected client */
1873 /* XXX Check client's mode on the channel. */
1875 /* Get data used in packet header encryption, keys and stuff. */
1876 sock = (SilcSocketConnection)client->connection;
1877 cipher = client->send_key;
1878 hmac = client->hmac;
1880 /* Send the packet */
1881 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1882 cipher, hmac, payload->data,
1883 payload->len, force_send);
1889 silc_free(packetdata.src_id);
1890 silc_free(packetdata.dst_id);
1891 silc_buffer_free(payload);
1894 /* This routine is explicitly used to relay messages to some channel.
1895 Packets sent with this function we have received earlier and are
1896 totally encrypted. This just sends the packet to all clients on
1897 the channel. If the sender of the packet is someone on the channel
1898 the message will not be sent to that client. The SILC Packet header
1899 is encrypted with the session key shared between us and the client.
1900 MAC is also computed before encrypting the header. Rest of the
1901 packet will be untouched. */
1903 void silc_server_packet_relay_to_channel(SilcServer server,
1904 SilcSocketConnection sender_sock,
1905 SilcChannelEntry channel,
1907 SilcIdType sender_type,
1908 unsigned char *data,
1909 unsigned int data_len,
1912 int i, found = FALSE;
1913 SilcSocketConnection sock = NULL;
1914 SilcPacketContext packetdata;
1915 SilcClientEntry client = NULL;
1916 SilcServerEntry *routed = NULL;
1917 unsigned int routed_count = 0;
1921 SILC_LOG_DEBUG(("Relaying packet to channel"));
1923 SILC_LOG_HEXDUMP(("XXX %d", data_len), data, data_len);
1925 /* Set the packet context pointers. */
1926 packetdata.flags = 0;
1927 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
1928 packetdata.src_id = silc_id_id2str(sender, sender_type);
1929 packetdata.src_id_len = silc_id_get_len(sender_type);
1930 packetdata.src_id_type = sender_type;
1931 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1932 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
1933 packetdata.dst_id_type = SILC_ID_CHANNEL;
1934 packetdata.rng = server->rng;
1935 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
1936 packetdata.src_id_len +
1937 packetdata.dst_id_len));
1939 /* If there are global users in the channel we will send the message
1940 first to our router for further routing. */
1941 if (server->server_type == SILC_SERVER && !server->standalone &&
1942 channel->global_users) {
1943 SilcServerEntry router;
1945 router = server->id_entry->router;
1947 /* Check that the sender is not our router. */
1948 if (sender_sock != (SilcSocketConnection)router->connection) {
1950 /* Get data used in packet header encryption, keys and stuff. */
1951 sock = (SilcSocketConnection)router->connection;
1952 cipher = router->send_key;
1953 hmac = router->hmac;
1955 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
1957 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1959 data_len, force_send);
1963 /* Send the message to clients on the channel's client list. */
1964 for (i = 0; i < channel->user_list_count; i++) {
1965 client = channel->user_list[i].client;
1969 /* If sender is one on the channel do not send it the packet. */
1970 if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
1975 /* If the client has set router it means that it is not locally
1976 connected client and we will route the packet further. */
1977 if (server->server_type == SILC_ROUTER && client->router) {
1980 /* Sender maybe server as well so we want to make sure that
1981 we won't send the message to the server it came from. */
1982 if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
1987 /* Check if we have sent the packet to this route already */
1988 for (k = 0; k < routed_count; k++)
1989 if (routed[k] == client->router)
1992 /* Get data used in packet header encryption, keys and stuff. */
1993 sock = (SilcSocketConnection)client->router->connection;
1994 cipher = client->router->send_key;
1995 hmac = client->router->hmac;
1997 /* Send the packet */
1998 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
2000 data_len, force_send);
2002 /* We want to make sure that the packet is routed to same router
2003 only once. Mark this route as sent route. */
2005 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
2006 routed[k] = client->router;
2012 /* XXX Check client's mode on the channel. */
2014 /* Get data used in packet header encryption, keys and stuff. */
2015 sock = (SilcSocketConnection)client->connection;
2016 cipher = client->send_key;
2017 hmac = client->hmac;
2019 SILC_LOG_DEBUG(("Sending packet to client %s",
2020 sock->hostname ? sock->hostname : sock->ip));
2022 /* Send the packet */
2023 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
2025 data_len, force_send);
2029 silc_free(packetdata.src_id);
2030 silc_free(packetdata.dst_id);
2033 /* This function is used to send packets strictly to all local clients
2034 on a particular channel. This is used for example to distribute new
2035 channel key to all our locally connected clients on the channel.
2036 The packets are always encrypted with the session key shared between
2039 void silc_server_packet_send_local_channel(SilcServer server,
2040 SilcChannelEntry channel,
2041 SilcPacketType type,
2042 SilcPacketFlags flags,
2043 unsigned char *data,
2044 unsigned int data_len,
2048 SilcClientEntry client;
2049 SilcSocketConnection sock = NULL;
2051 SILC_LOG_DEBUG(("Start"));
2053 /* Send the message to clients on the channel's client list. */
2054 for (i = 0; i < channel->user_list_count; i++) {
2055 client = channel->user_list[i].client;
2058 sock = (SilcSocketConnection)client->connection;
2060 /* Send the packet to the client */
2061 silc_server_packet_send_dest(server, sock, type, flags, client->id,
2062 SILC_ID_CLIENT, data, data_len,
2068 /* Relays received command reply packet to the correct destination. The
2069 destination must be one of our locally connected client or the packet
2070 will be ignored. This is called when server has forwarded one of
2071 client's command request to router and router has now replied to the
2074 void silc_server_packet_relay_command_reply(SilcServer server,
2075 SilcSocketConnection sock,
2076 SilcPacketContext *packet)
2078 SilcBuffer buffer = packet->buffer;
2079 SilcClientEntry client;
2081 SilcSocketConnection dst_sock;
2083 SILC_LOG_DEBUG(("Start"));
2085 /* Source must be server or router */
2086 /* XXX: actually it must be only router */
2087 if (packet->src_id_type != SILC_ID_SERVER &&
2088 (sock->type != SILC_SOCKET_TYPE_SERVER ||
2089 sock->type != SILC_SOCKET_TYPE_ROUTER))
2092 /* Destination must be client */
2093 if (packet->dst_id_type != SILC_ID_CLIENT)
2096 /* Execute command reply locally for the command */
2097 silc_server_command_reply_process(server, sock, buffer);
2099 id = silc_id_str2id(packet->dst_id, SILC_ID_CLIENT);
2101 /* Destination must be one of ours */
2102 client = silc_idlist_find_client_by_id(server->local_list, id);
2108 /* Relay the packet to the client */
2110 dst_sock = (SilcSocketConnection)client->connection;
2111 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2112 + packet->dst_id_len + packet->padlen);
2114 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2115 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2117 /* Encrypt packet */
2118 if (client && client->send_key)
2119 silc_packet_encrypt(client->send_key, client->hmac,
2120 dst_sock->outbuf, buffer->len);
2122 /* Send the packet */
2123 silc_server_packet_send_real(server, dst_sock, FALSE);
2128 silc_buffer_free(buffer);
2131 /* Closes connection to socket connection */
2133 void silc_server_close_connection(SilcServer server,
2134 SilcSocketConnection sock)
2137 SILC_LOG_DEBUG(("Closing connection %d", sock->sock));
2139 /* We won't listen for this connection anymore */
2140 silc_schedule_unset_listen_fd(sock->sock);
2142 /* Unregister all tasks */
2143 silc_task_unregister_by_fd(server->io_queue, sock->sock);
2144 silc_task_unregister_by_fd(server->timeout_queue, sock->sock);
2146 /* Close the actual connection */
2147 silc_net_close_connection(sock->sock);
2148 server->sockets[sock->sock] = NULL;
2149 silc_socket_free(sock);
2152 /* Sends disconnect message to remote connection and disconnects the
2155 void silc_server_disconnect_remote(SilcServer server,
2156 SilcSocketConnection sock,
2157 const char *fmt, ...)
2160 unsigned char buf[4096];
2162 memset(buf, 0, sizeof(buf));
2164 vsprintf(buf, fmt, ap);
2167 SILC_LOG_DEBUG(("Disconnecting remote host"));
2169 /* Notify remote end that the conversation is over. The notify message
2170 is tried to be sent immediately. */
2171 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2172 buf, strlen(buf), TRUE);
2174 /* Mark the connection to be disconnected */
2175 SILC_SET_DISCONNECTED(sock);
2176 silc_server_close_connection(server, sock);
2179 /* Free's user_data pointer from socket connection object. As this
2180 pointer maybe anything we wil switch here to find the corrent
2181 data type and free it the way it needs to be free'd. */
2183 void silc_server_free_sock_user_data(SilcServer server,
2184 SilcSocketConnection sock)
2186 SILC_LOG_DEBUG(("Start"));
2188 switch(sock->type) {
2189 case SILC_SOCKET_TYPE_CLIENT:
2191 SilcClientEntry user_data = (SilcClientEntry )sock->user_data;
2193 /* Remove client from all channels */
2194 silc_server_remove_from_channels(server, sock, user_data);
2196 /* Free the client entry and everything in it */
2197 /* XXX must take some info to history before freeing */
2198 silc_idlist_del_client(server->local_list, user_data);
2201 case SILC_SOCKET_TYPE_SERVER:
2202 case SILC_SOCKET_TYPE_ROUTER:
2210 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2212 if (user_data->send_key)
2213 silc_cipher_free(user_data->send_key);
2214 if (user_data->receive_key)
2215 silc_cipher_free(user_data->receive_key);
2216 if (user_data->pkcs)
2217 silc_pkcs_free(user_data->pkcs);
2218 if (user_data->hmac) {
2219 silc_hmac_free(user_data->hmac);
2220 memset(user_data->hmac_key, 0, user_data->hmac_key_len);
2221 silc_free(user_data->hmac_key);
2223 silc_free(user_data);
2228 sock->user_data = NULL;
2233 /* Removes client from all channels it has joined. This is used when
2234 client connection is disconnected. If the client on a channel
2235 is last, the channel is removed as well. */
2237 void silc_server_remove_from_channels(SilcServer server,
2238 SilcSocketConnection sock,
2239 SilcClientEntry client)
2242 SilcChannelEntry channel;
2244 /* Remove the client from all channels. The client is removed from
2245 the channels' user list. */
2246 for (i = 0; i < client->channel_count; i++) {
2247 channel = client->channel[i];
2251 /* Remove from channel */
2252 for (k = 0; k < channel->user_list_count; k++) {
2253 if (channel->user_list[k].client == client) {
2255 /* If this client is last one on the channel the channel
2256 is removed all together. */
2257 if (channel->user_list_count == 1) {
2258 silc_idlist_del_channel(server->local_list, channel);
2262 channel->user_list[k].client = NULL;
2263 channel->user_list[k].mode = SILC_CHANNEL_UMODE_NONE;
2265 /* Send notify to channel about client leaving SILC and thus
2266 the entire channel. */
2267 silc_server_send_notify_to_channel(server, channel,
2274 if (client->channel_count)
2275 silc_free(client->channel);
2276 client->channel = NULL;
2279 /* Removes client from one channel. This is used for example when client
2280 calls LEAVE command to remove itself from the channel. Returns TRUE
2281 if channel still exists and FALSE if the channel is removed when
2282 last client leaves the channel. */
2284 int silc_server_remove_from_one_channel(SilcServer server,
2285 SilcSocketConnection sock,
2286 SilcChannelEntry channel,
2287 SilcClientEntry client)
2290 SilcChannelEntry ch;
2292 /* Remove the client from the channel. The client is removed from
2293 the channel's user list. */
2294 for (i = 0; i < client->channel_count; i++) {
2295 ch = client->channel[i];
2296 if (!ch || ch != channel)
2300 client->channel[i] = NULL;
2302 /* Remove from channel */
2303 for (k = 0; k < channel->user_list_count; k++) {
2304 if (channel->user_list[k].client == client) {
2306 /* If this client is last one on the channel the channel
2307 is removed all together. */
2308 if (channel->user_list_count == 1) {
2309 silc_idlist_del_channel(server->local_list, channel);
2313 channel->user_list[k].client = NULL;
2314 channel->user_list[k].mode = SILC_CHANNEL_UMODE_NONE;
2316 /* Send notify to channel about client leaving the channel */
2317 silc_server_send_notify_to_channel(server, channel,
2318 "%s has left channel %s",
2320 channel->channel_name);
2328 /* Returns TRUE if the given client is on the channel. FALSE if not.
2329 This works because we assure that the user list on the channel is
2330 always in up to date thus we can only check the channel list from
2331 `client' which is faster than checking the user list from `channel'. */
2332 /* XXX This really is utility function and should be in eg. serverutil.c */
2334 int silc_server_client_on_channel(SilcClientEntry client,
2335 SilcChannelEntry channel)
2339 if (!client || !channel)
2342 for (i = 0; i < client->channel_count; i++) {
2343 if (client->channel[i] == channel)
2350 /* Timeout callback. This is called if connection is idle or for some
2351 other reason is not responding within some period of time. This
2352 disconnects the remote end. */
2354 SILC_TASK_CALLBACK(silc_server_timeout_remote)
2356 SilcServer server = (SilcServer)context;
2357 SilcSocketConnection sock = server->sockets[fd];
2359 silc_server_disconnect_remote(server, sock,
2360 "Server closed connection: "
2361 "Connection timeout");
2364 /* Internal routine used to send (relay, route) private messages to some
2365 destination. If the private message key does not exist then the message
2366 is re-encrypted, otherwise we just pass it along. */
2369 silc_server_private_message_send_internal(SilcServer server,
2370 SilcSocketConnection dst_sock,
2373 SilcPacketContext *packet)
2375 SilcBuffer buffer = packet->buffer;
2377 /* Send and re-encrypt if private messge key does not exist */
2378 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
2380 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2381 + packet->dst_id_len + packet->padlen);
2382 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2383 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2385 /* Re-encrypt packet */
2386 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
2388 /* Send the packet */
2389 silc_server_packet_send_real(server, dst_sock, FALSE);
2392 /* Key exist so just send it */
2393 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2394 + packet->dst_id_len + packet->padlen);
2395 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2396 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2397 silc_server_packet_send_real(server, dst_sock, FALSE);
2401 /* Received private message. This resolves the destination of the message
2402 and sends the packet. This is used by both server and router. If the
2403 destination is our locally connected client this sends the packet to
2404 the client. This may also send the message for further routing if
2405 the destination is not in our server (or router). */
2407 void silc_server_private_message(SilcServer server,
2408 SilcSocketConnection sock,
2409 SilcPacketContext *packet)
2411 SilcBuffer buffer = packet->buffer;
2413 SilcServerEntry router;
2414 SilcSocketConnection dst_sock;
2415 SilcClientEntry client;
2417 SILC_LOG_DEBUG(("Start"));
2419 if (!packet->dst_id) {
2420 SILC_LOG_ERROR(("Bad Client ID in private message packet, dropped"));
2424 /* Decode destination Client ID */
2425 id = silc_id_str2id(packet->dst_id, SILC_ID_CLIENT);
2427 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
2431 /* If the destination belongs to our server we don't have to route
2432 the message anywhere but to send it to the local destination. */
2433 client = silc_idlist_find_client_by_id(server->local_list, id);
2435 /* It exists, now deliver the message to the destination */
2436 dst_sock = (SilcSocketConnection)client->connection;
2438 /* If we are router and the client has router then the client is in
2439 our cell but not directly connected to us. */
2440 if (server->server_type == SILC_ROUTER && client->router) {
2441 silc_server_private_message_send_internal(server, dst_sock,
2442 client->router->send_key,
2443 client->router->hmac,
2448 /* Seems that client really is directly connected to us */
2449 silc_server_private_message_send_internal(server, dst_sock,
2451 client->hmac, packet);
2455 /* Destination belongs to someone not in this server. If we are normal
2456 server our action is to send the packet to our router. */
2457 if (server->server_type == SILC_SERVER && !server->standalone) {
2458 router = server->id_entry->router;
2460 /* Send to primary route */
2462 dst_sock = (SilcSocketConnection)router->connection;
2463 silc_server_private_message_send_internal(server, dst_sock,
2465 router->hmac, packet);
2470 /* We are router and we will perform route lookup for the destination
2471 and send the message to fastest route. */
2472 if (server->server_type == SILC_ROUTER && !server->standalone) {
2473 dst_sock = silc_server_get_route(server, id, SILC_ID_CLIENT);
2474 router = (SilcServerEntry)dst_sock->user_data;
2476 /* Get fastest route and send packet. */
2478 silc_server_private_message_send_internal(server, dst_sock,
2480 router->hmac, packet);
2486 silc_server_send_error(server, sock,
2487 "No such nickname: Private message not sent");
2489 silc_buffer_free(buffer);
2492 /* Process received channel message. */
2494 void silc_server_channel_message(SilcServer server,
2495 SilcSocketConnection sock,
2496 SilcPacketContext *packet)
2498 SilcChannelEntry channel = NULL;
2499 SilcClientEntry client = NULL;
2500 SilcChannelID *id = NULL;
2501 SilcClientID *sender = NULL;
2502 SilcBuffer buffer = packet->buffer;
2505 SILC_LOG_DEBUG(("Processing channel message"));
2508 if (packet->dst_id_type != SILC_ID_CHANNEL) {
2509 SILC_LOG_ERROR(("Received bad message for channel, dropped"));
2510 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
2514 /* Find channel entry */
2515 id = silc_id_str2id(packet->dst_id, SILC_ID_CHANNEL);
2516 channel = silc_idlist_find_channel_by_id(server->local_list, id);
2518 SILC_LOG_DEBUG(("Could not find channel"));
2522 /* See that this client is on the channel */
2523 sender = silc_id_str2id(packet->src_id, packet->src_id_type);
2524 for (i = 0; i < channel->user_list_count; i++) {
2525 client = channel->user_list[i].client;
2526 if (client && !SILC_ID_CLIENT_COMPARE(client->id, sender))
2529 if (i >= channel->user_list_count)
2532 /* Distribute the packet to our local clients. This will send the
2533 packet for further routing as well, if needed. */
2534 silc_server_packet_relay_to_channel(server, sock, channel, sender,
2535 packet->src_id_type,
2536 packet->buffer->data,
2537 packet->buffer->len, FALSE);
2544 silc_buffer_free(buffer);
2547 /* Received channel key packet. We distribute the key to all of our locally
2548 connected clients on the channel. */
2549 /* XXX Router must accept this packet and distribute the key to all its
2550 server that has clients on the channel */
2552 void silc_server_channel_key(SilcServer server,
2553 SilcSocketConnection sock,
2554 SilcPacketContext *packet)
2556 SilcBuffer buffer = packet->buffer;
2557 SilcChannelKeyPayload payload = NULL;
2558 SilcChannelID *id = NULL;
2559 SilcChannelEntry channel;
2560 SilcClientEntry client;
2562 unsigned int key_len;
2566 if (packet->src_id_type != SILC_ID_SERVER &&
2567 sock->type != SILC_SOCKET_TYPE_ROUTER)
2570 /* Decode channel key payload */
2571 payload = silc_channel_key_parse_payload(buffer);
2573 SILC_LOG_ERROR(("Bad channel key payload, dropped"));
2577 /* Get channel ID */
2578 id = silc_id_str2id(silc_channel_key_get_id(payload, NULL), SILC_ID_CHANNEL);
2582 /* Get the channel entry */
2583 channel = silc_idlist_find_channel_by_id(server->local_list, id);
2585 SILC_LOG_ERROR(("Received key for non-existent channel"));
2589 /* Save the key for us as well */
2590 key = silc_channel_key_get_key(payload, &key_len);
2593 cipher = silc_channel_key_get_cipher(payload, NULL);;
2596 channel->key_len = key_len * 8;
2597 channel->key = silc_calloc(key_len, sizeof(unsigned char));
2598 memcpy(channel->key, key, key_len);
2599 silc_cipher_alloc(cipher, &channel->channel_key);
2600 channel->channel_key->cipher->set_key(channel->channel_key->context,
2603 /* Distribute the key to all clients on the channel */
2604 for (i = 0; i < channel->user_list_count; i++) {
2605 client = channel->user_list[i].client;
2608 silc_server_packet_send_dest(server, client->connection,
2609 SILC_PACKET_CHANNEL_KEY, 0,
2610 client->id, SILC_ID_CLIENT,
2611 buffer->data, buffer->len, FALSE);
2618 silc_channel_key_free_payload(payload);
2619 silc_buffer_free(buffer);
2622 /* Sends error message. Error messages may or may not have any
2625 void silc_server_send_error(SilcServer server,
2626 SilcSocketConnection sock,
2627 const char *fmt, ...)
2630 unsigned char buf[4096];
2632 memset(buf, 0, sizeof(buf));
2634 vsprintf(buf, fmt, ap);
2637 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
2638 buf, strlen(buf), FALSE);
2641 /* Sends notify message */
2643 void silc_server_send_notify(SilcServer server,
2644 SilcSocketConnection sock,
2645 const char *fmt, ...)
2648 unsigned char buf[4096];
2650 memset(buf, 0, sizeof(buf));
2652 vsprintf(buf, fmt, ap);
2655 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0,
2656 buf, strlen(buf), FALSE);
2659 /* Sends notify message destined to specific entity. */
2661 void silc_server_send_notify_dest(SilcServer server,
2662 SilcSocketConnection sock,
2664 SilcIdType dest_id_type,
2665 const char *fmt, ...)
2668 unsigned char buf[4096];
2670 memset(buf, 0, sizeof(buf));
2672 vsprintf(buf, fmt, ap);
2675 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
2676 dest_id, dest_id_type,
2677 buf, strlen(buf), FALSE);
2680 /* Sends notify message to a channel. The notify message sent is
2681 distributed to all clients on the channel. Actually this is not real
2682 notify message, instead it is message to channel sent by server. But
2683 as server is sending it it will appear as notify type message on the
2686 void silc_server_send_notify_to_channel(SilcServer server,
2687 SilcChannelEntry channel,
2688 const char *fmt, ...)
2691 unsigned char buf[4096];
2693 memset(buf, 0, sizeof(buf));
2695 vsprintf(buf, fmt, ap);
2698 silc_server_packet_send_to_channel(server, channel, buf,
2699 strlen(buf), FALSE);
2702 /* Sends New ID Payload to remote end. The packet is used to distribute
2703 information about new registered clients, servers, channel etc. usually
2704 to routers so that they can keep these information up to date.
2705 If the argument `broadcast' is TRUE then the packet is sent as
2706 broadcast packet. */
2708 void silc_server_send_new_id(SilcServer server,
2709 SilcSocketConnection sock,
2711 void *id, SilcIdType id_type,
2712 unsigned int id_len)
2715 unsigned char *id_string;
2717 id_string = silc_id_id2str(id, id_type);
2721 packet = silc_buffer_alloc(2 + 2 + id_len);
2722 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2723 silc_buffer_format(packet,
2724 SILC_STR_UI_SHORT(id_type),
2725 SILC_STR_UI_SHORT(id_len),
2726 SILC_STR_UI_XNSTRING(id_string, id_len),
2729 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
2730 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
2731 packet->data, packet->len, FALSE);
2732 silc_free(id_string);
2733 silc_buffer_free(packet);
2736 /* Sends Replace ID payload to remote end. This is used to replace old
2737 ID with new ID sent in the packet. This is called for example when
2738 user changes nickname and we create new ID for the user. If the
2739 argument `broadcast' is TRUE then the packet is sent as
2740 broadcast packet. */
2741 /* XXX It would be expected that the new id is same type as the old
2744 void silc_server_send_replace_id(SilcServer server,
2745 SilcSocketConnection sock,
2747 void *old_id, SilcIdType old_id_type,
2748 unsigned int old_id_len,
2749 void *new_id, SilcIdType new_id_type,
2750 unsigned int new_id_len)
2756 oid = silc_id_id2str(old_id, old_id_type);
2760 nid = silc_id_id2str(new_id, new_id_type);
2764 packet = silc_buffer_alloc(2 + 2 + 2 + 2 + old_id_len + new_id_len);
2765 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2766 silc_buffer_format(packet,
2767 SILC_STR_UI_SHORT(old_id_type),
2768 SILC_STR_UI_SHORT(old_id_len),
2769 SILC_STR_UI_XNSTRING(oid, old_id_len),
2770 SILC_STR_UI_SHORT(new_id_type),
2771 SILC_STR_UI_SHORT(new_id_len),
2772 SILC_STR_UI_XNSTRING(nid, new_id_len),
2775 silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID,
2776 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
2777 packet->data, packet->len, FALSE);
2780 silc_buffer_free(packet);
2783 /* Creates new channel. */
2785 SilcChannelEntry silc_server_new_channel(SilcServer server,
2786 SilcServerID *router_id,
2787 char *cipher, char *channel_name)
2789 int i, channel_len, key_len;
2790 SilcChannelID *channel_id;
2791 SilcChannelEntry entry;
2793 unsigned char channel_key[32], *id_string;
2796 SILC_LOG_DEBUG(("Creating new channel"));
2798 /* Create channel key */
2799 for (i = 0; i < 32; i++)
2800 channel_key[i] = silc_rng_get_byte(server->rng);
2807 silc_cipher_alloc(cipher, &key);
2808 key->cipher->set_key(key->context, channel_key, key_len);
2810 /* Create the channel */
2811 silc_id_create_channel_id(router_id, server->rng, &channel_id);
2812 entry = silc_idlist_add_channel(server->local_list, channel_name,
2813 SILC_CHANNEL_MODE_NONE, channel_id,
2819 silc_idcache_add(server->local_list->channels, channel_name,
2820 SILC_ID_CHANNEL, channel_id, (void *)entry, TRUE);
2822 entry->key = silc_calloc(key_len, sizeof(*entry->key));
2823 entry->key_len = key_len * 8;
2824 memcpy(entry->key, channel_key, key_len);
2825 memset(channel_key, 0, sizeof(channel_key));
2827 /* Notify other routers about the new channel. We send the packet
2828 to our primary route. */
2829 if (server->standalone == FALSE) {
2830 channel_len = strlen(channel_name);
2831 id_string = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
2832 packet = silc_buffer_alloc(2 + SILC_ID_CHANNEL_LEN);
2834 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2835 silc_buffer_format(packet,
2836 SILC_STR_UI_SHORT(channel_len),
2837 SILC_STR_UI_XNSTRING(channel_name, channel_len),
2838 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
2839 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CHANNEL_LEN),
2842 /* Send the packet to our router. */
2843 silc_server_packet_send(server, (SilcSocketConnection)
2844 server->id_entry->router->connection,
2845 SILC_PACKET_NEW_CHANNEL_USER, 0,
2846 packet->data, packet->len, TRUE);
2848 silc_free(id_string);
2849 silc_buffer_free(packet);
2855 /* Create new client. This processes incoming NEW_CLIENT packet and creates
2856 Client ID for the client. Client becomes registered after calling this
2859 SilcClientEntry silc_server_new_client(SilcServer server,
2860 SilcSocketConnection sock,
2861 SilcPacketContext *packet)
2863 SilcBuffer buffer = packet->buffer;
2864 SilcClientEntry client;
2865 SilcIDCacheEntry cache;
2866 SilcClientID *client_id;
2868 char *username = NULL, *realname = NULL, *id_string;
2870 SILC_LOG_DEBUG(("Creating new client"));
2872 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
2875 /* Take client entry */
2876 client = (SilcClientEntry)sock->user_data;
2878 /* Fetch the old client cache entry so that we can update it. */
2879 if (!silc_idcache_find_by_context(server->local_list->clients,
2880 sock->user_data, &cache)) {
2881 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
2885 /* Parse incoming packet */
2886 silc_buffer_unformat(buffer,
2887 SILC_STR_UI16_STRING_ALLOC(&username),
2888 SILC_STR_UI16_STRING_ALLOC(&realname),
2891 /* Create Client ID */
2892 silc_id_create_client_id(server->id, server->rng, server->md5hash,
2893 username, &client_id);
2895 /* Update client entry */
2896 client->registered = TRUE;
2897 client->nickname = strdup(username);
2898 client->username = username;
2899 client->userinfo = realname;
2900 client->id = client_id;
2902 /* Update the cache entry */
2903 cache->id = (void *)client_id;
2904 cache->type = SILC_ID_CLIENT;
2905 cache->data = username;
2906 silc_idcache_sort_by_data(server->local_list->clients);
2908 /* Notify our router about new client on the SILC network */
2909 if (!server->standalone)
2910 silc_server_send_new_id(server, (SilcSocketConnection)
2911 server->id_entry->router->connection,
2912 server->server_type == SILC_SERVER ? TRUE : FALSE,
2913 client->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
2915 /* Send the new client ID to the client. */
2916 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
2917 reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
2918 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
2919 silc_buffer_format(reply,
2920 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
2921 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
2922 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
2924 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
2925 reply->data, reply->len, FALSE);
2926 silc_free(id_string);
2927 silc_buffer_free(reply);
2929 /* Send some nice info to the client */
2930 silc_server_send_notify(server, sock,
2931 "Welcome to the SILC Network %s@%s",
2933 sock->hostname ? sock->hostname : sock->ip);
2934 silc_server_send_notify(server, sock,
2935 "Your host is %s, running version %s",
2936 server->config->server_info->server_name,
2938 silc_server_send_notify(server, sock,
2939 "Your connection is secured with %s cipher, "
2940 "key length %d bits",
2941 client->send_key->cipher->name,
2942 client->send_key->cipher->key_len);
2943 silc_server_send_notify(server, sock,
2944 "Your current nickname is %s",
2952 /* Create new server. This processes incoming NEW_SERVER packet and
2953 saves the received Server ID. The server is our locally connected
2954 server thus we save all the information and save it to local list.
2955 This funtion can be used by both normal server and router server.
2956 If normal server uses this it means that its router has connected
2957 to the server. If router uses this it means that one of the cell's
2958 servers is connected to the router. */
2960 SilcServerEntry silc_server_new_server(SilcServer server,
2961 SilcSocketConnection sock,
2962 SilcPacketContext *packet)
2964 SilcBuffer buffer = packet->buffer;
2965 SilcServerEntry new_server;
2966 SilcIDCacheEntry cache;
2967 SilcServerID *server_id;
2968 unsigned char *server_name, *id_string;
2970 SILC_LOG_DEBUG(("Creating new server"));
2972 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2973 sock->type != SILC_SOCKET_TYPE_ROUTER)
2976 /* Take server entry */
2977 new_server = (SilcServerEntry)sock->user_data;
2979 /* Fetch the old server cache entry so that we can update it. */
2980 if (!silc_idcache_find_by_context(server->local_list->servers,
2981 sock->user_data, &cache)) {
2982 SILC_LOG_ERROR(("Lost server's cache entry - bad thing"));
2986 /* Parse the incoming packet */
2987 silc_buffer_unformat(buffer,
2988 SILC_STR_UI16_STRING_ALLOC(&id_string),
2989 SILC_STR_UI16_STRING_ALLOC(&server_name),
2993 server_id = silc_id_str2id(id_string, SILC_ID_SERVER);
2994 silc_free(id_string);
2996 /* Update client entry */
2997 new_server->registered = TRUE;
2998 new_server->server_name = server_name;
2999 new_server->id = server_id;
3001 /* Update the cache entry */
3002 cache->id = (void *)server_id;
3003 cache->type = SILC_ID_SERVER;
3004 cache->data = server_name;
3005 silc_idcache_sort_by_data(server->local_list->servers);
3007 /* Distribute the information about new server in the SILC network
3008 to our router. If we are normal server we won't send anything
3009 since this connection must be our router connection. */
3010 if (server->server_type == SILC_ROUTER && !server->standalone)
3011 silc_server_send_new_id(server, server->id_entry->router->connection,
3012 TRUE, new_server->id, SILC_ID_SERVER,
3013 SILC_ID_SERVER_LEN);
3018 /* Processes incoming New ID Payload. New ID Payload is used to distribute
3019 information about newly registered clients, servers and created
3022 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
3023 SilcPacketContext *packet)
3025 SilcBuffer buffer = packet->buffer;
3027 unsigned char *id_string;
3030 SILC_LOG_DEBUG(("Processing new ID"));
3032 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3033 server->server_type == SILC_SERVER)
3036 silc_buffer_unformat(buffer,
3037 SILC_STR_UI_SHORT(&id_type),
3038 SILC_STR_UI16_STRING_ALLOC(&id_string),
3041 /* Normal server cannot have other normal server connections */
3042 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
3045 id = silc_id_str2id(id_string, id_type);
3049 /* XXX Do check whether the packet is coming outside the cell or
3050 from someone inside the cell. If outside use global lists otherwise
3052 /* XXX If using local list set the idlist->connection to the sender's
3053 socket connection as it is used in packet sending */
3056 case SILC_ID_CLIENT:
3058 SilcClientEntry idlist;
3060 /* Add the client to our local list. We are router and we keep
3061 cell specific local database of all clients in the cell. */
3062 idlist = silc_idlist_add_client(server->local_list, NULL, NULL, NULL,
3063 id, sock->user_data, NULL, NULL,
3064 NULL, NULL, NULL, sock);
3068 case SILC_ID_SERVER:
3070 SilcServerEntry idlist;
3072 /* Add the server to our local list. We are router and we keep
3073 cell specific local database of all servers in the cell. */
3074 idlist = silc_idlist_add_server(server->local_list, NULL, 0,
3075 id, server->id_entry, NULL, NULL,
3076 NULL, NULL, NULL, sock);
3080 case SILC_ID_CHANNEL:
3081 /* Add the channel to our local list. We are router and we keep
3082 cell specific local database of all channels in the cell. */
3083 silc_idlist_add_channel(server->local_list, NULL, 0, id,
3084 server->id_entry, NULL);
3093 silc_free(id_string);