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.13 2000/08/21 14:21:21 priikone
29 * Fixed channel joining and channel message sending inside a
30 * SILC cell. Added silc_server_send_remove_channel_user and
31 * silc_server_remove_channel_user functions.
33 * Revision 1.12 2000/07/26 07:05:11 priikone
34 * Fixed the server to server (server to router actually) connections
35 * and made the private message work inside a cell. Added functin
36 * silc_server_replace_id.
38 * Revision 1.11 2000/07/20 10:17:25 priikone
39 * Added dynamic protocol registering/unregistering support. The
40 * patch was provided by cras.
42 * Revision 1.10 2000/07/17 11:47:30 priikone
43 * Added command lagging support. Added idle counting support.
45 * Revision 1.9 2000/07/14 06:15:47 priikone
46 * Moved all the generic packet sending, encryption, reception,
47 * decryption and processing functions to library as they were
48 * duplicated code. Now server uses the generic routine which is
49 * a lot cleaner. Channel message sending uses now also generic
50 * routines instead of duplicating the packet sending for every
51 * channel message sending function. Same was done for private
52 * message sending as well.
54 * Revision 1.8 2000/07/12 05:59:41 priikone
55 * Major rewrite of ID Cache system. Support added for the new
56 * ID cache system. Major rewrite of ID List stuff on server. All
57 * SilcXXXList's are now called SilcXXXEntry's and they are pointers
58 * by default. A lot rewritten ID list functions.
60 * Revision 1.7 2000/07/10 05:43:00 priikone
61 * Removed command packet processing from server.c and added it to
63 * Implemented INFO command. Added support for testing that
64 * connections are registered before executing commands.
66 * Revision 1.6 2000/07/07 06:55:59 priikone
67 * Added SILC style public key support and made server to use
70 * Revision 1.5 2000/07/06 13:18:07 priikone
71 * Check for NULL in client_on_channel.
73 * Revision 1.4 2000/07/05 06:14:01 priikone
74 * Global costemic changes.
76 * Revision 1.3 2000/07/04 08:13:53 priikone
77 * Changed message route discovery to use silc_server_get_route.
78 * Added silc_server_client_on_channel function.
80 * Revision 1.1.1.1 2000/06/27 11:36:56 priikone
81 * Imported from internal CVS/Added Log headers.
86 #include "serverincludes.h"
87 #include "server_internal.h"
89 /* Static prototypes */
90 SILC_TASK_CALLBACK(silc_server_connect_to_router);
91 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
92 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
93 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
94 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
95 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
96 SILC_TASK_CALLBACK(silc_server_packet_process);
97 SILC_TASK_CALLBACK(silc_server_packet_parse_real);
98 SILC_TASK_CALLBACK(silc_server_timeout_remote);
100 extern char server_version[];
102 /* Allocates a new SILC server object. This has to be done before the server
103 can be used. After allocation one must call silc_server_init to initialize
104 the server. The new allocated server object is returned to the new_server
107 int silc_server_alloc(SilcServer *new_server)
111 SILC_LOG_DEBUG(("Allocating new server object"));
113 server = silc_calloc(1, sizeof(*server));
114 server->server_type = SILC_SERVER;
115 server->standalone = FALSE;
116 server->local_list = silc_calloc(1, sizeof(*server->local_list));
117 server->global_list = silc_calloc(1, sizeof(*server->global_list));
119 *new_server = server;
124 /* Free's the SILC server object. This is called at the very end before
127 void silc_server_free(SilcServer server)
130 if (server->local_list)
131 silc_free(server->local_list);
132 if (server->global_list)
133 silc_free(server->global_list);
135 silc_rng_free(server->rng);
137 silc_math_primegen_uninit(); /* XXX */
142 /* Initializes the entire SILC server. This is called always before running
143 the server. This is called only once at the initialization of the program.
144 This binds the server to its listenning port. After this function returns
145 one should call silc_server_run to start the server. This returns TRUE
146 when everything is ok to run the server. Configuration file must be
147 read and parsed before calling this. */
149 int silc_server_init(SilcServer server)
151 int *sock = NULL, sock_count = 0, i;
153 SilcServerEntry id_entry;
155 SILC_LOG_DEBUG(("Initializing server"));
157 assert(server->config);
159 /* Set log files where log message should be saved. */
160 server->config->server = server;
161 silc_config_server_setlogfiles(server->config);
163 /* Register all configured ciphers, PKCS and hash functions. */
164 silc_config_server_register_ciphers(server->config);
165 silc_config_server_register_pkcs(server->config);
166 silc_config_server_register_hashfuncs(server->config);
168 /* Initialize random number generator for the server. */
169 server->rng = silc_rng_alloc();
170 silc_rng_init(server->rng);
171 silc_math_primegen_init(); /* XXX */
173 /* Initialize hash functions for server to use */
174 silc_hash_alloc("md5", &server->md5hash);
175 silc_hash_alloc("sha1", &server->sha1hash);
177 /* Initialize none cipher */
178 silc_cipher_alloc("none", &server->none_cipher);
180 /* XXXXX Generate RSA key pair */
182 unsigned char *public_key;
183 unsigned char *private_key;
184 unsigned int pk_len, prv_len;
187 if (stat("pubkey.pub", &st) < 0 && stat("privkey.prv", &st) < 0) {
189 if (silc_pkcs_alloc("rsa", &server->pkcs) == FALSE) {
190 SILC_LOG_ERROR(("Could not create RSA key pair"));
194 if (server->pkcs->pkcs->init(server->pkcs->context,
195 1024, server->rng) == FALSE) {
196 SILC_LOG_ERROR(("Could not generate RSA key pair"));
200 public_key = server->pkcs->pkcs->get_public_key(server->pkcs->context,
202 private_key = server->pkcs->pkcs->get_private_key(server->pkcs->context,
205 SILC_LOG_HEXDUMP(("public key"), public_key, pk_len);
206 SILC_LOG_HEXDUMP(("private key"), private_key, prv_len);
209 silc_pkcs_public_key_alloc("rsa", "UN=root, HN=dummy",
211 server->private_key =
212 silc_pkcs_private_key_alloc("rsa", private_key, prv_len);
215 silc_pkcs_save_public_key("pubkey.pub", server->public_key,
217 silc_pkcs_save_private_key("privkey.prv", server->private_key, NULL,
220 memset(public_key, 0, pk_len);
221 memset(private_key, 0, prv_len);
222 silc_free(public_key);
223 silc_free(private_key);
225 silc_pkcs_load_public_key("pubkey.pub", &server->public_key,
227 silc_pkcs_load_private_key("privkey.prv", &server->private_key,
232 /* Create a listening server. Note that our server can listen on
233 multiple ports. All listeners are created here and now. */
234 /* XXX Still check this whether to use server_info or listen_port. */
236 while(server->config->listen_port) {
239 tmp = silc_net_create_server(server->config->listen_port->port,
240 server->config->listen_port->host);
244 sock = silc_realloc(sock, (sizeof(int *) * (sock_count + 1)));
245 sock[sock_count] = tmp;
246 server->config->listen_port = server->config->listen_port->next;
250 /* Initialize ID caches */
251 server->local_list->clients = silc_idcache_alloc(0);
252 server->local_list->servers = silc_idcache_alloc(0);
253 server->local_list->channels = silc_idcache_alloc(0);
255 /* XXX for now these are allocated for normal server as well as these
256 hold some global information that the server has fetched from its
257 router. For router these are used as they are supposed to be used
258 on router. The XXX can be remoevd later if this is the way we are
259 going to do this in the normal server as well. */
260 server->global_list->clients = silc_idcache_alloc(0);
261 server->global_list->servers = silc_idcache_alloc(0);
262 server->global_list->channels = silc_idcache_alloc(0);
264 /* Allocate the entire socket list that is used in server. Eventually
265 all connections will have entry in this table (it is a table of
266 pointers to the actual object that is allocated individually
268 server->sockets = silc_calloc(SILC_SERVER_MAX_CONNECTIONS,
269 sizeof(*server->sockets));
271 for (i = 0; i < sock_count; i++) {
272 SilcSocketConnection newsocket = NULL;
274 /* Set socket to non-blocking mode */
275 silc_net_set_socket_nonblock(sock[i]);
276 server->sock = sock[i];
278 /* Create a Server ID for the server. */
279 silc_id_create_server_id(sock[i], server->rng, &id);
285 server->id_type = SILC_ID_SERVER;
286 server->server_name = server->config->server_info->server_name;
288 /* Add ourselves to the server list. We don't have a router yet
289 beacuse we haven't established a route yet. It will be done later.
290 For now, NULL is sent as router. This allocates new entry to
293 silc_idlist_add_server(server->local_list,
294 server->config->server_info->server_name,
295 server->server_type, server->id, NULL,
296 server->send_key, server->receive_key,
297 NULL, NULL, NULL, NULL);
299 SILC_LOG_ERROR(("Could not add ourselves to cache"));
303 /* Add ourselves also to the socket table. The entry allocated above
304 is sent as argument for fast referencing in the future. */
305 silc_socket_alloc(sock[i], SILC_SOCKET_TYPE_SERVER, id_entry,
310 server->sockets[sock[i]] = newsocket;
312 /* Put the allocated socket pointer also to the entry allocated above
313 for fast back-referencing to the socket list. */
314 id_entry->connection = (void *)server->sockets[sock[i]];
315 server->id_entry = id_entry;
318 /* Register the task queues. In SILC we have by default three task queues.
319 One task queue for non-timeout tasks which perform different kind of
320 I/O on file descriptors, timeout task queue for timeout tasks, and,
321 generic non-timeout task queue whose tasks apply to all connections. */
322 silc_task_queue_alloc(&server->io_queue, TRUE);
323 if (!server->io_queue) {
326 silc_task_queue_alloc(&server->timeout_queue, TRUE);
327 if (!server->timeout_queue) {
330 silc_task_queue_alloc(&server->generic_queue, TRUE);
331 if (!server->generic_queue) {
335 /* Register protocols */
336 silc_server_protocols_register();
338 /* Initialize the scheduler */
339 silc_schedule_init(server->io_queue, server->timeout_queue,
340 server->generic_queue,
341 SILC_SERVER_MAX_CONNECTIONS);
343 /* Add the first task to the queue. This is task that is executed by
344 timeout. It expires as soon as the caller calls silc_server_run. This
345 task performs authentication protocol and key exchange with our
347 if (silc_task_register(server->timeout_queue, sock[0],
348 silc_server_connect_to_router,
349 (void *)server, 0, 1,
351 SILC_TASK_PRI_NORMAL) == NULL) {
355 /* If server connections has been configured then we must be router as
356 normal server cannot have server connections, only router connections. */
357 if (server->config->servers)
358 server->server_type = SILC_ROUTER;
360 SILC_LOG_DEBUG(("Server initialized"));
362 /* We are done here, return succesfully */
366 silc_task_queue_free(server->timeout_queue);
368 silc_task_queue_free(server->io_queue);
370 for (i = 0; i < sock_count; i++)
371 silc_net_close_server(sock[i]);
376 /* Stops the SILC server. This function is used to shutdown the server.
377 This is usually called after the scheduler has returned. After stopping
378 the server one should call silc_server_free. */
380 void silc_server_stop(SilcServer server)
382 SILC_LOG_DEBUG(("Stopping server"));
384 /* Stop the scheduler, although it might be already stopped. This
385 doesn't hurt anyone. This removes all the tasks and task queues,
387 silc_schedule_stop();
388 silc_schedule_uninit();
390 silc_server_protocols_unregister();
392 SILC_LOG_DEBUG(("Server stopped"));
395 /* The heart of the server. This runs the scheduler thus runs the server. */
397 void silc_server_run(SilcServer server)
399 SILC_LOG_DEBUG(("Running server"));
401 /* Start the scheduler, the heart of the SILC server. When this returns
402 the program will be terminated. */
406 /* This function connects to our primary router or if we are a router this
407 establishes all our primary routes. This is called at the start of the
408 server to do authentication and key exchange with our router - called
411 SILC_TASK_CALLBACK(silc_server_connect_to_router)
413 SilcServer server = (SilcServer)context;
414 SilcSocketConnection newsocket;
417 SILC_LOG_DEBUG(("Connecting to router(s)"));
419 /* if we are normal SILC server we need to connect to our cell's
421 if (server->server_type == SILC_SERVER) {
422 SilcProtocol protocol;
423 SilcServerKEInternalContext *proto_ctx;
425 /* Create connection to the router, if configured. */
426 if (server->config->routers) {
427 sock = silc_net_create_connection(server->config->routers->port,
428 server->config->routers->host);
430 SILC_LOG_ERROR(("Could not connect to router"));
431 silc_schedule_stop();
435 /* Set socket options */
436 silc_net_set_socket_nonblock(sock);
437 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
439 /* Create socket connection for the connection. Even though we
440 know that we are connecting to a router we will mark the socket
441 to be unknown connection until we have executed authentication
443 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
444 server->sockets[sock] = newsocket;
445 newsocket->hostname = server->config->routers->host;
446 newsocket->port = server->config->routers->port;
448 /* Allocate internal protocol context. This is sent as context
450 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
451 proto_ctx->server = context;
452 proto_ctx->sock = newsocket;
453 proto_ctx->rng = server->rng;
454 proto_ctx->responder = FALSE;
456 /* Perform key exchange protocol. silc_server_connect_to_router_second
457 will be called after the protocol is finished. */
458 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
459 &protocol, proto_ctx,
460 silc_server_connect_to_router_second);
461 newsocket->protocol = protocol;
463 /* Register a timeout task that will be executed if the protocol
464 is not executed within 60 seconds. For now, this is a hard coded
465 limit. After 60 secs the connection will be closed if the key
466 exchange protocol has not been executed. */
467 proto_ctx->timeout_task =
468 silc_task_register(server->timeout_queue, sock,
469 silc_server_timeout_remote,
474 /* Register the connection for network input and output. This sets
475 that scheduler will listen for incoming packets for this connection
476 and sets that outgoing packets may be sent to this connection as
477 well. However, this doesn't set the scheduler for outgoing traffic,
478 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
479 later when outgoing data is available. */
480 SILC_REGISTER_CONNECTION_FOR_IO(sock);
482 /* Run the protocol */
483 protocol->execute(server->timeout_queue, 0, protocol, sock, 0, 0);
488 /* if we are a SILC router we need to establish all of our primary
490 if (server->server_type == SILC_ROUTER) {
491 SilcConfigServerSectionServerConnection *ptr;
493 /* Create the connections to all our routes */
494 ptr = server->config->routers;
496 SilcProtocol protocol;
497 SilcServerKEInternalContext *proto_ctx;
499 /* Create the connection to the remote end */
500 sock = silc_net_create_connection(ptr->port, ptr->host);
502 SILC_LOG_ERROR(("Could not connect to router"));
503 silc_schedule_stop();
507 /* Set socket options */
508 silc_net_set_socket_nonblock(sock);
509 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
511 /* Create socket connection for the connection. Even though we
512 know that we are connecting to a router we will mark the socket
513 to be unknown connection until we have executed authentication
515 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
516 server->sockets[sock] = newsocket;
517 newsocket->hostname = ptr->host;
518 newsocket->port = ptr->port;
520 /* Allocate internal protocol context. This is sent as context
522 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
523 proto_ctx->server = context;
524 proto_ctx->sock = newsocket;
525 proto_ctx->rng = server->rng;
526 proto_ctx->responder = FALSE;
528 /* Perform key exchange protocol. silc_server_connect_to_router_final
529 will be called after the protocol is finished. */
530 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
531 &protocol, proto_ctx,
532 silc_server_connect_to_router_second);
533 newsocket->protocol = protocol;
535 /* Register a timeout task that will be executed if the protocol
536 is not executed within 60 seconds. For now, this is a hard coded
537 limit. After 60 secs the connection will be closed if the key
538 exchange protocol has not been executed. */
539 proto_ctx->timeout_task =
540 silc_task_register(server->timeout_queue, sock,
541 silc_server_timeout_remote,
546 /* Register the connection for network input and output. This sets
547 that scheduler will listen for incoming packets for this connection
548 and sets that outgoing packets may be sent to this connection as
549 well. However, this doesn't set the scheduler for outgoing traffic,
550 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
551 later when outgoing data is available. */
552 SILC_REGISTER_CONNECTION_FOR_IO(sock);
554 /* Run the protocol */
555 protocol->execute(server->timeout_queue, 0, protocol, sock, 0, 0);
564 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
566 /* There wasn't a configured router, we will continue but we don't
567 have a connection to outside world. We will be standalone server. */
568 server->standalone = TRUE;
570 /* Add a task to the queue. This task receives new connections to the
571 server. This task remains on the queue until the end of the program. */
572 if (silc_task_register(server->io_queue, fd,
573 silc_server_accept_new_connection,
574 (void *)server, 0, 0,
576 SILC_TASK_PRI_NORMAL) == NULL) {
577 silc_schedule_stop();
582 /* Second part of connecting to router(s). Key exchange protocol has been
583 executed and now we will execute authentication protocol. */
585 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
587 SilcProtocol protocol = (SilcProtocol)context;
588 SilcServerKEInternalContext *ctx =
589 (SilcServerKEInternalContext *)protocol->context;
590 SilcServer server = (SilcServer)ctx->server;
591 SilcSocketConnection sock = NULL;
592 SilcServerConnAuthInternalContext *proto_ctx;
594 SILC_LOG_DEBUG(("Start"));
596 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
597 /* Error occured during protocol */
598 silc_protocol_free(protocol);
600 silc_buffer_free(ctx->packet);
602 silc_ske_free(ctx->ske);
604 silc_free(ctx->dest_id);
606 sock->protocol = NULL;
607 silc_server_disconnect_remote(server, sock, "Server closed connection: "
608 "Key exchange failed");
612 /* Allocate internal context for the authentication protocol. This
613 is sent as context for the protocol. */
614 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
615 proto_ctx->server = (void *)server;
616 proto_ctx->sock = sock = server->sockets[fd];
617 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
618 proto_ctx->dest_id_type = ctx->dest_id_type;
619 proto_ctx->dest_id = ctx->dest_id;
621 /* Resolve the authentication method used in this connection */
622 proto_ctx->auth_meth = SILC_PROTOCOL_CONN_AUTH_PASSWORD;
623 if (server->config->routers) {
624 SilcConfigServerSectionServerConnection *conn = NULL;
626 /* Check if we find a match from user configured connections */
627 conn = silc_config_server_find_router_conn(server->config,
631 /* Match found. Use the configured authentication method */
632 proto_ctx->auth_meth = conn->auth_meth;
633 if (conn->auth_data) {
634 proto_ctx->auth_data = strdup(conn->auth_data);
635 proto_ctx->auth_data_len = strlen(conn->auth_data);
638 /* No match found. */
645 /* Free old protocol as it is finished now */
646 silc_protocol_free(protocol);
648 silc_buffer_free(ctx->packet);
650 sock->protocol = NULL;
652 /* Allocate the authentication protocol. This is allocated here
653 but we won't start it yet. We will be receiving party of this
654 protocol thus we will wait that connecting party will make
656 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
657 &sock->protocol, proto_ctx,
658 silc_server_connect_to_router_final);
660 /* Register timeout task. If the protocol is not executed inside
661 this timelimit the connection will be terminated. Currently
662 this is 15 seconds and is hard coded limit (XXX). */
663 proto_ctx->timeout_task =
664 silc_task_register(server->timeout_queue, sock->sock,
665 silc_server_timeout_remote,
666 (void *)server, 15, 0,
670 /* Run the protocol */
671 sock->protocol->execute(server->timeout_queue, 0,
672 sock->protocol, sock->sock, 0, 0);
675 /* Finalizes the connection to router. Registers a server task to the
676 queue so that we can accept new connections. */
678 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
680 SilcProtocol protocol = (SilcProtocol)context;
681 SilcServerConnAuthInternalContext *ctx =
682 (SilcServerConnAuthInternalContext *)protocol->context;
683 SilcServer server = (SilcServer)ctx->server;
684 SilcSocketConnection sock = ctx->sock;
685 SilcServerEntry id_entry;
686 SilcUnknownEntry conn_data;
688 unsigned char *id_string;
690 SILC_LOG_DEBUG(("Start"));
692 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
693 /* Error occured during protocol */
694 silc_protocol_free(protocol);
696 silc_buffer_free(ctx->packet);
698 silc_ske_free(ctx->ske);
700 silc_free(ctx->dest_id);
702 sock->protocol = NULL;
703 silc_server_disconnect_remote(server, sock, "Server closed connection: "
704 "Authentication failed");
708 /* Add a task to the queue. This task receives new connections to the
709 server. This task remains on the queue until the end of the program. */
710 if (!server->listenning) {
711 if (silc_task_register(server->io_queue, server->sock,
712 silc_server_accept_new_connection,
713 (void *)server, 0, 0,
715 SILC_TASK_PRI_NORMAL) == NULL) {
716 silc_schedule_stop();
719 server->listenning = TRUE;
723 /* Send NEW_SERVER packet to the router. We will become registered
724 to the SILC network after sending this packet. */
725 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
726 packet = silc_buffer_alloc(2 + 2 + SILC_ID_SERVER_LEN +
727 strlen(server->server_name));
728 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
729 silc_buffer_format(packet,
730 SILC_STR_UI_SHORT(SILC_ID_SERVER_LEN),
731 SILC_STR_UI_XNSTRING(id_string, SILC_ID_SERVER_LEN),
732 SILC_STR_UI_SHORT(strlen(server->server_name)),
733 SILC_STR_UI_XNSTRING(server->server_name,
734 strlen(server->server_name)),
737 /* Send the packet */
738 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
739 packet->data, packet->len, TRUE);
740 silc_buffer_free(packet);
741 silc_free(id_string);
743 SILC_LOG_DEBUG(("Connected to router %s", sock->hostname));
745 /* Add the connected router to local server list */
746 server->standalone = FALSE;
747 conn_data = (SilcUnknownEntry)sock->user_data;
749 silc_idlist_add_server(server->local_list,
750 sock->hostname ? sock->hostname : sock->ip,
751 SILC_ROUTER, ctx->dest_id, NULL,
752 conn_data->send_key, conn_data->receive_key,
753 conn_data->pkcs, conn_data->hmac, NULL, sock);
755 id_entry->hmac_key = conn_data->hmac_key;
756 id_entry->hmac_key_len = conn_data->hmac_key_len;
757 sock->user_data = (void *)id_entry;
758 sock->type = SILC_SOCKET_TYPE_ROUTER;
759 server->id_entry->router = id_entry;
762 /* Free the temporary connection data context from key exchange */
763 silc_free(conn_data);
765 /* Free the protocol object */
766 silc_protocol_free(protocol);
768 silc_buffer_free(ctx->packet);
770 silc_ske_free(ctx->ske);
772 sock->protocol = NULL;
775 /* Accepts new connections to the server. Accepting new connections are
776 done in three parts to make it async. */
778 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
780 SilcServer server = (SilcServer)context;
781 SilcSocketConnection newsocket;
782 SilcServerKEInternalContext *proto_ctx;
785 SILC_LOG_DEBUG(("Accepting new connection"));
787 sock = silc_net_accept_connection(server->sock);
789 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
793 /* Check max connections */
794 if (sock > SILC_SERVER_MAX_CONNECTIONS) {
795 if (server->config->redirect) {
796 /* XXX Redirecting connection to somewhere else now?? */
797 /*silc_server_send_notify("Server is full, trying to redirect..."); */
799 SILC_LOG_ERROR(("Refusing connection, server is full"));
804 /* Set socket options */
805 silc_net_set_socket_nonblock(sock);
806 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
808 /* We don't create a ID yet, since we don't know what type of connection
809 this is yet. But, we do add the connection to the socket table. */
810 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
811 server->sockets[sock] = newsocket;
813 /* XXX This MUST be done async as this will block the entire server
814 process. Either we have to do our own resolver stuff or in the future
815 we can use threads. */
816 /* Perform mandatory name and address lookups for the remote host. */
817 silc_net_check_host_by_sock(sock, &newsocket->hostname, &newsocket->ip);
818 if (!newsocket->ip || !newsocket->hostname) {
819 SILC_LOG_DEBUG(("IP lookup/DNS lookup failed"));
820 SILC_LOG_ERROR(("IP lookup/DNS lookup failed"));
824 SILC_LOG_INFO(("Incoming connection from %s (%s)", newsocket->hostname,
827 /* Allocate internal context for key exchange protocol. This is
828 sent as context for the protocol. */
829 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
830 proto_ctx->server = context;
831 proto_ctx->sock = newsocket;
832 proto_ctx->rng = server->rng;
833 proto_ctx->responder = TRUE;
835 /* Prepare the connection for key exchange protocol. We allocate the
836 protocol but will not start it yet. The connector will be the
837 initiator of the protocol thus we will wait for initiation from
838 there before we start the protocol. */
839 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
840 &newsocket->protocol, proto_ctx,
841 silc_server_accept_new_connection_second);
843 /* Register a timeout task that will be executed if the connector
844 will not start the key exchange protocol within 60 seconds. For
845 now, this is a hard coded limit. After 60 secs the connection will
846 be closed if the key exchange protocol has not been started. */
847 proto_ctx->timeout_task =
848 silc_task_register(server->timeout_queue, newsocket->sock,
849 silc_server_timeout_remote,
854 /* Register the connection for network input and output. This sets
855 that scheduler will listen for incoming packets for this connection
856 and sets that outgoing packets may be sent to this connection as well.
857 However, this doesn't set the scheduler for outgoing traffic, it
858 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
859 later when outgoing data is available. */
860 SILC_REGISTER_CONNECTION_FOR_IO(sock);
863 /* Second part of accepting new connection. Key exchange protocol has been
864 performed and now it is time to do little connection authentication
865 protocol to figure out whether this connection is client or server
866 and whether it has right to access this server (especially server
867 connections needs to be authenticated). */
869 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
871 SilcProtocol protocol = (SilcProtocol)context;
872 SilcServerKEInternalContext *ctx =
873 (SilcServerKEInternalContext *)protocol->context;
874 SilcServer server = (SilcServer)ctx->server;
875 SilcSocketConnection sock = NULL;
876 SilcServerConnAuthInternalContext *proto_ctx;
878 SILC_LOG_DEBUG(("Start"));
880 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
881 /* Error occured during protocol */
882 silc_protocol_free(protocol);
884 silc_buffer_free(ctx->packet);
886 silc_ske_free(ctx->ske);
888 silc_free(ctx->dest_id);
891 sock->protocol = NULL;
892 silc_server_disconnect_remote(server, sock, "Server closed connection: "
893 "Key exchange failed");
897 /* Allocate internal context for the authentication protocol. This
898 is sent as context for the protocol. */
899 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
900 proto_ctx->server = (void *)server;
901 proto_ctx->sock = sock = server->sockets[fd];
902 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
903 proto_ctx->responder = TRUE;
904 proto_ctx->dest_id_type = ctx->dest_id_type;
905 proto_ctx->dest_id = ctx->dest_id;
907 /* Free old protocol as it is finished now */
908 silc_protocol_free(protocol);
910 silc_buffer_free(ctx->packet);
912 sock->protocol = NULL;
914 /* Allocate the authentication protocol. This is allocated here
915 but we won't start it yet. We will be receiving party of this
916 protocol thus we will wait that connecting party will make
918 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
919 &sock->protocol, proto_ctx,
920 silc_server_accept_new_connection_final);
922 /* Register timeout task. If the protocol is not executed inside
923 this timelimit the connection will be terminated. Currently
924 this is 60 seconds and is hard coded limit (XXX). */
925 proto_ctx->timeout_task =
926 silc_task_register(server->timeout_queue, sock->sock,
927 silc_server_timeout_remote,
928 (void *)server, 60, 0,
933 /* Final part of accepting new connection. The connection has now
934 been authenticated and keys has been exchanged. We also know whether
935 this is client or server connection. */
937 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
939 SilcProtocol protocol = (SilcProtocol)context;
940 SilcServerConnAuthInternalContext *ctx =
941 (SilcServerConnAuthInternalContext *)protocol->context;
942 SilcServer server = (SilcServer)ctx->server;
943 SilcSocketConnection sock = ctx->sock;
945 SILC_LOG_DEBUG(("Start"));
947 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
948 /* Error occured during protocol */
949 silc_protocol_free(protocol);
951 silc_buffer_free(ctx->packet);
953 silc_ske_free(ctx->ske);
955 silc_free(ctx->dest_id);
958 sock->protocol = NULL;
959 silc_server_disconnect_remote(server, sock, "Server closed connection: "
960 "Authentication failed");
964 sock->type = ctx->conn_type;
966 case SILC_SOCKET_TYPE_CLIENT:
968 SilcClientEntry client;
969 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
971 SILC_LOG_DEBUG(("Remote host is client"));
972 SILC_LOG_INFO(("Connection from %s (%s) is client", sock->hostname,
975 /* Add the client to the client ID cache. The nickname and Client ID
976 and other information is created after we have received NEW_CLIENT
977 packet from client. */
979 silc_idlist_add_client(server->local_list, NULL, NULL, NULL, NULL,
980 NULL, conn_data->send_key,
981 conn_data->receive_key, conn_data->pkcs,
982 conn_data->hmac, NULL, sock);
984 SILC_LOG_ERROR(("Could not add new client to cache"));
985 silc_free(conn_data);
989 /* Free the temporary connection data context from key exchange */
990 silc_free(conn_data);
992 /* Add to sockets internal pointer for fast referencing */
993 sock->user_data = (void *)client;
996 case SILC_SOCKET_TYPE_SERVER:
997 case SILC_SOCKET_TYPE_ROUTER:
999 SilcServerEntry new_server;
1000 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
1002 SILC_LOG_DEBUG(("Remote host is %s",
1003 sock->type == SILC_SOCKET_TYPE_SERVER ?
1004 "server" : "router"));
1005 SILC_LOG_INFO(("Connection from %s (%s) is %s", sock->hostname,
1006 sock->ip, sock->type == SILC_SOCKET_TYPE_SERVER ?
1007 "server" : "router"));
1009 /* Add the server into server cache. The server name and Server ID
1010 is updated after we have received NEW_SERVER packet from the
1013 silc_idlist_add_server(server->local_list, NULL,
1014 sock->type == SILC_SOCKET_TYPE_SERVER ?
1015 SILC_SERVER : SILC_ROUTER, NULL, NULL,
1016 conn_data->send_key, conn_data->receive_key,
1017 conn_data->pkcs, conn_data->hmac, NULL, sock);
1019 SILC_LOG_ERROR(("Could not add new server to cache"));
1020 silc_free(conn_data);
1024 new_server->registered = TRUE;
1025 new_server->hmac_key = conn_data->hmac_key;
1026 new_server->hmac_key_len = conn_data->hmac_key_len;
1028 /* Free the temporary connection data context from protocols */
1029 silc_free(conn_data);
1031 /* Add to sockets internal pointer for fast referencing */
1032 sock->user_data = (void *)new_server;
1034 /* There is connection to other server now, if it is router then
1035 we will have connection to outside world. If we are router but
1036 normal server connected to us then we will remain standalone,
1037 if we are standlone. */
1038 if (server->standalone && sock->type == SILC_SOCKET_TYPE_ROUTER) {
1039 SILC_LOG_DEBUG(("We are not standalone server anymore"));
1040 server->standalone = FALSE;
1048 /* Connection has been fully established now. Everything is ok. */
1049 SILC_LOG_DEBUG(("New connection authenticated"));
1051 silc_protocol_free(protocol);
1053 silc_buffer_free(ctx->packet);
1055 silc_ske_free(ctx->ske);
1057 silc_free(ctx->dest_id);
1059 sock->protocol = NULL;
1062 /* Internal routine that sends packet or marks packet to be sent. This
1063 is used directly only in special cases. Normal cases should use
1064 silc_server_packet_send. Returns < 0 error. */
1066 static int silc_server_packet_send_real(SilcServer server,
1067 SilcSocketConnection sock,
1072 /* Send the packet */
1073 ret = silc_packet_send(sock, force_send);
1077 /* Mark that there is some outgoing data available for this connection.
1078 This call sets the connection both for input and output (the input
1079 is set always and this call keeps the input setting, actually).
1080 Actual data sending is performed by silc_server_packet_process. */
1081 SILC_SET_CONNECTION_FOR_OUTPUT(sock->sock);
1083 /* Mark to socket that data is pending in outgoing buffer. This flag
1084 is needed if new data is added to the buffer before the earlier
1085 put data is sent to the network. */
1086 SILC_SET_OUTBUF_PENDING(sock);
1092 SilcPacketContext *packetdata;
1094 SilcSocketConnection sock;
1097 } SilcServerInternalPacket;
1099 /* This function is used to read packets from network and send packets to
1100 network. This is usually a generic task. */
1102 SILC_TASK_CALLBACK(silc_server_packet_process)
1104 SilcServer server = (SilcServer)context;
1105 SilcSocketConnection sock = server->sockets[fd];
1106 SilcCipher cipher = NULL;
1107 SilcHmac hmac = NULL;
1110 SILC_LOG_DEBUG(("Processing packet"));
1112 /* Packet sending */
1113 if (type == SILC_TASK_WRITE) {
1114 SILC_LOG_DEBUG(("Writing data to connection"));
1116 if (sock->outbuf->data - sock->outbuf->head)
1117 silc_buffer_push(sock->outbuf,
1118 sock->outbuf->data - sock->outbuf->head);
1120 ret = silc_server_packet_send_real(server, sock, TRUE);
1122 /* If returned -2 could not write to connection now, will do
1127 /* The packet has been sent and now it is time to set the connection
1128 back to only for input. When there is again some outgoing data
1129 available for this connection it will be set for output as well.
1130 This call clears the output setting and sets it only for input. */
1131 SILC_SET_CONNECTION_FOR_INPUT(fd);
1132 SILC_UNSET_OUTBUF_PENDING(sock);
1134 silc_buffer_clear(sock->outbuf);
1138 /* Packet receiving */
1139 if (type == SILC_TASK_READ) {
1140 SILC_LOG_DEBUG(("Reading data from connection"));
1142 /* Read some data from connection */
1143 ret = silc_packet_receive(sock);
1149 SILC_LOG_DEBUG(("Read EOF"));
1151 /* If connection is disconnecting already we will finally
1152 close the connection */
1153 if (SILC_IS_DISCONNECTING(sock)) {
1154 if (sock->user_data)
1155 silc_server_free_sock_user_data(server, sock);
1156 silc_server_close_connection(server, sock);
1160 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1162 if (sock->user_data)
1163 silc_server_free_sock_user_data(server, sock);
1164 silc_server_close_connection(server, sock);
1168 /* If connection is disconnecting or disconnected we will ignore
1170 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1171 SILC_LOG_DEBUG(("Ignoring read data from invalid connection"));
1175 switch (sock->type) {
1176 case SILC_SOCKET_TYPE_CLIENT:
1178 SilcClientEntry clnt = (SilcClientEntry)sock->user_data;
1182 clnt->last_receive = time(NULL);
1184 cipher = clnt->receive_key;
1188 case SILC_SOCKET_TYPE_SERVER:
1189 case SILC_SOCKET_TYPE_ROUTER:
1191 SilcServerEntry srvr = (SilcServerEntry)sock->user_data;
1195 srvr->last_receive = time(NULL);
1197 cipher = srvr->receive_key;
1201 case SILC_SOCKET_TYPE_UNKNOWN:
1203 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
1207 cipher = conn_data->receive_key;
1208 hmac = conn_data->hmac;
1215 /* Process the packet. This will call the parser that will then
1216 decrypt and parse the packet. */
1217 if (!silc_packet_receive_process(sock, cipher, hmac,
1218 silc_server_packet_parse, server)) {
1219 silc_buffer_clear(sock->inbuf);
1225 /* Parses whole packet, received earlier. */
1227 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1229 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1230 SilcServer server = (SilcServer)parse_ctx->context;
1231 SilcSocketConnection sock = parse_ctx->sock;
1232 SilcPacketContext *packet = parse_ctx->packet;
1233 SilcBuffer buffer = packet->buffer;
1236 SILC_LOG_DEBUG(("Start"));
1238 /* Decrypt the received packet */
1239 ret = silc_packet_decrypt(parse_ctx->cipher, parse_ctx->hmac,
1245 /* Parse the packet. Packet type is returned. */
1246 ret = silc_packet_parse(packet);
1248 /* Parse the packet header in special way as this is "special"
1250 ret = silc_packet_parse_special(packet);
1253 if (ret == SILC_PACKET_NONE)
1256 /* Parse the incoming packet type */
1257 silc_server_packet_parse_type(server, sock, packet);
1260 silc_buffer_clear(sock->inbuf);
1262 silc_free(parse_ctx);
1265 /* Parser callback called by silc_packet_receive_process. This merely
1266 registers timeout that will handle the actual parsing whem appropriate. */
1268 void silc_server_packet_parse(SilcPacketParserContext *parser_context)
1270 SilcServer server = (SilcServer)parser_context->context;
1271 SilcSocketConnection sock = parser_context->sock;
1273 switch (sock->type) {
1274 case SILC_SOCKET_TYPE_CLIENT:
1275 case SILC_SOCKET_TYPE_UNKNOWN:
1276 /* Parse the packet with timeout */
1277 silc_task_register(server->timeout_queue, sock->sock,
1278 silc_server_packet_parse_real,
1279 (void *)parser_context, 0, 100000,
1281 SILC_TASK_PRI_NORMAL);
1283 case SILC_SOCKET_TYPE_SERVER:
1284 case SILC_SOCKET_TYPE_ROUTER:
1285 /* Packets from servers are parsed as soon as possible */
1286 silc_task_register(server->timeout_queue, sock->sock,
1287 silc_server_packet_parse_real,
1288 (void *)parser_context, 0, 1,
1290 SILC_TASK_PRI_NORMAL);
1297 /* Parses the packet type and calls what ever routines the packet type
1298 requires. This is done for all incoming packets. */
1300 void silc_server_packet_parse_type(SilcServer server,
1301 SilcSocketConnection sock,
1302 SilcPacketContext *packet)
1304 SilcBuffer buffer = packet->buffer;
1305 SilcPacketType type = packet->type;
1307 SILC_LOG_DEBUG(("Parsing packet type %d", type));
1309 /* Parse the packet type */
1311 case SILC_PACKET_DISCONNECT:
1312 SILC_LOG_DEBUG(("Disconnect packet"));
1314 case SILC_PACKET_SUCCESS:
1316 * Success received for something. For now we can have only
1317 * one protocol for connection executing at once hence this
1318 * success message is for whatever protocol is executing currently.
1320 SILC_LOG_DEBUG(("Success packet"));
1321 if (sock->protocol) {
1322 sock->protocol->execute(server->timeout_queue, 0,
1323 sock->protocol, sock->sock, 0, 0);
1326 case SILC_PACKET_FAILURE:
1327 SILC_LOG_DEBUG(("Failure packet"));
1329 case SILC_PACKET_REJECT:
1330 SILC_LOG_DEBUG(("Reject packet"));
1337 case SILC_PACKET_CHANNEL_MESSAGE:
1339 * Received channel message. Channel messages are special packets
1340 * (although probably most common ones) hence they are handled
1343 SILC_LOG_DEBUG(("Channel Message packet"));
1344 silc_server_channel_message(server, sock, packet);
1347 case SILC_PACKET_CHANNEL_KEY:
1349 * Received key for channel. As channels are created by the router
1350 * the keys are as well. We will distribute the key to all of our
1351 * locally connected clients on the particular channel. Router
1352 * never receives this channel and thus is ignored.
1354 SILC_LOG_DEBUG(("Channel Key packet"));
1355 silc_server_channel_key(server, sock, packet);
1361 case SILC_PACKET_COMMAND:
1363 * Recived command. Allocate command context and execute the command.
1365 SILC_LOG_DEBUG(("Command packet"));
1366 silc_server_command_process(server, sock, packet);
1369 case SILC_PACKET_COMMAND_REPLY:
1371 * Received command reply packet. Servers never send commands thus
1372 * they don't receive command reply packets either, except in cases
1373 * where server has forwarded command packet coming from client.
1374 * This must be the case here or we will ignore the packet.
1376 SILC_LOG_DEBUG(("Command Reply packet"));
1377 silc_server_packet_relay_command_reply(server, sock, packet);
1381 * Private Message packets
1383 case SILC_PACKET_PRIVATE_MESSAGE:
1385 * Received private message packet. The packet is coming from either
1388 SILC_LOG_DEBUG(("Private Message packet"));
1389 silc_server_private_message(server, sock, packet);
1392 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1396 * Key Exchange protocol packets
1398 case SILC_PACKET_KEY_EXCHANGE:
1399 SILC_LOG_DEBUG(("KE packet"));
1400 if (sock->protocol && sock->protocol->protocol->type
1401 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1403 SilcServerKEInternalContext *proto_ctx =
1404 (SilcServerKEInternalContext *)sock->protocol->context;
1406 proto_ctx->packet = buffer;
1408 /* Let the protocol handle the packet */
1409 sock->protocol->execute(server->timeout_queue, 0,
1410 sock->protocol, sock->sock, 0, 100000);
1412 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1413 "protocol active, packet dropped."));
1415 /* XXX Trigger KE protocol?? Rekey actually, maybe. */
1419 case SILC_PACKET_KEY_EXCHANGE_1:
1420 SILC_LOG_DEBUG(("KE 1 packet"));
1421 if (sock->protocol && sock->protocol->protocol->type
1422 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1424 SilcServerKEInternalContext *proto_ctx =
1425 (SilcServerKEInternalContext *)sock->protocol->context;
1427 if (proto_ctx->packet)
1428 silc_buffer_free(proto_ctx->packet);
1430 proto_ctx->packet = buffer;
1431 proto_ctx->dest_id_type = packet->src_id_type;
1432 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
1434 /* Let the protocol handle the packet */
1435 sock->protocol->execute(server->timeout_queue, 0,
1436 sock->protocol, sock->sock,
1439 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1440 "protocol active, packet dropped."));
1444 case SILC_PACKET_KEY_EXCHANGE_2:
1445 SILC_LOG_DEBUG(("KE 2 packet"));
1446 if (sock->protocol && sock->protocol->protocol->type
1447 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1449 SilcServerKEInternalContext *proto_ctx =
1450 (SilcServerKEInternalContext *)sock->protocol->context;
1452 if (proto_ctx->packet)
1453 silc_buffer_free(proto_ctx->packet);
1455 proto_ctx->packet = buffer;
1456 proto_ctx->dest_id_type = packet->src_id_type;
1457 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
1459 /* Let the protocol handle the packet */
1460 sock->protocol->execute(server->timeout_queue, 0,
1461 sock->protocol, sock->sock,
1464 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1465 "protocol active, packet dropped."));
1469 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1470 /* If we receive this packet we will send to the other end information
1471 about our mandatory authentication method for the connection.
1472 This packet maybe received at any time. */
1475 * Connection Authentication protocol packets
1477 case SILC_PACKET_CONNECTION_AUTH:
1478 /* Start of the authentication protocol. We receive here the
1479 authentication data and will verify it. */
1480 SILC_LOG_DEBUG(("Connection auth packet"));
1481 if (sock->protocol && sock->protocol->protocol->type
1482 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
1484 SilcServerConnAuthInternalContext *proto_ctx =
1485 (SilcServerConnAuthInternalContext *)sock->protocol->context;
1487 proto_ctx->packet = buffer;
1489 /* Let the protocol handle the packet */
1490 sock->protocol->execute(server->timeout_queue, 0,
1491 sock->protocol, sock->sock, 0, 0);
1493 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
1494 "protocol active, packet dropped."));
1498 case SILC_PACKET_NEW_ID:
1500 * Received New ID packet. This includes some new ID that has been
1501 * created. It may be for client, server or channel. This is the way
1502 * to distribute information about new registered entities in the
1505 SILC_LOG_DEBUG(("New ID packet"));
1506 silc_server_new_id(server, sock, packet);
1509 case SILC_PACKET_NEW_CLIENT:
1511 * Received new client packet. This includes client information that
1512 * we will use to create initial client ID. After creating new
1513 * ID we will send it to the client.
1515 SILC_LOG_DEBUG(("New Client packet"));
1516 silc_server_new_client(server, sock, packet);
1519 case SILC_PACKET_NEW_SERVER:
1521 * Received new server packet. This includes Server ID and some other
1522 * information that we may save. This is received after server has
1525 SILC_LOG_DEBUG(("New Server packet"));
1526 silc_server_new_server(server, sock, packet);
1529 case SILC_PACKET_NEW_CHANNEL:
1532 case SILC_PACKET_NEW_CHANNEL_USER:
1535 case SILC_PACKET_NEW_CHANNEL_LIST:
1538 case SILC_PACKET_NEW_CHANNEL_USER_LIST:
1541 case SILC_PACKET_REPLACE_ID:
1543 * Received replace ID packet. This sends the old ID that is to be
1544 * replaced with the new one included into the packet. Client must not
1547 SILC_LOG_DEBUG(("Replace ID packet"));
1548 silc_server_replace_id(server, sock, packet);
1551 case SILC_PACKET_REMOVE_ID:
1554 case SILC_PACKET_REMOVE_CHANNEL_USER:
1556 * Received packet to remove user from a channel. Routers notify other
1557 * routers about a user leaving a channel.
1559 SILC_LOG_DEBUG(("Remove Channel User packet"));
1560 silc_server_remove_channel_user(server, sock, packet);
1564 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
1570 /* Assembles a new packet to be sent out to network. This doesn't actually
1571 send the packet but creates the packet and fills the outgoing data
1572 buffer and marks the packet ready to be sent to network. However, If
1573 argument force_send is TRUE the packet is sent immediately and not put
1574 to queue. Normal case is that the packet is not sent immediately. */
1576 void silc_server_packet_send(SilcServer server,
1577 SilcSocketConnection sock,
1578 SilcPacketType type,
1579 SilcPacketFlags flags,
1580 unsigned char *data,
1581 unsigned int data_len,
1584 void *dst_id = NULL;
1585 SilcIdType dst_id_type = SILC_ID_NONE;
1590 /* Get data used in the packet sending, keys and stuff */
1591 switch(sock->type) {
1592 case SILC_SOCKET_TYPE_CLIENT:
1593 if (((SilcClientEntry)sock->user_data)->id) {
1594 dst_id = ((SilcClientEntry)sock->user_data)->id;
1595 dst_id_type = SILC_ID_CLIENT;
1598 case SILC_SOCKET_TYPE_SERVER:
1599 case SILC_SOCKET_TYPE_ROUTER:
1600 if (((SilcServerEntry)sock->user_data)->id) {
1601 dst_id = ((SilcServerEntry)sock->user_data)->id;
1602 dst_id_type = SILC_ID_SERVER;
1609 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
1610 dst_id_type, data, data_len, force_send);
1613 /* Assembles a new packet to be sent out to network. This doesn't actually
1614 send the packet but creates the packet and fills the outgoing data
1615 buffer and marks the packet ready to be sent to network. However, If
1616 argument force_send is TRUE the packet is sent immediately and not put
1617 to queue. Normal case is that the packet is not sent immediately.
1618 Destination information is sent as argument for this function. */
1620 void silc_server_packet_send_dest(SilcServer server,
1621 SilcSocketConnection sock,
1622 SilcPacketType type,
1623 SilcPacketFlags flags,
1625 SilcIdType dst_id_type,
1626 unsigned char *data,
1627 unsigned int data_len,
1630 SilcPacketContext packetdata;
1631 SilcCipher cipher = NULL;
1632 SilcHmac hmac = NULL;
1633 unsigned char *dst_id_data = NULL;
1634 unsigned int dst_id_len = 0;
1636 SILC_LOG_DEBUG(("Sending packet, type %d", type));
1638 /* Get data used in the packet sending, keys and stuff */
1639 switch(sock->type) {
1640 case SILC_SOCKET_TYPE_CLIENT:
1641 if (sock->user_data) {
1642 cipher = ((SilcClientEntry)sock->user_data)->send_key;
1643 hmac = ((SilcClientEntry)sock->user_data)->hmac;
1646 case SILC_SOCKET_TYPE_SERVER:
1647 case SILC_SOCKET_TYPE_ROUTER:
1648 if (sock->user_data) {
1649 cipher = ((SilcServerEntry)sock->user_data)->send_key;
1650 hmac = ((SilcServerEntry)sock->user_data)->hmac;
1654 if (sock->user_data) {
1655 /* We don't know what type of connection this is thus it must
1656 be in authentication phase. */
1657 cipher = ((SilcUnknownEntry)sock->user_data)->send_key;
1658 hmac = ((SilcUnknownEntry)sock->user_data)->hmac;
1664 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
1665 dst_id_len = silc_id_get_len(dst_id_type);
1668 /* Set the packet context pointers */
1669 packetdata.type = type;
1670 packetdata.flags = flags;
1671 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
1672 packetdata.src_id_len = SILC_ID_SERVER_LEN;
1673 packetdata.src_id_type = server->id_type;
1674 packetdata.dst_id = dst_id_data;
1675 packetdata.dst_id_len = dst_id_len;
1676 packetdata.dst_id_type = dst_id_type;
1677 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1678 packetdata.src_id_len + dst_id_len;
1679 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1680 packetdata.rng = server->rng;
1682 /* Prepare outgoing data buffer for packet sending */
1683 silc_packet_send_prepare(sock,
1684 SILC_PACKET_HEADER_LEN +
1685 packetdata.src_id_len +
1686 packetdata.dst_id_len,
1690 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
1692 packetdata.buffer = sock->outbuf;
1694 /* Put the data to the buffer */
1695 if (data && data_len)
1696 silc_buffer_put(sock->outbuf, data, data_len);
1698 /* Create the outgoing packet */
1699 silc_packet_assemble(&packetdata);
1701 /* Encrypt the packet */
1703 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
1705 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
1706 sock->outbuf->data, sock->outbuf->len);
1708 /* Now actually send the packet */
1709 silc_server_packet_send_real(server, sock, force_send);
1711 if (packetdata.src_id)
1712 silc_free(packetdata.src_id);
1713 if (packetdata.dst_id)
1714 silc_free(packetdata.dst_id);
1717 /* Forwards packet. Packets sent with this function will be marked as
1718 forwarded (in the SILC header flags) so that the receiver knows that
1719 we have forwarded the packet to it. Forwarded packets are handled
1720 specially by the receiver as they are not destined to the receiver
1721 originally. However, the receiver knows this because the forwarded
1722 flag has been set (and the flag is authenticated). */
1724 void silc_server_packet_forward(SilcServer server,
1725 SilcSocketConnection sock,
1726 unsigned char *data, unsigned int data_len,
1729 SilcCipher cipher = NULL;
1730 SilcHmac hmac = NULL;
1732 SILC_LOG_DEBUG(("Forwarding packet"));
1734 /* Get data used in the packet sending, keys and stuff */
1735 switch(sock->type) {
1736 case SILC_SOCKET_TYPE_CLIENT:
1737 if (sock->user_data) {
1738 cipher = ((SilcClientEntry )sock->user_data)->send_key;
1739 hmac = ((SilcClientEntry )sock->user_data)->hmac;
1742 case SILC_SOCKET_TYPE_SERVER:
1743 case SILC_SOCKET_TYPE_ROUTER:
1744 if (sock->user_data) {
1745 cipher = ((SilcServerEntry )sock->user_data)->send_key;
1746 hmac = ((SilcServerEntry )sock->user_data)->hmac;
1750 /* We won't forward to unknown destination - keys must exist with
1751 the destination before forwarding. */
1755 /* Prepare outgoing data buffer for packet sending */
1756 silc_packet_send_prepare(sock, 0, 0, data_len);
1758 /* Mungle the packet flags and add the FORWARDED flag */
1760 data[2] |= (unsigned char)SILC_PACKET_FLAG_FORWARDED;
1762 /* Put the data to the buffer */
1763 if (data && data_len)
1764 silc_buffer_put(sock->outbuf, data, data_len);
1766 /* Encrypt the packet */
1768 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
1770 SILC_LOG_HEXDUMP(("Forwarded packet, len %d", sock->outbuf->len),
1771 sock->outbuf->data, sock->outbuf->len);
1773 /* Now actually send the packet */
1774 silc_server_packet_send_real(server, sock, force_send);
1777 /* Internal routine to actually create the channel packet and send it
1778 to network. This is common function in channel message sending. */
1781 silc_server_packet_send_to_channel_real(SilcServer server,
1782 SilcSocketConnection sock,
1783 SilcPacketContext *packet,
1786 unsigned char *data,
1787 unsigned int data_len,
1790 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
1791 packet->src_id_len + packet->dst_id_len;
1793 /* Prepare outgoing data buffer for packet sending */
1794 silc_packet_send_prepare(sock,
1795 SILC_PACKET_HEADER_LEN +
1796 packet->src_id_len +
1801 packet->buffer = sock->outbuf;
1803 /* Put the data to buffer, assemble and encrypt the packet. The packet
1804 is encrypted with normal session key shared with the client. */
1805 silc_buffer_put(sock->outbuf, data, data_len);
1806 silc_packet_assemble(packet);
1807 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
1808 packet->src_id_len + packet->dst_id_len +
1811 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
1812 sock->outbuf->data, sock->outbuf->len);
1814 /* Now actually send the packet */
1815 silc_server_packet_send_real(server, sock, force_send);
1818 /* This routine is used by the server to send packets to channel. The
1819 packet sent with this function is distributed to all clients on
1820 the channel. Usually this is used to send notify messages to the
1821 channel, things like notify about new user joining to the channel. */
1823 void silc_server_packet_send_to_channel(SilcServer server,
1824 SilcChannelEntry channel,
1825 unsigned char *data,
1826 unsigned int data_len,
1830 SilcSocketConnection sock = NULL;
1831 SilcPacketContext packetdata;
1832 SilcClientEntry client = NULL;
1833 SilcServerEntry *routed = NULL;
1834 unsigned int routed_count = 0;
1839 SILC_LOG_DEBUG(("Sending packet to channel"));
1842 for (i = 0; i < 16; i++)
1843 channel->iv[i] = silc_rng_get_byte(server->rng);
1845 /* Encode the channel payload */
1846 payload = silc_channel_encode_payload(0, "", data_len, data,
1847 16, channel->iv, server->rng);
1849 SILC_LOG_ERROR(("Could not encode channel payload, message not sent"));
1853 /* Encrypt payload of the packet. This is encrypted with the
1855 channel->channel_key->cipher->encrypt(channel->channel_key->context,
1856 payload->data, payload->data,
1857 payload->len - 16, /* -IV_LEN */
1860 /* Set the packet context pointers. */
1861 packetdata.flags = 0;
1862 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
1863 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1864 packetdata.src_id_len = SILC_ID_SERVER_LEN;
1865 packetdata.src_id_type = SILC_ID_SERVER;
1866 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1867 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
1868 packetdata.dst_id_type = SILC_ID_CHANNEL;
1869 packetdata.rng = server->rng;
1870 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
1871 packetdata.src_id_len +
1872 packetdata.dst_id_len));
1874 /* If there are global users in the channel we will send the message
1875 first to our router for further routing. */
1876 if (server->server_type == SILC_SERVER && !server->standalone &&
1877 channel->global_users) {
1878 SilcServerEntry router;
1880 /* Get data used in packet header encryption, keys and stuff. */
1881 router = server->id_entry->router;
1882 sock = (SilcSocketConnection)router->connection;
1883 cipher = router->send_key;
1884 hmac = router->hmac;
1886 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
1888 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1889 cipher, hmac, payload->data,
1890 payload->len, force_send);
1893 /* Send the message to clients on the channel's client list. */
1894 for (i = 0; i < channel->user_list_count; i++) {
1895 client = channel->user_list[i].client;
1897 /* If client has router set it is not locally connected client and
1898 we will route the message to the router set in the client. */
1899 if (client && client->router && server->server_type == SILC_ROUTER) {
1902 /* Check if we have sent the packet to this route already */
1903 for (k = 0; k < routed_count; k++)
1904 if (routed[k] == client->router)
1907 /* Get data used in packet header encryption, keys and stuff. */
1908 sock = (SilcSocketConnection)client->router->connection;
1909 cipher = client->router->send_key;
1910 hmac = client->router->hmac;
1912 /* Send the packet */
1913 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1914 cipher, hmac, payload->data,
1915 payload->len, force_send);
1917 /* We want to make sure that the packet is routed to same router
1918 only once. Mark this route as sent route. */
1920 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1921 routed[k] = client->router;
1927 /* Send to locally connected client */
1930 /* XXX Check client's mode on the channel. */
1932 /* Get data used in packet header encryption, keys and stuff. */
1933 sock = (SilcSocketConnection)client->connection;
1934 cipher = client->send_key;
1935 hmac = client->hmac;
1937 /* Send the packet */
1938 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1939 cipher, hmac, payload->data,
1940 payload->len, force_send);
1946 silc_free(packetdata.src_id);
1947 silc_free(packetdata.dst_id);
1948 silc_buffer_free(payload);
1951 /* This routine is explicitly used to relay messages to some channel.
1952 Packets sent with this function we have received earlier and are
1953 totally encrypted. This just sends the packet to all clients on
1954 the channel. If the sender of the packet is someone on the channel
1955 the message will not be sent to that client. The SILC Packet header
1956 is encrypted with the session key shared between us and the client.
1957 MAC is also computed before encrypting the header. Rest of the
1958 packet will be untouched. */
1960 void silc_server_packet_relay_to_channel(SilcServer server,
1961 SilcSocketConnection sender_sock,
1962 SilcChannelEntry channel,
1964 SilcIdType sender_type,
1965 unsigned char *data,
1966 unsigned int data_len,
1969 int i, found = FALSE;
1970 SilcSocketConnection sock = NULL;
1971 SilcPacketContext packetdata;
1972 SilcClientEntry client = NULL;
1973 SilcServerEntry *routed = NULL;
1974 unsigned int routed_count = 0;
1978 SILC_LOG_DEBUG(("Relaying packet to channel"));
1980 SILC_LOG_HEXDUMP(("XXX %d", data_len), data, data_len);
1982 /* Set the packet context pointers. */
1983 packetdata.flags = 0;
1984 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
1985 packetdata.src_id = silc_id_id2str(sender, sender_type);
1986 packetdata.src_id_len = silc_id_get_len(sender_type);
1987 packetdata.src_id_type = sender_type;
1988 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1989 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
1990 packetdata.dst_id_type = SILC_ID_CHANNEL;
1991 packetdata.rng = server->rng;
1992 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
1993 packetdata.src_id_len +
1994 packetdata.dst_id_len));
1996 /* If there are global users in the channel we will send the message
1997 first to our router for further routing. */
1998 if (server->server_type == SILC_SERVER && !server->standalone &&
1999 channel->global_users) {
2000 SilcServerEntry router;
2002 router = server->id_entry->router;
2004 /* Check that the sender is not our router. */
2005 if (sender_sock != (SilcSocketConnection)router->connection) {
2007 /* Get data used in packet header encryption, keys and stuff. */
2008 sock = (SilcSocketConnection)router->connection;
2009 cipher = router->send_key;
2010 hmac = router->hmac;
2012 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
2014 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
2016 data_len, force_send);
2020 /* Send the message to clients on the channel's client list. */
2021 for (i = 0; i < channel->user_list_count; i++) {
2022 client = channel->user_list[i].client;
2026 /* If sender is one on the channel do not send it the packet. */
2027 if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
2032 /* If the client has set router it means that it is not locally
2033 connected client and we will route the packet further. */
2034 if (server->server_type == SILC_ROUTER && client->router) {
2037 /* Sender maybe server as well so we want to make sure that
2038 we won't send the message to the server it came from. */
2039 if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
2044 /* Check if we have sent the packet to this route already */
2045 for (k = 0; k < routed_count; k++)
2046 if (routed[k] == client->router)
2049 /* Get data used in packet header encryption, keys and stuff. */
2050 sock = (SilcSocketConnection)client->router->connection;
2051 cipher = client->router->send_key;
2052 hmac = client->router->hmac;
2054 /* Send the packet */
2055 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
2057 data_len, force_send);
2059 /* We want to make sure that the packet is routed to same router
2060 only once. Mark this route as sent route. */
2062 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
2063 routed[k] = client->router;
2069 /* XXX Check client's mode on the channel. */
2071 /* Get data used in packet header encryption, keys and stuff. */
2072 sock = (SilcSocketConnection)client->connection;
2073 cipher = client->send_key;
2074 hmac = client->hmac;
2076 SILC_LOG_DEBUG(("Sending packet to client %s",
2077 sock->hostname ? sock->hostname : sock->ip));
2079 /* Send the packet */
2080 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
2082 data_len, force_send);
2086 silc_free(packetdata.src_id);
2087 silc_free(packetdata.dst_id);
2090 /* This function is used to send packets strictly to all local clients
2091 on a particular channel. This is used for example to distribute new
2092 channel key to all our locally connected clients on the channel.
2093 The packets are always encrypted with the session key shared between
2096 void silc_server_packet_send_local_channel(SilcServer server,
2097 SilcChannelEntry channel,
2098 SilcPacketType type,
2099 SilcPacketFlags flags,
2100 unsigned char *data,
2101 unsigned int data_len,
2105 SilcClientEntry client;
2106 SilcSocketConnection sock = NULL;
2108 SILC_LOG_DEBUG(("Start"));
2110 /* Send the message to clients on the channel's client list. */
2111 for (i = 0; i < channel->user_list_count; i++) {
2112 client = channel->user_list[i].client;
2115 sock = (SilcSocketConnection)client->connection;
2117 /* Send the packet to the client */
2118 silc_server_packet_send_dest(server, sock, type, flags, client->id,
2119 SILC_ID_CLIENT, data, data_len,
2125 /* Relays received command reply packet to the correct destination. The
2126 destination must be one of our locally connected client or the packet
2127 will be ignored. This is called when server has forwarded one of
2128 client's command request to router and router has now replied to the
2131 void silc_server_packet_relay_command_reply(SilcServer server,
2132 SilcSocketConnection sock,
2133 SilcPacketContext *packet)
2135 SilcBuffer buffer = packet->buffer;
2136 SilcClientEntry client;
2138 SilcSocketConnection dst_sock;
2140 SILC_LOG_DEBUG(("Start"));
2142 /* Source must be server or router */
2143 if (packet->src_id_type != SILC_ID_SERVER &&
2144 sock->type != SILC_SOCKET_TYPE_ROUTER)
2147 /* Destination must be client */
2148 if (packet->dst_id_type != SILC_ID_CLIENT)
2151 /* Execute command reply locally for the command */
2152 silc_server_command_reply_process(server, sock, buffer);
2154 id = silc_id_str2id(packet->dst_id, SILC_ID_CLIENT);
2156 /* Destination must be one of ours */
2157 client = silc_idlist_find_client_by_id(server->local_list, id);
2163 /* Relay the packet to the client */
2165 dst_sock = (SilcSocketConnection)client->connection;
2166 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2167 + packet->dst_id_len + packet->padlen);
2169 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2170 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2172 /* Encrypt packet */
2173 if (client && client->send_key)
2174 silc_packet_encrypt(client->send_key, client->hmac,
2175 dst_sock->outbuf, buffer->len);
2177 /* Send the packet */
2178 silc_server_packet_send_real(server, dst_sock, FALSE);
2183 silc_buffer_free(buffer);
2186 /* Closes connection to socket connection */
2188 void silc_server_close_connection(SilcServer server,
2189 SilcSocketConnection sock)
2192 SILC_LOG_DEBUG(("Closing connection %d", sock->sock));
2194 /* We won't listen for this connection anymore */
2195 silc_schedule_unset_listen_fd(sock->sock);
2197 /* Unregister all tasks */
2198 silc_task_unregister_by_fd(server->io_queue, sock->sock);
2199 silc_task_unregister_by_fd(server->timeout_queue, sock->sock);
2201 /* Close the actual connection */
2202 silc_net_close_connection(sock->sock);
2203 server->sockets[sock->sock] = NULL;
2204 silc_socket_free(sock);
2207 /* Sends disconnect message to remote connection and disconnects the
2210 void silc_server_disconnect_remote(SilcServer server,
2211 SilcSocketConnection sock,
2212 const char *fmt, ...)
2215 unsigned char buf[4096];
2217 memset(buf, 0, sizeof(buf));
2219 vsprintf(buf, fmt, ap);
2222 SILC_LOG_DEBUG(("Disconnecting remote host"));
2224 /* Notify remote end that the conversation is over. The notify message
2225 is tried to be sent immediately. */
2226 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2227 buf, strlen(buf), TRUE);
2229 /* Mark the connection to be disconnected */
2230 SILC_SET_DISCONNECTED(sock);
2231 silc_server_close_connection(server, sock);
2234 /* Free's user_data pointer from socket connection object. As this
2235 pointer maybe anything we wil switch here to find the correct
2236 data type and free it the way it needs to be free'd. */
2238 void silc_server_free_sock_user_data(SilcServer server,
2239 SilcSocketConnection sock)
2241 SILC_LOG_DEBUG(("Start"));
2243 switch(sock->type) {
2244 case SILC_SOCKET_TYPE_CLIENT:
2246 SilcClientEntry user_data = (SilcClientEntry )sock->user_data;
2248 /* Remove client from all channels */
2249 silc_server_remove_from_channels(server, sock, user_data);
2251 /* Free the client entry and everything in it */
2252 /* XXX must take some info to history before freeing */
2253 silc_idlist_del_client(server->local_list, user_data);
2256 case SILC_SOCKET_TYPE_SERVER:
2257 case SILC_SOCKET_TYPE_ROUTER:
2265 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2267 if (user_data->send_key)
2268 silc_cipher_free(user_data->send_key);
2269 if (user_data->receive_key)
2270 silc_cipher_free(user_data->receive_key);
2271 if (user_data->pkcs)
2272 silc_pkcs_free(user_data->pkcs);
2273 if (user_data->hmac) {
2274 silc_hmac_free(user_data->hmac);
2275 memset(user_data->hmac_key, 0, user_data->hmac_key_len);
2276 silc_free(user_data->hmac_key);
2278 silc_free(user_data);
2283 sock->user_data = NULL;
2288 /* Removes client from all channels it has joined. This is used when
2289 client connection is disconnected. If the client on a channel
2290 is last, the channel is removed as well. */
2292 void silc_server_remove_from_channels(SilcServer server,
2293 SilcSocketConnection sock,
2294 SilcClientEntry client)
2297 SilcChannelEntry channel;
2299 /* Remove the client from all channels. The client is removed from
2300 the channels' user list. */
2301 for (i = 0; i < client->channel_count; i++) {
2302 channel = client->channel[i];
2306 /* Remove from channel */
2307 for (k = 0; k < channel->user_list_count; k++) {
2308 if (channel->user_list[k].client == client) {
2310 /* If this client is last one on the channel the channel
2311 is removed all together. */
2312 if (channel->user_list_count == 1) {
2314 /* However, if the channel has marked global users then the
2315 channel is not created locally, and this does not remove the
2316 channel globally from SILC network, in this case we will
2317 notify that this client has left the channel. */
2318 if (channel->global_users)
2319 silc_server_send_notify_to_channel(server, channel,
2323 sock->hostname : sock->ip);
2325 silc_idlist_del_channel(server->local_list, channel);
2329 channel->user_list[k].client = NULL;
2330 channel->user_list[k].mode = SILC_CHANNEL_UMODE_NONE;
2332 /* Send notify to channel about client leaving SILC and thus
2333 the entire channel. */
2334 silc_server_send_notify_to_channel(server, channel,
2338 sock->hostname : sock->ip);
2343 if (client->channel_count)
2344 silc_free(client->channel);
2345 client->channel = NULL;
2348 /* Removes client from one channel. This is used for example when client
2349 calls LEAVE command to remove itself from the channel. Returns TRUE
2350 if channel still exists and FALSE if the channel is removed when
2351 last client leaves the channel. If `notify' is FALSE notify messages
2354 int silc_server_remove_from_one_channel(SilcServer server,
2355 SilcSocketConnection sock,
2356 SilcChannelEntry channel,
2357 SilcClientEntry client,
2361 SilcChannelEntry ch;
2363 /* Remove the client from the channel. The client is removed from
2364 the channel's user list. */
2365 for (i = 0; i < client->channel_count; i++) {
2366 ch = client->channel[i];
2367 if (!ch || ch != channel)
2371 client->channel[i] = NULL;
2373 /* Remove from channel */
2374 for (k = 0; k < channel->user_list_count; k++) {
2375 if (channel->user_list[k].client == client) {
2377 /* If this client is last one on the channel the channel
2378 is removed all together. */
2379 if (channel->user_list_count == 1) {
2380 /* Notify about leaving client if this channel has global users,
2381 ie. the channel is not created locally. */
2382 if (notify && channel->global_users)
2383 silc_server_send_notify_to_channel(server, channel,
2384 "%s@%s has left channel %s",
2387 sock->hostname : sock->ip,
2388 channel->channel_name);
2390 silc_idlist_del_channel(server->local_list, channel);
2394 channel->user_list[k].client = NULL;
2395 channel->user_list[k].mode = SILC_CHANNEL_UMODE_NONE;
2397 /* Send notify to channel about client leaving the channel */
2399 silc_server_send_notify_to_channel(server, channel,
2400 "%s@%s has left channel %s",
2401 client->nickname, sock->hostname ?
2402 sock->hostname : sock->ip,
2403 channel->channel_name);
2411 /* Returns TRUE if the given client is on the channel. FALSE if not.
2412 This works because we assure that the user list on the channel is
2413 always in up to date thus we can only check the channel list from
2414 `client' which is faster than checking the user list from `channel'. */
2415 /* XXX This really is utility function and should be in eg. serverutil.c */
2417 int silc_server_client_on_channel(SilcClientEntry client,
2418 SilcChannelEntry channel)
2422 if (!client || !channel)
2425 for (i = 0; i < client->channel_count; i++) {
2426 if (client->channel[i] == channel)
2433 /* Timeout callback. This is called if connection is idle or for some
2434 other reason is not responding within some period of time. This
2435 disconnects the remote end. */
2437 SILC_TASK_CALLBACK(silc_server_timeout_remote)
2439 SilcServer server = (SilcServer)context;
2440 SilcSocketConnection sock = server->sockets[fd];
2442 silc_server_disconnect_remote(server, sock,
2443 "Server closed connection: "
2444 "Connection timeout");
2447 /* Internal routine used to send (relay, route) private messages to some
2448 destination. If the private message key does not exist then the message
2449 is re-encrypted, otherwise we just pass it along. */
2452 silc_server_private_message_send_internal(SilcServer server,
2453 SilcSocketConnection dst_sock,
2456 SilcPacketContext *packet)
2458 SilcBuffer buffer = packet->buffer;
2460 /* Send and re-encrypt if private messge key does not exist */
2461 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
2463 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2464 + packet->dst_id_len + packet->padlen);
2465 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2466 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2468 /* Re-encrypt packet */
2469 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
2471 /* Send the packet */
2472 silc_server_packet_send_real(server, dst_sock, FALSE);
2475 /* Key exist so just send it */
2476 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2477 + packet->dst_id_len + packet->padlen);
2478 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2479 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2480 silc_server_packet_send_real(server, dst_sock, FALSE);
2484 /* Received private message. This resolves the destination of the message
2485 and sends the packet. This is used by both server and router. If the
2486 destination is our locally connected client this sends the packet to
2487 the client. This may also send the message for further routing if
2488 the destination is not in our server (or router). */
2490 void silc_server_private_message(SilcServer server,
2491 SilcSocketConnection sock,
2492 SilcPacketContext *packet)
2494 SilcBuffer buffer = packet->buffer;
2496 SilcServerEntry router;
2497 SilcSocketConnection dst_sock;
2498 SilcClientEntry client;
2500 SILC_LOG_DEBUG(("Start"));
2502 if (!packet->dst_id) {
2503 SILC_LOG_ERROR(("Bad Client ID in private message packet, dropped"));
2507 /* Decode destination Client ID */
2508 id = silc_id_str2id(packet->dst_id, SILC_ID_CLIENT);
2510 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
2514 /* If the destination belongs to our server we don't have to route
2515 the message anywhere but to send it to the local destination. */
2516 client = silc_idlist_find_client_by_id(server->local_list, id);
2518 /* It exists, now deliver the message to the destination */
2519 dst_sock = (SilcSocketConnection)client->connection;
2521 /* If we are router and the client has router then the client is in
2522 our cell but not directly connected to us. */
2523 if (server->server_type == SILC_ROUTER && client->router) {
2524 /* We are of course in this case the client's router thus the real
2525 "router" of the client is the server who owns the client. Thus
2526 we will send the packet to that server. */
2527 router = (SilcServerEntry)dst_sock->user_data;
2528 // assert(client->router == server->id_entry);
2530 silc_server_private_message_send_internal(server, dst_sock,
2537 /* Seems that client really is directly connected to us */
2538 silc_server_private_message_send_internal(server, dst_sock,
2540 client->hmac, packet);
2544 /* Destination belongs to someone not in this server. If we are normal
2545 server our action is to send the packet to our router. */
2546 if (server->server_type == SILC_SERVER && !server->standalone) {
2547 router = server->id_entry->router;
2549 /* Send to primary route */
2551 dst_sock = (SilcSocketConnection)router->connection;
2552 silc_server_private_message_send_internal(server, dst_sock,
2554 router->hmac, packet);
2559 /* We are router and we will perform route lookup for the destination
2560 and send the message to fastest route. */
2561 if (server->server_type == SILC_ROUTER && !server->standalone) {
2562 dst_sock = silc_server_get_route(server, id, SILC_ID_CLIENT);
2563 router = (SilcServerEntry)dst_sock->user_data;
2565 /* Get fastest route and send packet. */
2567 silc_server_private_message_send_internal(server, dst_sock,
2569 router->hmac, packet);
2575 silc_server_send_error(server, sock,
2576 "No such nickname: Private message not sent");
2578 silc_buffer_free(buffer);
2581 /* Process received channel message. The message can be originated from
2582 client or server. */
2584 void silc_server_channel_message(SilcServer server,
2585 SilcSocketConnection sock,
2586 SilcPacketContext *packet)
2588 SilcChannelEntry channel = NULL;
2589 SilcClientEntry client = NULL;
2590 SilcChannelID *id = NULL;
2591 void *sender = NULL;
2592 SilcBuffer buffer = packet->buffer;
2595 SILC_LOG_DEBUG(("Processing channel message"));
2598 if (packet->dst_id_type != SILC_ID_CHANNEL) {
2599 SILC_LOG_ERROR(("Received bad message for channel, dropped"));
2600 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
2604 /* Find channel entry */
2605 id = silc_id_str2id(packet->dst_id, SILC_ID_CHANNEL);
2606 channel = silc_idlist_find_channel_by_id(server->local_list, id);
2608 SILC_LOG_DEBUG(("Could not find channel"));
2612 /* See that this client is on the channel. If the message is coming
2613 from router we won't do the check as the message is from client that
2614 we don't know about. Also, if the original sender is not client
2615 (as it can be server as well) we don't do the check. */
2616 sender = silc_id_str2id(packet->src_id, packet->src_id_type);
2617 if (sock->type != SILC_SOCKET_TYPE_ROUTER &&
2618 packet->src_id_type == SILC_ID_CLIENT) {
2619 for (i = 0; i < channel->user_list_count; i++) {
2620 client = channel->user_list[i].client;
2621 if (client && !SILC_ID_CLIENT_COMPARE(client->id, sender))
2624 if (i >= channel->user_list_count)
2628 /* Distribute the packet to our local clients. This will send the
2629 packet for further routing as well, if needed. */
2630 silc_server_packet_relay_to_channel(server, sock, channel, sender,
2631 packet->src_id_type,
2632 packet->buffer->data,
2633 packet->buffer->len, FALSE);
2640 silc_buffer_free(buffer);
2643 /* Received channel key packet. We distribute the key to all of our locally
2644 connected clients on the channel. */
2645 /* XXX Router must accept this packet and distribute the key to all its
2646 server that has clients on the channel */
2648 void silc_server_channel_key(SilcServer server,
2649 SilcSocketConnection sock,
2650 SilcPacketContext *packet)
2652 SilcBuffer buffer = packet->buffer;
2653 SilcChannelKeyPayload payload = NULL;
2654 SilcChannelID *id = NULL;
2655 SilcChannelEntry channel;
2656 SilcClientEntry client;
2658 unsigned int key_len;
2662 if (packet->src_id_type != SILC_ID_SERVER &&
2663 sock->type != SILC_SOCKET_TYPE_ROUTER)
2666 /* Decode channel key payload */
2667 payload = silc_channel_key_parse_payload(buffer);
2669 SILC_LOG_ERROR(("Bad channel key payload, dropped"));
2673 /* Get channel ID */
2674 id = silc_id_str2id(silc_channel_key_get_id(payload, NULL), SILC_ID_CHANNEL);
2678 /* Get the channel entry */
2679 channel = silc_idlist_find_channel_by_id(server->local_list, id);
2681 SILC_LOG_ERROR(("Received key for non-existent channel"));
2685 /* Save the key for us as well */
2686 key = silc_channel_key_get_key(payload, &key_len);
2689 cipher = silc_channel_key_get_cipher(payload, NULL);;
2692 channel->key_len = key_len * 8;
2693 channel->key = silc_calloc(key_len, sizeof(unsigned char));
2694 memcpy(channel->key, key, key_len);
2695 silc_cipher_alloc(cipher, &channel->channel_key);
2696 channel->channel_key->cipher->set_key(channel->channel_key->context,
2699 /* Distribute the key to all clients on the channel */
2700 for (i = 0; i < channel->user_list_count; i++) {
2701 client = channel->user_list[i].client;
2704 silc_server_packet_send_dest(server, client->connection,
2705 SILC_PACKET_CHANNEL_KEY, 0,
2706 client->id, SILC_ID_CLIENT,
2707 buffer->data, buffer->len, FALSE);
2714 silc_channel_key_free_payload(payload);
2715 silc_buffer_free(buffer);
2718 /* Sends error message. Error messages may or may not have any
2721 void silc_server_send_error(SilcServer server,
2722 SilcSocketConnection sock,
2723 const char *fmt, ...)
2726 unsigned char buf[4096];
2728 memset(buf, 0, sizeof(buf));
2730 vsprintf(buf, fmt, ap);
2733 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
2734 buf, strlen(buf), FALSE);
2737 /* Sends notify message */
2739 void silc_server_send_notify(SilcServer server,
2740 SilcSocketConnection sock,
2741 const char *fmt, ...)
2744 unsigned char buf[4096];
2746 memset(buf, 0, sizeof(buf));
2748 vsprintf(buf, fmt, ap);
2751 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0,
2752 buf, strlen(buf), FALSE);
2755 /* Sends notify message destined to specific entity. */
2757 void silc_server_send_notify_dest(SilcServer server,
2758 SilcSocketConnection sock,
2760 SilcIdType dest_id_type,
2761 const char *fmt, ...)
2764 unsigned char buf[4096];
2766 memset(buf, 0, sizeof(buf));
2768 vsprintf(buf, fmt, ap);
2771 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
2772 dest_id, dest_id_type,
2773 buf, strlen(buf), FALSE);
2776 /* Sends notify message to a channel. The notify message sent is
2777 distributed to all clients on the channel. Actually this is not real
2778 notify message, instead it is message to channel sent by server. But
2779 as server is sending it it will appear as notify type message on the
2782 void silc_server_send_notify_to_channel(SilcServer server,
2783 SilcChannelEntry channel,
2784 const char *fmt, ...)
2787 unsigned char buf[4096];
2789 memset(buf, 0, sizeof(buf));
2791 vsprintf(buf, fmt, ap);
2794 silc_server_packet_send_to_channel(server, channel, buf,
2795 strlen(buf), FALSE);
2798 /* Sends New ID Payload to remote end. The packet is used to distribute
2799 information about new registered clients, servers, channel etc. usually
2800 to routers so that they can keep these information up to date.
2801 If the argument `broadcast' is TRUE then the packet is sent as
2802 broadcast packet. */
2804 void silc_server_send_new_id(SilcServer server,
2805 SilcSocketConnection sock,
2807 void *id, SilcIdType id_type,
2808 unsigned int id_len)
2811 unsigned char *id_string;
2813 id_string = silc_id_id2str(id, id_type);
2817 packet = silc_buffer_alloc(2 + 2 + id_len);
2818 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2819 silc_buffer_format(packet,
2820 SILC_STR_UI_SHORT(id_type),
2821 SILC_STR_UI_SHORT(id_len),
2822 SILC_STR_UI_XNSTRING(id_string, id_len),
2825 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
2826 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
2827 packet->data, packet->len, FALSE);
2828 silc_free(id_string);
2829 silc_buffer_free(packet);
2832 /* Sends Replace ID payload to remote end. This is used to replace old
2833 ID with new ID sent in the packet. This is called for example when
2834 user changes nickname and we create new ID for the user. If the
2835 argument `broadcast' is TRUE then the packet is sent as
2836 broadcast packet. */
2837 /* XXX It would be expected that the new id is same type as the old
2840 void silc_server_send_replace_id(SilcServer server,
2841 SilcSocketConnection sock,
2843 void *old_id, SilcIdType old_id_type,
2844 unsigned int old_id_len,
2845 void *new_id, SilcIdType new_id_type,
2846 unsigned int new_id_len)
2852 oid = silc_id_id2str(old_id, old_id_type);
2856 nid = silc_id_id2str(new_id, new_id_type);
2860 packet = silc_buffer_alloc(2 + 2 + 2 + 2 + old_id_len + new_id_len);
2861 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2862 silc_buffer_format(packet,
2863 SILC_STR_UI_SHORT(old_id_type),
2864 SILC_STR_UI_SHORT(old_id_len),
2865 SILC_STR_UI_XNSTRING(oid, old_id_len),
2866 SILC_STR_UI_SHORT(new_id_type),
2867 SILC_STR_UI_SHORT(new_id_len),
2868 SILC_STR_UI_XNSTRING(nid, new_id_len),
2871 silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID,
2872 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
2873 packet->data, packet->len, FALSE);
2876 silc_buffer_free(packet);
2879 /* This function is used to send Remove Channel User payload. This may sent
2880 by server but is usually used only by router to notify other routers that
2881 user has left a channel. Normal server sends this packet to its router
2882 to notify that the router should not hold a record about this client
2883 on a channel anymore. Router distributes it further to other routers. */
2885 void silc_server_send_remove_channel_user(SilcServer server,
2886 SilcSocketConnection sock,
2888 void *client_id, void *channel_id)
2891 unsigned char *clid, *chid;
2893 clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
2897 chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
2901 packet = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN + SILC_ID_CHANNEL_LEN);
2902 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2903 silc_buffer_format(packet,
2904 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
2905 SILC_STR_UI_XNSTRING(clid, SILC_ID_CLIENT_LEN),
2906 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
2907 SILC_STR_UI_XNSTRING(chid, SILC_ID_CHANNEL_LEN),
2910 silc_server_packet_send(server, sock, SILC_PACKET_REMOVE_CHANNEL_USER,
2911 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
2912 packet->data, packet->len, FALSE);
2915 silc_buffer_free(packet);
2918 /* Received packet to replace a ID. This checks that the requested ID
2919 exists and replaces it with the new one. */
2921 void silc_server_replace_id(SilcServer server,
2922 SilcSocketConnection sock,
2923 SilcPacketContext *packet)
2925 SilcBuffer buffer = packet->buffer;
2926 unsigned char *old_id = NULL, *new_id = NULL;
2927 SilcIdType old_id_type, new_id_type;
2928 unsigned short old_id_len, new_id_len;
2929 void *id = NULL, *id2 = NULL;
2931 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2932 packet->src_id_type == SILC_ID_CLIENT)
2935 SILC_LOG_DEBUG(("Replacing ID"));
2937 silc_buffer_unformat(buffer,
2938 SILC_STR_UI_SHORT(&old_id_type),
2939 SILC_STR_UI16_NSTRING_ALLOC(&old_id, &old_id_len),
2940 SILC_STR_UI_SHORT(&new_id_type),
2941 SILC_STR_UI16_NSTRING_ALLOC(&new_id, &new_id_len),
2944 if (old_id_type != new_id_type)
2947 if (old_id_len != silc_id_get_len(old_id_type) ||
2948 new_id_len != silc_id_get_len(new_id_type))
2951 id = silc_id_str2id(old_id, old_id_type);
2955 id2 = silc_id_str2id(new_id, new_id_type);
2959 /* Replace the old ID */
2960 switch(old_id_type) {
2961 case SILC_ID_CLIENT:
2962 if (silc_idlist_replace_client_id(server->local_list, id, id2) == NULL)
2963 if (server->server_type == SILC_ROUTER)
2964 silc_idlist_replace_client_id(server->global_list, id, id2);
2967 case SILC_ID_SERVER:
2968 if (silc_idlist_replace_server_id(server->local_list, id, id2) == NULL)
2969 if (server->server_type == SILC_ROUTER)
2970 silc_idlist_replace_server_id(server->global_list, id, id2);
2973 case SILC_ID_CHANNEL:
2974 /* XXX Hmm... Basically this cannot occur. Channel ID's cannot be
2993 /* Creates new channel. */
2995 SilcChannelEntry silc_server_new_channel(SilcServer server,
2996 SilcServerID *router_id,
2997 char *cipher, char *channel_name)
2999 int i, channel_len, key_len;
3000 SilcChannelID *channel_id;
3001 SilcChannelEntry entry;
3003 unsigned char channel_key[32], *id_string;
3006 SILC_LOG_DEBUG(("Creating new channel"));
3008 /* Create channel key */
3009 for (i = 0; i < 32; i++)
3010 channel_key[i] = silc_rng_get_byte(server->rng);
3017 silc_cipher_alloc(cipher, &key);
3018 key->cipher->set_key(key->context, channel_key, key_len);
3020 /* Create the channel */
3021 silc_id_create_channel_id(router_id, server->rng, &channel_id);
3022 entry = silc_idlist_add_channel(server->local_list, channel_name,
3023 SILC_CHANNEL_MODE_NONE, channel_id,
3029 silc_idcache_add(server->local_list->channels, channel_name,
3030 SILC_ID_CHANNEL, channel_id, (void *)entry, TRUE);
3032 entry->key = silc_calloc(key_len, sizeof(*entry->key));
3033 entry->key_len = key_len * 8;
3034 memcpy(entry->key, channel_key, key_len);
3035 memset(channel_key, 0, sizeof(channel_key));
3037 /* Notify other routers about the new channel. We send the packet
3038 to our primary route. */
3039 if (server->standalone == FALSE) {
3040 channel_len = strlen(channel_name);
3041 id_string = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3042 packet = silc_buffer_alloc(2 + SILC_ID_CHANNEL_LEN);
3044 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
3045 silc_buffer_format(packet,
3046 SILC_STR_UI_SHORT(channel_len),
3047 SILC_STR_UI_XNSTRING(channel_name, channel_len),
3048 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
3049 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CHANNEL_LEN),
3052 /* Send the packet to our router. */
3053 silc_server_packet_send(server, (SilcSocketConnection)
3054 server->id_entry->router->connection,
3055 SILC_PACKET_NEW_CHANNEL_USER, 0,
3056 packet->data, packet->len, TRUE);
3058 silc_free(id_string);
3059 silc_buffer_free(packet);
3065 /* Create new client. This processes incoming NEW_CLIENT packet and creates
3066 Client ID for the client. Client becomes registered after calling this
3069 SilcClientEntry silc_server_new_client(SilcServer server,
3070 SilcSocketConnection sock,
3071 SilcPacketContext *packet)
3073 SilcBuffer buffer = packet->buffer;
3074 SilcClientEntry client;
3075 SilcIDCacheEntry cache;
3076 SilcClientID *client_id;
3078 char *username = NULL, *realname = NULL, *id_string;
3080 SILC_LOG_DEBUG(("Creating new client"));
3082 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
3085 /* Take client entry */
3086 client = (SilcClientEntry)sock->user_data;
3088 /* Fetch the old client cache entry so that we can update it. */
3089 if (!silc_idcache_find_by_context(server->local_list->clients,
3090 sock->user_data, &cache)) {
3091 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
3095 /* Parse incoming packet */
3096 silc_buffer_unformat(buffer,
3097 SILC_STR_UI16_STRING_ALLOC(&username),
3098 SILC_STR_UI16_STRING_ALLOC(&realname),
3101 /* Create Client ID */
3102 silc_id_create_client_id(server->id, server->rng, server->md5hash,
3103 username, &client_id);
3105 /* Update client entry */
3106 client->registered = TRUE;
3107 client->nickname = strdup(username);
3108 client->username = username;
3109 client->userinfo = realname;
3110 client->id = client_id;
3112 /* Update the cache entry */
3113 cache->id = (void *)client_id;
3114 cache->type = SILC_ID_CLIENT;
3115 cache->data = username;
3116 silc_idcache_sort_by_data(server->local_list->clients);
3118 /* Notify our router about new client on the SILC network */
3119 if (!server->standalone)
3120 silc_server_send_new_id(server, (SilcSocketConnection)
3121 server->id_entry->router->connection,
3122 server->server_type == SILC_SERVER ? TRUE : FALSE,
3123 client->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
3125 /* Send the new client ID to the client. */
3126 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
3127 reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
3128 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
3129 silc_buffer_format(reply,
3130 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
3131 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
3132 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
3134 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
3135 reply->data, reply->len, FALSE);
3136 silc_free(id_string);
3137 silc_buffer_free(reply);
3139 /* Send some nice info to the client */
3140 silc_server_send_notify(server, sock,
3141 "Welcome to the SILC Network %s@%s",
3143 sock->hostname ? sock->hostname : sock->ip);
3144 silc_server_send_notify(server, sock,
3145 "Your host is %s, running version %s",
3146 server->config->server_info->server_name,
3148 silc_server_send_notify(server, sock,
3149 "Your connection is secured with %s cipher, "
3150 "key length %d bits",
3151 client->send_key->cipher->name,
3152 client->send_key->cipher->key_len);
3153 silc_server_send_notify(server, sock,
3154 "Your current nickname is %s",
3162 /* Create new server. This processes incoming NEW_SERVER packet and
3163 saves the received Server ID. The server is our locally connected
3164 server thus we save all the information and save it to local list.
3165 This funtion can be used by both normal server and router server.
3166 If normal server uses this it means that its router has connected
3167 to the server. If router uses this it means that one of the cell's
3168 servers is connected to the router. */
3170 SilcServerEntry silc_server_new_server(SilcServer server,
3171 SilcSocketConnection sock,
3172 SilcPacketContext *packet)
3174 SilcBuffer buffer = packet->buffer;
3175 SilcServerEntry new_server;
3176 SilcIDCacheEntry cache;
3177 SilcServerID *server_id;
3178 unsigned char *server_name, *id_string;
3180 SILC_LOG_DEBUG(("Creating new server"));
3182 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
3183 sock->type != SILC_SOCKET_TYPE_ROUTER)
3186 /* Take server entry */
3187 new_server = (SilcServerEntry)sock->user_data;
3189 /* Fetch the old server cache entry so that we can update it. */
3190 if (!silc_idcache_find_by_context(server->local_list->servers,
3191 sock->user_data, &cache)) {
3192 SILC_LOG_ERROR(("Lost server's cache entry - bad thing"));
3196 /* Parse the incoming packet */
3197 silc_buffer_unformat(buffer,
3198 SILC_STR_UI16_STRING_ALLOC(&id_string),
3199 SILC_STR_UI16_STRING_ALLOC(&server_name),
3203 server_id = silc_id_str2id(id_string, SILC_ID_SERVER);
3204 silc_free(id_string);
3206 /* Update client entry */
3207 new_server->registered = TRUE;
3208 new_server->server_name = server_name;
3209 new_server->id = server_id;
3211 /* Update the cache entry */
3212 cache->id = (void *)server_id;
3213 cache->type = SILC_ID_SERVER;
3214 cache->data = server_name;
3215 silc_idcache_sort_by_data(server->local_list->servers);
3217 /* Distribute the information about new server in the SILC network
3218 to our router. If we are normal server we won't send anything
3219 since this connection must be our router connection. */
3220 if (server->server_type == SILC_ROUTER && !server->standalone)
3221 silc_server_send_new_id(server, server->id_entry->router->connection,
3222 TRUE, new_server->id, SILC_ID_SERVER,
3223 SILC_ID_SERVER_LEN);
3228 /* Processes incoming New ID Payload. New ID Payload is used to distribute
3229 information about newly registered clients, servers and created
3232 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
3233 SilcPacketContext *packet)
3235 SilcBuffer buffer = packet->buffer;
3237 SilcServerEntry tmpserver, router;
3238 SilcSocketConnection router_sock;
3240 unsigned char *id_string;
3243 SILC_LOG_DEBUG(("Processing new ID"));
3245 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3246 server->server_type == SILC_SERVER ||
3247 packet->src_id_type != SILC_ID_SERVER)
3250 silc_buffer_unformat(buffer,
3251 SILC_STR_UI_SHORT(&id_type),
3252 SILC_STR_UI16_STRING_ALLOC(&id_string),
3255 /* Normal server cannot have other normal server connections */
3256 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
3259 id = silc_id_str2id(id_string, id_type);
3263 /* If the packet is originated from the one who sent it to us we know
3264 that the ID belongs to our cell, unless the sender was router. */
3265 tmpid = silc_id_str2id(packet->src_id, SILC_ID_SERVER);
3266 tmpserver = (SilcServerEntry)sock->user_data;
3268 if (!SILC_ID_SERVER_COMPARE(tmpid, tmpserver->id) &&
3269 sock->type == SILC_SOCKET_TYPE_SERVER) {
3270 id_list = server->local_list;
3272 router = sock->user_data;
3273 /* router = server->id_entry; */
3275 id_list = server->global_list;
3276 router_sock = (SilcSocketConnection)server->id_entry->router->connection;
3277 router = server->id_entry->router;
3283 case SILC_ID_CLIENT:
3285 SilcClientEntry idlist;
3287 /* Add the client to our local list. We are router and we keep
3288 cell specific local database of all clients in the cell. */
3289 idlist = silc_idlist_add_client(id_list, NULL, NULL, NULL,
3290 id, router, NULL, NULL,
3291 NULL, NULL, NULL, router_sock);
3295 case SILC_ID_SERVER:
3297 SilcServerEntry idlist;
3299 /* Add the server to our local list. We are router and we keep
3300 cell specific local database of all servers in the cell. */
3301 idlist = silc_idlist_add_server(id_list, NULL, 0,
3302 id, router, NULL, NULL,
3303 NULL, NULL, NULL, router_sock);
3307 case SILC_ID_CHANNEL:
3308 /* Add the channel to our local list. We are router and we keep
3309 cell specific local database of all channels in the cell. */
3310 silc_idlist_add_channel(id_list, NULL, 0, id, router, NULL);
3319 silc_free(id_string);
3322 /* Received packet to remove a user from a channel. Routers notify other
3323 routers that user has left a channel. Client must not send this packet.
3324 Normal server may send this packet but ignores if it receives one. */
3326 void silc_server_remove_channel_user(SilcServer server,
3327 SilcSocketConnection sock,
3328 SilcPacketContext *packet)
3330 SilcBuffer buffer = packet->buffer;
3331 unsigned char *tmp1 = NULL, *tmp2 = NULL;
3332 SilcClientID *client_id = NULL;
3333 SilcChannelID *channel_id = NULL;
3334 SilcChannelEntry channel;
3335 SilcClientEntry client;
3337 SILC_LOG_DEBUG(("Removing user from channel"));
3339 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3340 server->server_type == SILC_SERVER)
3343 silc_buffer_unformat(buffer,
3344 SILC_STR_UI16_STRING_ALLOC(&tmp1),
3345 SILC_STR_UI16_STRING_ALLOC(&tmp2),
3351 client_id = silc_id_str2id(tmp1, SILC_ID_CLIENT);
3352 channel_id = silc_id_str2id(tmp2, SILC_ID_CHANNEL);
3353 if (!client_id || !channel_id)
3356 /* XXX routers should check server->global_list as well */
3357 /* Get channel entry */
3358 channel = silc_idlist_find_channel_by_id(server->local_list, channel_id);
3362 /* XXX routers should check server->global_list as well */
3363 /* Get client entry */
3364 client = silc_idlist_find_client_by_id(server->local_list, client_id);
3368 /* Remove from channel */
3369 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
3377 silc_free(client_id);
3379 silc_free(channel_id);