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.
27 #include "serverincludes.h"
28 #include "server_internal.h"
30 /* Static prototypes */
31 SILC_TASK_CALLBACK(silc_server_connect_to_router);
32 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
33 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
34 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
35 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
36 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
37 SILC_TASK_CALLBACK(silc_server_packet_process);
38 SILC_TASK_CALLBACK(silc_server_packet_parse_real);
39 SILC_TASK_CALLBACK(silc_server_timeout_remote);
41 extern char *server_version;
43 /* Allocates a new SILC server object. This has to be done before the server
44 can be used. After allocation one must call silc_server_init to initialize
45 the server. The new allocated server object is returned to the new_server
48 int silc_server_alloc(SilcServer *new_server)
52 SILC_LOG_DEBUG(("Allocating new server object"));
54 server = silc_calloc(1, sizeof(*server));
55 server->server_type = SILC_SERVER;
56 server->standalone = FALSE;
57 server->local_list = silc_calloc(1, sizeof(*server->local_list));
58 server->global_list = silc_calloc(1, sizeof(*server->global_list));
65 /* Free's the SILC server object. This is called at the very end before
68 void silc_server_free(SilcServer server)
71 if (server->local_list)
72 silc_free(server->local_list);
73 if (server->global_list)
74 silc_free(server->global_list);
76 silc_rng_free(server->rng);
78 silc_math_primegen_uninit(); /* XXX */
83 /* Initializes the entire SILC server. This is called always before running
84 the server. This is called only once at the initialization of the program.
85 This binds the server to its listenning port. After this function returns
86 one should call silc_server_run to start the server. This returns TRUE
87 when everything is ok to run the server. Configuration file must be
88 read and parsed before calling this. */
90 int silc_server_init(SilcServer server)
92 int *sock = NULL, sock_count = 0, i;
94 SilcServerEntry id_entry;
96 SILC_LOG_DEBUG(("Initializing server"));
98 assert(server->config);
100 /* Set log files where log message should be saved. */
101 server->config->server = server;
102 silc_config_server_setlogfiles(server->config);
104 /* Register all configured ciphers, PKCS and hash functions. */
105 silc_config_server_register_ciphers(server->config);
106 silc_config_server_register_pkcs(server->config);
107 silc_config_server_register_hashfuncs(server->config);
109 /* Initialize random number generator for the server. */
110 server->rng = silc_rng_alloc();
111 silc_rng_init(server->rng);
112 silc_math_primegen_init(); /* XXX */
114 /* Initialize hash functions for server to use */
115 silc_hash_alloc("md5", &server->md5hash);
116 silc_hash_alloc("sha1", &server->sha1hash);
118 /* Initialize none cipher */
119 silc_cipher_alloc("none", &server->none_cipher);
121 /* XXXXX Generate RSA key pair */
123 unsigned char *public_key;
124 unsigned char *private_key;
125 unsigned int pk_len, prv_len;
128 if (stat("pubkey.pub", &st) < 0 && stat("privkey.prv", &st) < 0) {
130 if (silc_pkcs_alloc("rsa", &server->pkcs) == FALSE) {
131 SILC_LOG_ERROR(("Could not create RSA key pair"));
135 if (server->pkcs->pkcs->init(server->pkcs->context,
136 1024, server->rng) == FALSE) {
137 SILC_LOG_ERROR(("Could not generate RSA key pair"));
141 public_key = server->pkcs->pkcs->get_public_key(server->pkcs->context,
143 private_key = server->pkcs->pkcs->get_private_key(server->pkcs->context,
146 SILC_LOG_HEXDUMP(("public key"), public_key, pk_len);
147 SILC_LOG_HEXDUMP(("private key"), private_key, prv_len);
150 silc_pkcs_public_key_alloc("rsa", "UN=root, HN=dummy",
152 server->private_key =
153 silc_pkcs_private_key_alloc("rsa", private_key, prv_len);
156 silc_pkcs_save_public_key("pubkey.pub", server->public_key,
158 silc_pkcs_save_private_key("privkey.prv", server->private_key, NULL,
161 memset(public_key, 0, pk_len);
162 memset(private_key, 0, prv_len);
163 silc_free(public_key);
164 silc_free(private_key);
166 silc_pkcs_load_public_key("pubkey.pub", &server->public_key,
168 silc_pkcs_load_private_key("privkey.prv", &server->private_key,
173 /* Create a listening server. Note that our server can listen on
174 multiple ports. All listeners are created here and now. */
175 /* XXX Still check this whether to use server_info or listen_port. */
177 while(server->config->listen_port) {
180 tmp = silc_net_create_server(server->config->listen_port->port,
181 server->config->listen_port->host);
185 sock = silc_realloc(sock, (sizeof(int *) * (sock_count + 1)));
186 sock[sock_count] = tmp;
187 server->config->listen_port = server->config->listen_port->next;
191 /* Initialize ID caches */
192 server->local_list->clients = silc_idcache_alloc(0);
193 server->local_list->servers = silc_idcache_alloc(0);
194 server->local_list->channels = silc_idcache_alloc(0);
196 /* XXX for now these are allocated for normal server as well as these
197 hold some global information that the server has fetched from its
198 router. For router these are used as they are supposed to be used
199 on router. The XXX can be remoevd later if this is the way we are
200 going to do this in the normal server as well. */
201 server->global_list->clients = silc_idcache_alloc(0);
202 server->global_list->servers = silc_idcache_alloc(0);
203 server->global_list->channels = silc_idcache_alloc(0);
205 /* Allocate the entire socket list that is used in server. Eventually
206 all connections will have entry in this table (it is a table of
207 pointers to the actual object that is allocated individually
209 server->sockets = silc_calloc(SILC_SERVER_MAX_CONNECTIONS,
210 sizeof(*server->sockets));
212 for (i = 0; i < sock_count; i++) {
213 SilcSocketConnection newsocket = NULL;
215 /* Set socket to non-blocking mode */
216 silc_net_set_socket_nonblock(sock[i]);
217 server->sock = sock[i];
219 /* Create a Server ID for the server. */
220 silc_id_create_server_id(sock[i], server->rng, &id);
226 server->id_type = SILC_ID_SERVER;
227 server->server_name = server->config->server_info->server_name;
229 /* Add ourselves to the server list. We don't have a router yet
230 beacuse we haven't established a route yet. It will be done later.
231 For now, NULL is sent as router. This allocates new entry to
234 silc_idlist_add_server(server->local_list,
235 server->config->server_info->server_name,
236 server->server_type, server->id, NULL,
237 server->send_key, server->receive_key,
238 NULL, NULL, NULL, NULL);
240 SILC_LOG_ERROR(("Could not add ourselves to cache"));
244 /* Add ourselves also to the socket table. The entry allocated above
245 is sent as argument for fast referencing in the future. */
246 silc_socket_alloc(sock[i], SILC_SOCKET_TYPE_SERVER, id_entry,
251 server->sockets[sock[i]] = newsocket;
253 /* Put the allocated socket pointer also to the entry allocated above
254 for fast back-referencing to the socket list. */
255 id_entry->connection = (void *)server->sockets[sock[i]];
256 server->id_entry = id_entry;
259 /* Register the task queues. In SILC we have by default three task queues.
260 One task queue for non-timeout tasks which perform different kind of
261 I/O on file descriptors, timeout task queue for timeout tasks, and,
262 generic non-timeout task queue whose tasks apply to all connections. */
263 silc_task_queue_alloc(&server->io_queue, TRUE);
264 if (!server->io_queue) {
267 silc_task_queue_alloc(&server->timeout_queue, TRUE);
268 if (!server->timeout_queue) {
271 silc_task_queue_alloc(&server->generic_queue, TRUE);
272 if (!server->generic_queue) {
276 /* Register protocols */
277 silc_server_protocols_register();
279 /* Initialize the scheduler */
280 silc_schedule_init(&server->io_queue, &server->timeout_queue,
281 &server->generic_queue,
282 SILC_SERVER_MAX_CONNECTIONS);
284 /* Add the first task to the queue. This is task that is executed by
285 timeout. It expires as soon as the caller calls silc_server_run. This
286 task performs authentication protocol and key exchange with our
288 if (silc_task_register(server->timeout_queue, sock[0],
289 silc_server_connect_to_router,
290 (void *)server, 0, 1,
292 SILC_TASK_PRI_NORMAL) == NULL) {
296 /* If server connections has been configured then we must be router as
297 normal server cannot have server connections, only router connections. */
298 if (server->config->servers)
299 server->server_type = SILC_ROUTER;
301 SILC_LOG_DEBUG(("Server initialized"));
303 /* We are done here, return succesfully */
307 silc_task_queue_free(server->timeout_queue);
309 silc_task_queue_free(server->io_queue);
311 for (i = 0; i < sock_count; i++)
312 silc_net_close_server(sock[i]);
317 /* Stops the SILC server. This function is used to shutdown the server.
318 This is usually called after the scheduler has returned. After stopping
319 the server one should call silc_server_free. */
321 void silc_server_stop(SilcServer server)
323 SILC_LOG_DEBUG(("Stopping server"));
325 /* Stop the scheduler, although it might be already stopped. This
326 doesn't hurt anyone. This removes all the tasks and task queues,
328 silc_schedule_stop();
329 silc_schedule_uninit();
331 silc_server_protocols_unregister();
333 SILC_LOG_DEBUG(("Server stopped"));
336 /* The heart of the server. This runs the scheduler thus runs the server. */
338 void silc_server_run(SilcServer server)
340 SILC_LOG_DEBUG(("Running server"));
342 /* Start the scheduler, the heart of the SILC server. When this returns
343 the program will be terminated. */
347 /* This function connects to our primary router or if we are a router this
348 establishes all our primary routes. This is called at the start of the
349 server to do authentication and key exchange with our router - called
352 SILC_TASK_CALLBACK(silc_server_connect_to_router)
354 SilcServer server = (SilcServer)context;
355 SilcSocketConnection newsocket;
358 SILC_LOG_DEBUG(("Connecting to router(s)"));
360 /* if we are normal SILC server we need to connect to our cell's
362 if (server->server_type == SILC_SERVER) {
363 SilcProtocol protocol;
364 SilcServerKEInternalContext *proto_ctx;
366 /* Create connection to the router, if configured. */
367 if (server->config->routers) {
368 sock = silc_net_create_connection(server->config->routers->port,
369 server->config->routers->host);
371 SILC_LOG_ERROR(("Could not connect to router"));
372 silc_schedule_stop();
376 /* Set socket options */
377 silc_net_set_socket_nonblock(sock);
378 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
380 /* Create socket connection for the connection. Even though we
381 know that we are connecting to a router we will mark the socket
382 to be unknown connection until we have executed authentication
384 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
385 server->sockets[sock] = newsocket;
386 newsocket->hostname = server->config->routers->host;
387 newsocket->port = server->config->routers->port;
389 /* Allocate internal protocol context. This is sent as context
391 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
392 proto_ctx->server = context;
393 proto_ctx->sock = newsocket;
394 proto_ctx->rng = server->rng;
395 proto_ctx->responder = FALSE;
397 /* Perform key exchange protocol. silc_server_connect_to_router_second
398 will be called after the protocol is finished. */
399 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
400 &protocol, proto_ctx,
401 silc_server_connect_to_router_second);
402 newsocket->protocol = protocol;
404 /* Register a timeout task that will be executed if the protocol
405 is not executed within 60 seconds. For now, this is a hard coded
406 limit. After 60 secs the connection will be closed if the key
407 exchange protocol has not been executed. */
408 proto_ctx->timeout_task =
409 silc_task_register(server->timeout_queue, sock,
410 silc_server_timeout_remote,
415 /* Register the connection for network input and output. This sets
416 that scheduler will listen for incoming packets for this connection
417 and sets that outgoing packets may be sent to this connection as
418 well. However, this doesn't set the scheduler for outgoing traffic,
419 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
420 later when outgoing data is available. */
421 SILC_REGISTER_CONNECTION_FOR_IO(sock);
423 /* Run the protocol */
424 protocol->execute(server->timeout_queue, 0, protocol, sock, 0, 0);
429 /* if we are a SILC router we need to establish all of our primary
431 if (server->server_type == SILC_ROUTER) {
432 SilcConfigServerSectionServerConnection *ptr;
434 /* Create the connections to all our routes */
435 ptr = server->config->routers;
437 SilcProtocol protocol;
438 SilcServerKEInternalContext *proto_ctx;
440 /* Create the connection to the remote end */
441 sock = silc_net_create_connection(ptr->port, ptr->host);
443 SILC_LOG_ERROR(("Could not connect to router"));
444 silc_schedule_stop();
448 /* Set socket options */
449 silc_net_set_socket_nonblock(sock);
450 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
452 /* Create socket connection for the connection. Even though we
453 know that we are connecting to a router we will mark the socket
454 to be unknown connection until we have executed authentication
456 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
457 server->sockets[sock] = newsocket;
458 newsocket->hostname = ptr->host;
459 newsocket->port = ptr->port;
461 /* Allocate internal protocol context. This is sent as context
463 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
464 proto_ctx->server = context;
465 proto_ctx->sock = newsocket;
466 proto_ctx->rng = server->rng;
467 proto_ctx->responder = FALSE;
469 /* Perform key exchange protocol. silc_server_connect_to_router_final
470 will be called after the protocol is finished. */
471 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
472 &protocol, proto_ctx,
473 silc_server_connect_to_router_second);
474 newsocket->protocol = protocol;
476 /* Register a timeout task that will be executed if the protocol
477 is not executed within 60 seconds. For now, this is a hard coded
478 limit. After 60 secs the connection will be closed if the key
479 exchange protocol has not been executed. */
480 proto_ctx->timeout_task =
481 silc_task_register(server->timeout_queue, sock,
482 silc_server_timeout_remote,
487 /* Register the connection for network input and output. This sets
488 that scheduler will listen for incoming packets for this connection
489 and sets that outgoing packets may be sent to this connection as
490 well. However, this doesn't set the scheduler for outgoing traffic,
491 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
492 later when outgoing data is available. */
493 SILC_REGISTER_CONNECTION_FOR_IO(sock);
495 /* Run the protocol */
496 protocol->execute(server->timeout_queue, 0, protocol, sock, 0, 0);
505 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
507 /* There wasn't a configured router, we will continue but we don't
508 have a connection to outside world. We will be standalone server. */
509 server->standalone = TRUE;
511 /* Add a task to the queue. This task receives new connections to the
512 server. This task remains on the queue until the end of the program. */
513 if (silc_task_register(server->io_queue, fd,
514 silc_server_accept_new_connection,
515 (void *)server, 0, 0,
517 SILC_TASK_PRI_NORMAL) == NULL) {
518 silc_schedule_stop();
523 /* Second part of connecting to router(s). Key exchange protocol has been
524 executed and now we will execute authentication protocol. */
526 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
528 SilcProtocol protocol = (SilcProtocol)context;
529 SilcServerKEInternalContext *ctx =
530 (SilcServerKEInternalContext *)protocol->context;
531 SilcServer server = (SilcServer)ctx->server;
532 SilcSocketConnection sock = NULL;
533 SilcServerConnAuthInternalContext *proto_ctx;
535 SILC_LOG_DEBUG(("Start"));
537 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
538 /* Error occured during protocol */
539 silc_protocol_free(protocol);
541 silc_buffer_free(ctx->packet);
543 silc_ske_free(ctx->ske);
545 silc_free(ctx->dest_id);
547 sock->protocol = NULL;
548 silc_server_disconnect_remote(server, sock, "Server closed connection: "
549 "Key exchange failed");
553 /* Allocate internal context for the authentication protocol. This
554 is sent as context for the protocol. */
555 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
556 proto_ctx->server = (void *)server;
557 proto_ctx->sock = sock = server->sockets[fd];
558 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
559 proto_ctx->dest_id_type = ctx->dest_id_type;
560 proto_ctx->dest_id = ctx->dest_id;
562 /* Resolve the authentication method used in this connection */
563 proto_ctx->auth_meth = SILC_PROTOCOL_CONN_AUTH_PASSWORD;
564 if (server->config->routers) {
565 SilcConfigServerSectionServerConnection *conn = NULL;
567 /* Check if we find a match from user configured connections */
568 conn = silc_config_server_find_router_conn(server->config,
572 /* Match found. Use the configured authentication method */
573 proto_ctx->auth_meth = conn->auth_meth;
574 if (conn->auth_data) {
575 proto_ctx->auth_data = strdup(conn->auth_data);
576 proto_ctx->auth_data_len = strlen(conn->auth_data);
579 /* No match found. */
586 /* Free old protocol as it is finished now */
587 silc_protocol_free(protocol);
589 silc_buffer_free(ctx->packet);
591 sock->protocol = NULL;
593 /* Allocate the authentication protocol. This is allocated here
594 but we won't start it yet. We will be receiving party of this
595 protocol thus we will wait that connecting party will make
597 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
598 &sock->protocol, proto_ctx,
599 silc_server_connect_to_router_final);
601 /* Register timeout task. If the protocol is not executed inside
602 this timelimit the connection will be terminated. Currently
603 this is 15 seconds and is hard coded limit (XXX). */
604 proto_ctx->timeout_task =
605 silc_task_register(server->timeout_queue, sock->sock,
606 silc_server_timeout_remote,
607 (void *)server, 15, 0,
611 /* Run the protocol */
612 sock->protocol->execute(server->timeout_queue, 0,
613 sock->protocol, sock->sock, 0, 0);
616 /* Finalizes the connection to router. Registers a server task to the
617 queue so that we can accept new connections. */
619 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
621 SilcProtocol protocol = (SilcProtocol)context;
622 SilcServerConnAuthInternalContext *ctx =
623 (SilcServerConnAuthInternalContext *)protocol->context;
624 SilcServer server = (SilcServer)ctx->server;
625 SilcSocketConnection sock = ctx->sock;
626 SilcServerEntry id_entry;
627 SilcUnknownEntry conn_data;
629 unsigned char *id_string;
631 SILC_LOG_DEBUG(("Start"));
633 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
634 /* Error occured during protocol */
635 silc_protocol_free(protocol);
637 silc_buffer_free(ctx->packet);
639 silc_ske_free(ctx->ske);
641 silc_free(ctx->dest_id);
643 sock->protocol = NULL;
644 silc_server_disconnect_remote(server, sock, "Server closed connection: "
645 "Authentication failed");
649 /* Add a task to the queue. This task receives new connections to the
650 server. This task remains on the queue until the end of the program. */
651 if (!server->listenning) {
652 if (silc_task_register(server->io_queue, server->sock,
653 silc_server_accept_new_connection,
654 (void *)server, 0, 0,
656 SILC_TASK_PRI_NORMAL) == NULL) {
657 silc_schedule_stop();
660 server->listenning = TRUE;
664 /* Send NEW_SERVER packet to the router. We will become registered
665 to the SILC network after sending this packet. */
666 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
667 packet = silc_buffer_alloc(2 + 2 + SILC_ID_SERVER_LEN +
668 strlen(server->server_name));
669 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
670 silc_buffer_format(packet,
671 SILC_STR_UI_SHORT(SILC_ID_SERVER_LEN),
672 SILC_STR_UI_XNSTRING(id_string, SILC_ID_SERVER_LEN),
673 SILC_STR_UI_SHORT(strlen(server->server_name)),
674 SILC_STR_UI_XNSTRING(server->server_name,
675 strlen(server->server_name)),
678 /* Send the packet */
679 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
680 packet->data, packet->len, TRUE);
681 silc_buffer_free(packet);
682 silc_free(id_string);
684 SILC_LOG_DEBUG(("Connected to router %s", sock->hostname));
686 /* Add the connected router to local server list */
687 server->standalone = FALSE;
688 conn_data = (SilcUnknownEntry)sock->user_data;
690 silc_idlist_add_server(server->local_list,
691 sock->hostname ? sock->hostname : sock->ip,
692 SILC_ROUTER, ctx->dest_id, NULL,
693 conn_data->send_key, conn_data->receive_key,
694 conn_data->pkcs, conn_data->hmac, NULL, sock);
696 id_entry->hmac_key = conn_data->hmac_key;
697 id_entry->hmac_key_len = conn_data->hmac_key_len;
698 sock->user_data = (void *)id_entry;
699 sock->type = SILC_SOCKET_TYPE_ROUTER;
700 server->id_entry->router = id_entry;
703 /* Free the temporary connection data context from key exchange */
704 silc_free(conn_data);
706 /* Free the protocol object */
707 silc_protocol_free(protocol);
709 silc_buffer_free(ctx->packet);
711 silc_ske_free(ctx->ske);
713 sock->protocol = NULL;
716 /* Accepts new connections to the server. Accepting new connections are
717 done in three parts to make it async. */
719 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
721 SilcServer server = (SilcServer)context;
722 SilcSocketConnection newsocket;
723 SilcServerKEInternalContext *proto_ctx;
726 SILC_LOG_DEBUG(("Accepting new connection"));
728 sock = silc_net_accept_connection(server->sock);
730 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
734 /* Check max connections */
735 if (sock > SILC_SERVER_MAX_CONNECTIONS) {
736 if (server->config->redirect) {
737 /* XXX Redirecting connection to somewhere else now?? */
738 /*silc_server_send_notify("Server is full, trying to redirect..."); */
740 SILC_LOG_ERROR(("Refusing connection, server is full"));
745 /* Set socket options */
746 silc_net_set_socket_nonblock(sock);
747 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
749 /* We don't create a ID yet, since we don't know what type of connection
750 this is yet. But, we do add the connection to the socket table. */
751 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
752 server->sockets[sock] = newsocket;
754 /* XXX This MUST be done async as this will block the entire server
755 process. Either we have to do our own resolver stuff or in the future
756 we can use threads. */
757 /* Perform mandatory name and address lookups for the remote host. */
758 silc_net_check_host_by_sock(sock, &newsocket->hostname, &newsocket->ip);
759 if (!newsocket->ip || !newsocket->hostname) {
760 SILC_LOG_DEBUG(("IP lookup/DNS lookup failed"));
761 SILC_LOG_ERROR(("IP lookup/DNS lookup failed"));
765 SILC_LOG_INFO(("Incoming connection from %s (%s)", newsocket->hostname,
768 /* Allocate internal context for key exchange protocol. This is
769 sent as context for the protocol. */
770 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
771 proto_ctx->server = context;
772 proto_ctx->sock = newsocket;
773 proto_ctx->rng = server->rng;
774 proto_ctx->responder = TRUE;
776 /* Prepare the connection for key exchange protocol. We allocate the
777 protocol but will not start it yet. The connector will be the
778 initiator of the protocol thus we will wait for initiation from
779 there before we start the protocol. */
780 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
781 &newsocket->protocol, proto_ctx,
782 silc_server_accept_new_connection_second);
784 /* Register a timeout task that will be executed if the connector
785 will not start the key exchange protocol within 60 seconds. For
786 now, this is a hard coded limit. After 60 secs the connection will
787 be closed if the key exchange protocol has not been started. */
788 proto_ctx->timeout_task =
789 silc_task_register(server->timeout_queue, newsocket->sock,
790 silc_server_timeout_remote,
795 /* Register the connection for network input and output. This sets
796 that scheduler will listen for incoming packets for this connection
797 and sets that outgoing packets may be sent to this connection as well.
798 However, this doesn't set the scheduler for outgoing traffic, it
799 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
800 later when outgoing data is available. */
801 SILC_REGISTER_CONNECTION_FOR_IO(sock);
804 /* Second part of accepting new connection. Key exchange protocol has been
805 performed and now it is time to do little connection authentication
806 protocol to figure out whether this connection is client or server
807 and whether it has right to access this server (especially server
808 connections needs to be authenticated). */
810 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
812 SilcProtocol protocol = (SilcProtocol)context;
813 SilcServerKEInternalContext *ctx =
814 (SilcServerKEInternalContext *)protocol->context;
815 SilcServer server = (SilcServer)ctx->server;
816 SilcSocketConnection sock = NULL;
817 SilcServerConnAuthInternalContext *proto_ctx;
819 SILC_LOG_DEBUG(("Start"));
821 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
822 /* Error occured during protocol */
823 silc_protocol_free(protocol);
825 silc_buffer_free(ctx->packet);
827 silc_ske_free(ctx->ske);
829 silc_free(ctx->dest_id);
832 sock->protocol = NULL;
833 silc_server_disconnect_remote(server, sock, "Server closed connection: "
834 "Key exchange failed");
838 /* Allocate internal context for the authentication protocol. This
839 is sent as context for the protocol. */
840 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
841 proto_ctx->server = (void *)server;
842 proto_ctx->sock = sock = server->sockets[fd];
843 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
844 proto_ctx->responder = TRUE;
845 proto_ctx->dest_id_type = ctx->dest_id_type;
846 proto_ctx->dest_id = ctx->dest_id;
848 /* Free old protocol as it is finished now */
849 silc_protocol_free(protocol);
851 silc_buffer_free(ctx->packet);
853 sock->protocol = NULL;
855 /* Allocate the authentication protocol. This is allocated here
856 but we won't start it yet. We will be receiving party of this
857 protocol thus we will wait that connecting party will make
859 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
860 &sock->protocol, proto_ctx,
861 silc_server_accept_new_connection_final);
863 /* Register timeout task. If the protocol is not executed inside
864 this timelimit the connection will be terminated. Currently
865 this is 60 seconds and is hard coded limit (XXX). */
866 proto_ctx->timeout_task =
867 silc_task_register(server->timeout_queue, sock->sock,
868 silc_server_timeout_remote,
869 (void *)server, 60, 0,
874 /* Final part of accepting new connection. The connection has now
875 been authenticated and keys has been exchanged. We also know whether
876 this is client or server connection. */
878 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
880 SilcProtocol protocol = (SilcProtocol)context;
881 SilcServerConnAuthInternalContext *ctx =
882 (SilcServerConnAuthInternalContext *)protocol->context;
883 SilcServer server = (SilcServer)ctx->server;
884 SilcSocketConnection sock = ctx->sock;
886 SILC_LOG_DEBUG(("Start"));
888 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
889 /* Error occured during protocol */
890 silc_protocol_free(protocol);
892 silc_buffer_free(ctx->packet);
894 silc_ske_free(ctx->ske);
896 silc_free(ctx->dest_id);
899 sock->protocol = NULL;
900 silc_server_disconnect_remote(server, sock, "Server closed connection: "
901 "Authentication failed");
905 sock->type = ctx->conn_type;
907 case SILC_SOCKET_TYPE_CLIENT:
909 SilcClientEntry client;
910 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
912 SILC_LOG_DEBUG(("Remote host is client"));
913 SILC_LOG_INFO(("Connection from %s (%s) is client", sock->hostname,
916 /* Add the client to the client ID cache. The nickname and Client ID
917 and other information is created after we have received NEW_CLIENT
918 packet from client. */
920 silc_idlist_add_client(server->local_list, NULL, NULL, NULL, NULL,
921 NULL, conn_data->send_key,
922 conn_data->receive_key, conn_data->pkcs,
923 conn_data->hmac, NULL, sock);
925 SILC_LOG_ERROR(("Could not add new client to cache"));
926 silc_free(conn_data);
930 /* Free the temporary connection data context from key exchange */
931 silc_free(conn_data);
933 /* Add to sockets internal pointer for fast referencing */
934 sock->user_data = (void *)client;
937 case SILC_SOCKET_TYPE_SERVER:
938 case SILC_SOCKET_TYPE_ROUTER:
940 SilcServerEntry new_server;
941 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
943 SILC_LOG_DEBUG(("Remote host is %s",
944 sock->type == SILC_SOCKET_TYPE_SERVER ?
945 "server" : "router"));
946 SILC_LOG_INFO(("Connection from %s (%s) is %s", sock->hostname,
947 sock->ip, sock->type == SILC_SOCKET_TYPE_SERVER ?
948 "server" : "router"));
950 /* Add the server into server cache. The server name and Server ID
951 is updated after we have received NEW_SERVER packet from the
954 silc_idlist_add_server(server->local_list, NULL,
955 sock->type == SILC_SOCKET_TYPE_SERVER ?
956 SILC_SERVER : SILC_ROUTER, NULL, NULL,
957 conn_data->send_key, conn_data->receive_key,
958 conn_data->pkcs, conn_data->hmac, NULL, sock);
960 SILC_LOG_ERROR(("Could not add new server to cache"));
961 silc_free(conn_data);
965 new_server->registered = TRUE;
966 new_server->hmac_key = conn_data->hmac_key;
967 new_server->hmac_key_len = conn_data->hmac_key_len;
969 /* Free the temporary connection data context from protocols */
970 silc_free(conn_data);
972 /* Add to sockets internal pointer for fast referencing */
973 sock->user_data = (void *)new_server;
975 /* There is connection to other server now, if it is router then
976 we will have connection to outside world. If we are router but
977 normal server connected to us then we will remain standalone,
978 if we are standlone. */
979 if (server->standalone && sock->type == SILC_SOCKET_TYPE_ROUTER) {
980 SILC_LOG_DEBUG(("We are not standalone server anymore"));
981 server->standalone = FALSE;
989 /* Connection has been fully established now. Everything is ok. */
990 SILC_LOG_DEBUG(("New connection authenticated"));
992 silc_protocol_free(protocol);
994 silc_buffer_free(ctx->packet);
996 silc_ske_free(ctx->ske);
998 silc_free(ctx->dest_id);
1000 sock->protocol = NULL;
1003 /* Internal routine that sends packet or marks packet to be sent. This
1004 is used directly only in special cases. Normal cases should use
1005 silc_server_packet_send. Returns < 0 error. */
1007 static int silc_server_packet_send_real(SilcServer server,
1008 SilcSocketConnection sock,
1013 /* Send the packet */
1014 ret = silc_packet_send(sock, force_send);
1018 /* Mark that there is some outgoing data available for this connection.
1019 This call sets the connection both for input and output (the input
1020 is set always and this call keeps the input setting, actually).
1021 Actual data sending is performed by silc_server_packet_process. */
1022 SILC_SET_CONNECTION_FOR_OUTPUT(sock->sock);
1024 /* Mark to socket that data is pending in outgoing buffer. This flag
1025 is needed if new data is added to the buffer before the earlier
1026 put data is sent to the network. */
1027 SILC_SET_OUTBUF_PENDING(sock);
1033 SilcPacketContext *packetdata;
1035 SilcSocketConnection sock;
1038 } SilcServerInternalPacket;
1040 /* This function is used to read packets from network and send packets to
1041 network. This is usually a generic task. */
1043 SILC_TASK_CALLBACK(silc_server_packet_process)
1045 SilcServer server = (SilcServer)context;
1046 SilcSocketConnection sock = server->sockets[fd];
1047 SilcCipher cipher = NULL;
1048 SilcHmac hmac = NULL;
1051 SILC_LOG_DEBUG(("Processing packet"));
1053 /* Packet sending */
1054 if (type == SILC_TASK_WRITE) {
1055 SILC_LOG_DEBUG(("Writing data to connection"));
1057 if (sock->outbuf->data - sock->outbuf->head)
1058 silc_buffer_push(sock->outbuf,
1059 sock->outbuf->data - sock->outbuf->head);
1061 ret = silc_server_packet_send_real(server, sock, TRUE);
1063 /* If returned -2 could not write to connection now, will do
1068 /* The packet has been sent and now it is time to set the connection
1069 back to only for input. When there is again some outgoing data
1070 available for this connection it will be set for output as well.
1071 This call clears the output setting and sets it only for input. */
1072 SILC_SET_CONNECTION_FOR_INPUT(fd);
1073 SILC_UNSET_OUTBUF_PENDING(sock);
1075 silc_buffer_clear(sock->outbuf);
1079 /* Packet receiving */
1080 if (type == SILC_TASK_READ) {
1081 SILC_LOG_DEBUG(("Reading data from connection"));
1083 /* Read some data from connection */
1084 ret = silc_packet_receive(sock);
1090 SILC_LOG_DEBUG(("Read EOF"));
1092 /* If connection is disconnecting already we will finally
1093 close the connection */
1094 if (SILC_IS_DISCONNECTING(sock)) {
1095 if (sock->user_data)
1096 silc_server_free_sock_user_data(server, sock);
1097 silc_server_close_connection(server, sock);
1101 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1103 if (sock->user_data)
1104 silc_server_free_sock_user_data(server, sock);
1105 silc_server_close_connection(server, sock);
1109 /* If connection is disconnecting or disconnected we will ignore
1111 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1112 SILC_LOG_DEBUG(("Ignoring read data from invalid connection"));
1116 switch (sock->type) {
1117 case SILC_SOCKET_TYPE_CLIENT:
1119 SilcClientEntry clnt = (SilcClientEntry)sock->user_data;
1123 clnt->last_receive = time(NULL);
1125 cipher = clnt->receive_key;
1129 case SILC_SOCKET_TYPE_SERVER:
1130 case SILC_SOCKET_TYPE_ROUTER:
1132 SilcServerEntry srvr = (SilcServerEntry)sock->user_data;
1136 srvr->last_receive = time(NULL);
1138 cipher = srvr->receive_key;
1142 case SILC_SOCKET_TYPE_UNKNOWN:
1144 SilcUnknownEntry conn_data = (SilcUnknownEntry)sock->user_data;
1148 cipher = conn_data->receive_key;
1149 hmac = conn_data->hmac;
1156 /* Process the packet. This will call the parser that will then
1157 decrypt and parse the packet. */
1158 if (!silc_packet_receive_process(sock, cipher, hmac,
1159 silc_server_packet_parse, server)) {
1160 silc_buffer_clear(sock->inbuf);
1166 /* Parses whole packet, received earlier. */
1168 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1170 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1171 SilcServer server = (SilcServer)parse_ctx->context;
1172 SilcSocketConnection sock = parse_ctx->sock;
1173 SilcPacketContext *packet = parse_ctx->packet;
1174 SilcBuffer buffer = packet->buffer;
1177 SILC_LOG_DEBUG(("Start"));
1179 /* Decrypt the received packet */
1180 ret = silc_packet_decrypt(parse_ctx->cipher, parse_ctx->hmac,
1186 /* Parse the packet. Packet type is returned. */
1187 ret = silc_packet_parse(packet);
1189 /* Parse the packet header in special way as this is "special"
1191 ret = silc_packet_parse_special(packet);
1194 if (ret == SILC_PACKET_NONE)
1197 /* Parse the incoming packet type */
1198 silc_server_packet_parse_type(server, sock, packet);
1201 silc_buffer_clear(sock->inbuf);
1203 silc_free(parse_ctx);
1206 /* Parser callback called by silc_packet_receive_process. This merely
1207 registers timeout that will handle the actual parsing whem appropriate. */
1209 void silc_server_packet_parse(SilcPacketParserContext *parser_context)
1211 SilcServer server = (SilcServer)parser_context->context;
1212 SilcSocketConnection sock = parser_context->sock;
1214 switch (sock->type) {
1215 case SILC_SOCKET_TYPE_CLIENT:
1216 case SILC_SOCKET_TYPE_UNKNOWN:
1217 /* Parse the packet with timeout */
1218 silc_task_register(server->timeout_queue, sock->sock,
1219 silc_server_packet_parse_real,
1220 (void *)parser_context, 0, 100000,
1222 SILC_TASK_PRI_NORMAL);
1224 case SILC_SOCKET_TYPE_SERVER:
1225 case SILC_SOCKET_TYPE_ROUTER:
1226 /* Packets from servers are parsed as soon as possible */
1227 silc_task_register(server->timeout_queue, sock->sock,
1228 silc_server_packet_parse_real,
1229 (void *)parser_context, 0, 1,
1231 SILC_TASK_PRI_NORMAL);
1238 /* Parses the packet type and calls what ever routines the packet type
1239 requires. This is done for all incoming packets. */
1241 void silc_server_packet_parse_type(SilcServer server,
1242 SilcSocketConnection sock,
1243 SilcPacketContext *packet)
1245 SilcBuffer buffer = packet->buffer;
1246 SilcPacketType type = packet->type;
1248 SILC_LOG_DEBUG(("Parsing packet type %d", type));
1250 /* Parse the packet type */
1252 case SILC_PACKET_DISCONNECT:
1253 SILC_LOG_DEBUG(("Disconnect packet"));
1255 case SILC_PACKET_SUCCESS:
1257 * Success received for something. For now we can have only
1258 * one protocol for connection executing at once hence this
1259 * success message is for whatever protocol is executing currently.
1261 SILC_LOG_DEBUG(("Success packet"));
1262 if (sock->protocol) {
1263 sock->protocol->execute(server->timeout_queue, 0,
1264 sock->protocol, sock->sock, 0, 0);
1267 case SILC_PACKET_FAILURE:
1268 SILC_LOG_DEBUG(("Failure packet"));
1270 case SILC_PACKET_REJECT:
1271 SILC_LOG_DEBUG(("Reject packet"));
1278 case SILC_PACKET_CHANNEL_MESSAGE:
1280 * Received channel message. Channel messages are special packets
1281 * (although probably most common ones) hence they are handled
1284 SILC_LOG_DEBUG(("Channel Message packet"));
1285 silc_server_channel_message(server, sock, packet);
1288 case SILC_PACKET_CHANNEL_KEY:
1290 * Received key for channel. As channels are created by the router
1291 * the keys are as well. We will distribute the key to all of our
1292 * locally connected clients on the particular channel. Router
1293 * never receives this channel and thus is ignored.
1295 SILC_LOG_DEBUG(("Channel Key packet"));
1296 silc_server_channel_key(server, sock, packet);
1302 case SILC_PACKET_COMMAND:
1304 * Recived command. Allocate command context and execute the command.
1306 SILC_LOG_DEBUG(("Command packet"));
1307 silc_server_command_process(server, sock, packet);
1310 case SILC_PACKET_COMMAND_REPLY:
1312 * Received command reply packet. Servers never send commands thus
1313 * they don't receive command reply packets either, except in cases
1314 * where server has forwarded command packet coming from client.
1315 * This must be the case here or we will ignore the packet.
1317 SILC_LOG_DEBUG(("Command Reply packet"));
1318 silc_server_packet_relay_command_reply(server, sock, packet);
1322 * Private Message packets
1324 case SILC_PACKET_PRIVATE_MESSAGE:
1326 * Received private message packet. The packet is coming from either
1329 SILC_LOG_DEBUG(("Private Message packet"));
1330 silc_server_private_message(server, sock, packet);
1333 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1337 * Key Exchange protocol packets
1339 case SILC_PACKET_KEY_EXCHANGE:
1340 SILC_LOG_DEBUG(("KE packet"));
1341 if (sock->protocol && sock->protocol->protocol->type
1342 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1344 SilcServerKEInternalContext *proto_ctx =
1345 (SilcServerKEInternalContext *)sock->protocol->context;
1347 proto_ctx->packet = buffer;
1349 /* Let the protocol handle the packet */
1350 sock->protocol->execute(server->timeout_queue, 0,
1351 sock->protocol, sock->sock, 0, 100000);
1353 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1354 "protocol active, packet dropped."));
1356 /* XXX Trigger KE protocol?? Rekey actually, maybe. */
1360 case SILC_PACKET_KEY_EXCHANGE_1:
1361 SILC_LOG_DEBUG(("KE 1 packet"));
1362 if (sock->protocol && sock->protocol->protocol->type
1363 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1365 SilcServerKEInternalContext *proto_ctx =
1366 (SilcServerKEInternalContext *)sock->protocol->context;
1368 if (proto_ctx->packet)
1369 silc_buffer_free(proto_ctx->packet);
1371 proto_ctx->packet = buffer;
1372 proto_ctx->dest_id_type = packet->src_id_type;
1373 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
1375 /* Let the protocol handle the packet */
1376 sock->protocol->execute(server->timeout_queue, 0,
1377 sock->protocol, sock->sock,
1380 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1381 "protocol active, packet dropped."));
1385 case SILC_PACKET_KEY_EXCHANGE_2:
1386 SILC_LOG_DEBUG(("KE 2 packet"));
1387 if (sock->protocol && sock->protocol->protocol->type
1388 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1390 SilcServerKEInternalContext *proto_ctx =
1391 (SilcServerKEInternalContext *)sock->protocol->context;
1393 if (proto_ctx->packet)
1394 silc_buffer_free(proto_ctx->packet);
1396 proto_ctx->packet = buffer;
1397 proto_ctx->dest_id_type = packet->src_id_type;
1398 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
1400 /* Let the protocol handle the packet */
1401 sock->protocol->execute(server->timeout_queue, 0,
1402 sock->protocol, sock->sock,
1405 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1406 "protocol active, packet dropped."));
1410 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1411 /* If we receive this packet we will send to the other end information
1412 about our mandatory authentication method for the connection.
1413 This packet maybe received at any time. */
1416 * Connection Authentication protocol packets
1418 case SILC_PACKET_CONNECTION_AUTH:
1419 /* Start of the authentication protocol. We receive here the
1420 authentication data and will verify it. */
1421 SILC_LOG_DEBUG(("Connection auth packet"));
1422 if (sock->protocol && sock->protocol->protocol->type
1423 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
1425 SilcServerConnAuthInternalContext *proto_ctx =
1426 (SilcServerConnAuthInternalContext *)sock->protocol->context;
1428 proto_ctx->packet = buffer;
1430 /* Let the protocol handle the packet */
1431 sock->protocol->execute(server->timeout_queue, 0,
1432 sock->protocol, sock->sock, 0, 0);
1434 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
1435 "protocol active, packet dropped."));
1439 case SILC_PACKET_NEW_ID:
1441 * Received New ID packet. This includes some new ID that has been
1442 * created. It may be for client, server or channel. This is the way
1443 * to distribute information about new registered entities in the
1446 SILC_LOG_DEBUG(("New ID packet"));
1447 silc_server_new_id(server, sock, packet);
1450 case SILC_PACKET_NEW_CLIENT:
1452 * Received new client packet. This includes client information that
1453 * we will use to create initial client ID. After creating new
1454 * ID we will send it to the client.
1456 SILC_LOG_DEBUG(("New Client packet"));
1457 silc_server_new_client(server, sock, packet);
1460 case SILC_PACKET_NEW_SERVER:
1462 * Received new server packet. This includes Server ID and some other
1463 * information that we may save. This is received after server has
1466 SILC_LOG_DEBUG(("New Server packet"));
1467 silc_server_new_server(server, sock, packet);
1470 case SILC_PACKET_NEW_CHANNEL:
1473 case SILC_PACKET_NEW_CHANNEL_USER:
1476 case SILC_PACKET_NEW_CHANNEL_LIST:
1479 case SILC_PACKET_NEW_CHANNEL_USER_LIST:
1482 case SILC_PACKET_REPLACE_ID:
1484 * Received replace ID packet. This sends the old ID that is to be
1485 * replaced with the new one included into the packet. Client must not
1488 SILC_LOG_DEBUG(("Replace ID packet"));
1489 silc_server_replace_id(server, sock, packet);
1492 case SILC_PACKET_REMOVE_ID:
1495 case SILC_PACKET_REMOVE_CHANNEL_USER:
1497 * Received packet to remove user from a channel. Routers notify other
1498 * routers about a user leaving a channel.
1500 SILC_LOG_DEBUG(("Remove Channel User packet"));
1501 silc_server_remove_channel_user(server, sock, packet);
1505 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
1511 /* Assembles a new packet to be sent out to network. This doesn't actually
1512 send the packet but creates the packet and fills the outgoing data
1513 buffer and marks the packet ready to be sent to network. However, If
1514 argument force_send is TRUE the packet is sent immediately and not put
1515 to queue. Normal case is that the packet is not sent immediately. */
1517 void silc_server_packet_send(SilcServer server,
1518 SilcSocketConnection sock,
1519 SilcPacketType type,
1520 SilcPacketFlags flags,
1521 unsigned char *data,
1522 unsigned int data_len,
1525 void *dst_id = NULL;
1526 SilcIdType dst_id_type = SILC_ID_NONE;
1531 /* Get data used in the packet sending, keys and stuff */
1532 switch(sock->type) {
1533 case SILC_SOCKET_TYPE_CLIENT:
1534 if (((SilcClientEntry)sock->user_data)->id) {
1535 dst_id = ((SilcClientEntry)sock->user_data)->id;
1536 dst_id_type = SILC_ID_CLIENT;
1539 case SILC_SOCKET_TYPE_SERVER:
1540 case SILC_SOCKET_TYPE_ROUTER:
1541 if (((SilcServerEntry)sock->user_data)->id) {
1542 dst_id = ((SilcServerEntry)sock->user_data)->id;
1543 dst_id_type = SILC_ID_SERVER;
1550 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
1551 dst_id_type, data, data_len, force_send);
1554 /* Assembles a new packet to be sent out to network. This doesn't actually
1555 send the packet but creates the packet and fills the outgoing data
1556 buffer and marks the packet ready to be sent to network. However, If
1557 argument force_send is TRUE the packet is sent immediately and not put
1558 to queue. Normal case is that the packet is not sent immediately.
1559 Destination information is sent as argument for this function. */
1561 void silc_server_packet_send_dest(SilcServer server,
1562 SilcSocketConnection sock,
1563 SilcPacketType type,
1564 SilcPacketFlags flags,
1566 SilcIdType dst_id_type,
1567 unsigned char *data,
1568 unsigned int data_len,
1571 SilcPacketContext packetdata;
1572 SilcCipher cipher = NULL;
1573 SilcHmac hmac = NULL;
1574 unsigned char *dst_id_data = NULL;
1575 unsigned int dst_id_len = 0;
1577 SILC_LOG_DEBUG(("Sending packet, type %d", type));
1579 /* Get data used in the packet sending, keys and stuff */
1580 switch(sock->type) {
1581 case SILC_SOCKET_TYPE_CLIENT:
1582 if (sock->user_data) {
1583 cipher = ((SilcClientEntry)sock->user_data)->send_key;
1584 hmac = ((SilcClientEntry)sock->user_data)->hmac;
1587 case SILC_SOCKET_TYPE_SERVER:
1588 case SILC_SOCKET_TYPE_ROUTER:
1589 if (sock->user_data) {
1590 cipher = ((SilcServerEntry)sock->user_data)->send_key;
1591 hmac = ((SilcServerEntry)sock->user_data)->hmac;
1595 if (sock->user_data) {
1596 /* We don't know what type of connection this is thus it must
1597 be in authentication phase. */
1598 cipher = ((SilcUnknownEntry)sock->user_data)->send_key;
1599 hmac = ((SilcUnknownEntry)sock->user_data)->hmac;
1605 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
1606 dst_id_len = silc_id_get_len(dst_id_type);
1609 /* Set the packet context pointers */
1610 packetdata.type = type;
1611 packetdata.flags = flags;
1612 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
1613 packetdata.src_id_len = SILC_ID_SERVER_LEN;
1614 packetdata.src_id_type = server->id_type;
1615 packetdata.dst_id = dst_id_data;
1616 packetdata.dst_id_len = dst_id_len;
1617 packetdata.dst_id_type = dst_id_type;
1618 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1619 packetdata.src_id_len + dst_id_len;
1620 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1621 packetdata.rng = server->rng;
1623 /* Prepare outgoing data buffer for packet sending */
1624 silc_packet_send_prepare(sock,
1625 SILC_PACKET_HEADER_LEN +
1626 packetdata.src_id_len +
1627 packetdata.dst_id_len,
1631 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
1633 packetdata.buffer = sock->outbuf;
1635 /* Put the data to the buffer */
1636 if (data && data_len)
1637 silc_buffer_put(sock->outbuf, data, data_len);
1639 /* Create the outgoing packet */
1640 silc_packet_assemble(&packetdata);
1642 /* Encrypt the packet */
1644 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
1646 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
1647 sock->outbuf->data, sock->outbuf->len);
1649 /* Now actually send the packet */
1650 silc_server_packet_send_real(server, sock, force_send);
1652 if (packetdata.src_id)
1653 silc_free(packetdata.src_id);
1654 if (packetdata.dst_id)
1655 silc_free(packetdata.dst_id);
1658 /* Forwards packet. Packets sent with this function will be marked as
1659 forwarded (in the SILC header flags) so that the receiver knows that
1660 we have forwarded the packet to it. Forwarded packets are handled
1661 specially by the receiver as they are not destined to the receiver
1662 originally. However, the receiver knows this because the forwarded
1663 flag has been set (and the flag is authenticated). */
1665 void silc_server_packet_forward(SilcServer server,
1666 SilcSocketConnection sock,
1667 unsigned char *data, unsigned int data_len,
1670 SilcCipher cipher = NULL;
1671 SilcHmac hmac = NULL;
1673 SILC_LOG_DEBUG(("Forwarding packet"));
1675 /* Get data used in the packet sending, keys and stuff */
1676 switch(sock->type) {
1677 case SILC_SOCKET_TYPE_CLIENT:
1678 if (sock->user_data) {
1679 cipher = ((SilcClientEntry )sock->user_data)->send_key;
1680 hmac = ((SilcClientEntry )sock->user_data)->hmac;
1683 case SILC_SOCKET_TYPE_SERVER:
1684 case SILC_SOCKET_TYPE_ROUTER:
1685 if (sock->user_data) {
1686 cipher = ((SilcServerEntry )sock->user_data)->send_key;
1687 hmac = ((SilcServerEntry )sock->user_data)->hmac;
1691 /* We won't forward to unknown destination - keys must exist with
1692 the destination before forwarding. */
1696 /* Prepare outgoing data buffer for packet sending */
1697 silc_packet_send_prepare(sock, 0, 0, data_len);
1699 /* Mungle the packet flags and add the FORWARDED flag */
1701 data[2] |= (unsigned char)SILC_PACKET_FLAG_FORWARDED;
1703 /* Put the data to the buffer */
1704 if (data && data_len)
1705 silc_buffer_put(sock->outbuf, data, data_len);
1707 /* Encrypt the packet */
1709 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
1711 SILC_LOG_HEXDUMP(("Forwarded packet, len %d", sock->outbuf->len),
1712 sock->outbuf->data, sock->outbuf->len);
1714 /* Now actually send the packet */
1715 silc_server_packet_send_real(server, sock, force_send);
1718 /* Internal routine to actually create the channel packet and send it
1719 to network. This is common function in channel message sending. */
1722 silc_server_packet_send_to_channel_real(SilcServer server,
1723 SilcSocketConnection sock,
1724 SilcPacketContext *packet,
1727 unsigned char *data,
1728 unsigned int data_len,
1729 int channel_message,
1732 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
1733 packet->src_id_len + packet->dst_id_len;
1735 /* Prepare outgoing data buffer for packet sending */
1736 silc_packet_send_prepare(sock,
1737 SILC_PACKET_HEADER_LEN +
1738 packet->src_id_len +
1743 packet->buffer = sock->outbuf;
1745 /* Put the data to buffer, assemble and encrypt the packet. The packet
1746 is encrypted with normal session key shared with the client. */
1747 silc_buffer_put(sock->outbuf, data, data_len);
1748 silc_packet_assemble(packet);
1749 if (channel_message)
1750 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
1751 packet->src_id_len + packet->dst_id_len +
1754 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
1756 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
1757 sock->outbuf->data, sock->outbuf->len);
1759 /* Now actually send the packet */
1760 silc_server_packet_send_real(server, sock, force_send);
1763 /* This routine is used by the server to send packets to channel. The
1764 packet sent with this function is distributed to all clients on
1765 the channel. Usually this is used to send notify messages to the
1766 channel, things like notify about new user joining to the channel. */
1768 void silc_server_packet_send_to_channel(SilcServer server,
1769 SilcChannelEntry channel,
1770 SilcPacketType type,
1771 unsigned char *data,
1772 unsigned int data_len,
1776 SilcSocketConnection sock = NULL;
1777 SilcPacketContext packetdata;
1778 SilcClientEntry client = NULL;
1779 SilcServerEntry *routed = NULL;
1780 unsigned int routed_count = 0;
1784 /* This doesn't send channel message packets */
1785 if (type == SILC_PACKET_CHANNEL_MESSAGE)
1788 SILC_LOG_DEBUG(("Sending packet to channel"));
1790 /* Set the packet context pointers. */
1791 packetdata.flags = 0;
1792 packetdata.type = type;
1793 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1794 packetdata.src_id_len = SILC_ID_SERVER_LEN;
1795 packetdata.src_id_type = SILC_ID_SERVER;
1796 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1797 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
1798 packetdata.dst_id_type = SILC_ID_CHANNEL;
1799 packetdata.rng = server->rng;
1800 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1801 packetdata.src_id_len + packetdata.dst_id_len;
1802 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1804 /* If there are global users in the channel we will send the message
1805 first to our router for further routing. */
1806 if (server->server_type == SILC_SERVER && !server->standalone &&
1807 channel->global_users) {
1808 SilcServerEntry router;
1810 /* Get data used in packet header encryption, keys and stuff. */
1811 router = server->id_entry->router;
1812 sock = (SilcSocketConnection)router->connection;
1813 cipher = router->send_key;
1814 hmac = router->hmac;
1816 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
1818 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1820 data_len, FALSE, force_send);
1823 /* Send the message to clients on the channel's client list. */
1824 for (i = 0; i < channel->user_list_count; i++) {
1825 client = channel->user_list[i].client;
1827 /* If client has router set it is not locally connected client and
1828 we will route the message to the router set in the client. */
1829 if (client && client->router && server->server_type == SILC_ROUTER) {
1832 /* Check if we have sent the packet to this route already */
1833 for (k = 0; k < routed_count; k++)
1834 if (routed[k] == client->router)
1837 /* Get data used in packet header encryption, keys and stuff. */
1838 sock = (SilcSocketConnection)client->router->connection;
1839 cipher = client->router->send_key;
1840 hmac = client->router->hmac;
1842 /* Send the packet */
1843 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1845 data_len, FALSE, force_send);
1847 /* We want to make sure that the packet is routed to same router
1848 only once. Mark this route as sent route. */
1850 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1851 routed[k] = client->router;
1857 /* Send to locally connected client */
1860 /* XXX Check client's mode on the channel. */
1862 /* Get data used in packet header encryption, keys and stuff. */
1863 sock = (SilcSocketConnection)client->connection;
1864 cipher = client->send_key;
1865 hmac = client->hmac;
1867 /* Send the packet */
1868 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1870 data_len, FALSE, force_send);
1876 silc_free(packetdata.src_id);
1877 silc_free(packetdata.dst_id);
1880 /* This routine is explicitly used to relay messages to some channel.
1881 Packets sent with this function we have received earlier and are
1882 totally encrypted. This just sends the packet to all clients on
1883 the channel. If the sender of the packet is someone on the channel
1884 the message will not be sent to that client. The SILC Packet header
1885 is encrypted with the session key shared between us and the client.
1886 MAC is also computed before encrypting the header. Rest of the
1887 packet will be untouched. */
1889 void silc_server_packet_relay_to_channel(SilcServer server,
1890 SilcSocketConnection sender_sock,
1891 SilcChannelEntry channel,
1893 SilcIdType sender_type,
1894 unsigned char *data,
1895 unsigned int data_len,
1898 int i, found = FALSE;
1899 SilcSocketConnection sock = NULL;
1900 SilcPacketContext packetdata;
1901 SilcClientEntry client = NULL;
1902 SilcServerEntry *routed = NULL;
1903 unsigned int routed_count = 0;
1907 SILC_LOG_DEBUG(("Relaying packet to channel"));
1909 /* Set the packet context pointers. */
1910 packetdata.flags = 0;
1911 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
1912 packetdata.src_id = silc_id_id2str(sender, sender_type);
1913 packetdata.src_id_len = silc_id_get_len(sender_type);
1914 packetdata.src_id_type = sender_type;
1915 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1916 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
1917 packetdata.dst_id_type = SILC_ID_CHANNEL;
1918 packetdata.rng = server->rng;
1919 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
1920 packetdata.src_id_len +
1921 packetdata.dst_id_len));
1923 /* If there are global users in the channel we will send the message
1924 first to our router for further routing. */
1925 if (server->server_type == SILC_SERVER && !server->standalone &&
1926 channel->global_users) {
1927 SilcServerEntry router;
1929 router = server->id_entry->router;
1931 /* Check that the sender is not our router. */
1932 if (sender_sock != (SilcSocketConnection)router->connection) {
1934 /* Get data used in packet header encryption, keys and stuff. */
1935 sock = (SilcSocketConnection)router->connection;
1936 cipher = router->send_key;
1937 hmac = router->hmac;
1939 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
1941 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1943 data_len, TRUE, force_send);
1947 /* Send the message to clients on the channel's client list. */
1948 for (i = 0; i < channel->user_list_count; i++) {
1949 client = channel->user_list[i].client;
1953 /* If sender is one on the channel do not send it the packet. */
1954 if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
1959 /* If the client has set router it means that it is not locally
1960 connected client and we will route the packet further. */
1961 if (server->server_type == SILC_ROUTER && client->router) {
1964 /* Sender maybe server as well so we want to make sure that
1965 we won't send the message to the server it came from. */
1966 if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
1971 /* Check if we have sent the packet to this route already */
1972 for (k = 0; k < routed_count; k++)
1973 if (routed[k] == client->router)
1976 /* Get data used in packet header encryption, keys and stuff. */
1977 sock = (SilcSocketConnection)client->router->connection;
1978 cipher = client->router->send_key;
1979 hmac = client->router->hmac;
1981 /* Send the packet */
1982 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1984 data_len, TRUE, force_send);
1986 /* We want to make sure that the packet is routed to same router
1987 only once. Mark this route as sent route. */
1989 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1990 routed[k] = client->router;
1996 /* XXX Check client's mode on the channel. */
1998 /* Get data used in packet header encryption, keys and stuff. */
1999 sock = (SilcSocketConnection)client->connection;
2000 cipher = client->send_key;
2001 hmac = client->hmac;
2003 SILC_LOG_DEBUG(("Sending packet to client %s",
2004 sock->hostname ? sock->hostname : sock->ip));
2006 /* Send the packet */
2007 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
2009 data_len, TRUE, force_send);
2013 silc_free(packetdata.src_id);
2014 silc_free(packetdata.dst_id);
2017 /* This function is used to send packets strictly to all local clients
2018 on a particular channel. This is used for example to distribute new
2019 channel key to all our locally connected clients on the channel.
2020 The packets are always encrypted with the session key shared between
2023 void silc_server_packet_send_local_channel(SilcServer server,
2024 SilcChannelEntry channel,
2025 SilcPacketType type,
2026 SilcPacketFlags flags,
2027 unsigned char *data,
2028 unsigned int data_len,
2032 SilcClientEntry client;
2033 SilcSocketConnection sock = NULL;
2035 SILC_LOG_DEBUG(("Start"));
2037 /* Send the message to clients on the channel's client list. */
2038 for (i = 0; i < channel->user_list_count; i++) {
2039 client = channel->user_list[i].client;
2042 sock = (SilcSocketConnection)client->connection;
2044 /* Send the packet to the client */
2045 silc_server_packet_send_dest(server, sock, type, flags, client->id,
2046 SILC_ID_CLIENT, data, data_len,
2052 /* Relays received command reply packet to the correct destination. The
2053 destination must be one of our locally connected client or the packet
2054 will be ignored. This is called when server has forwarded one of
2055 client's command request to router and router has now replied to the
2058 void silc_server_packet_relay_command_reply(SilcServer server,
2059 SilcSocketConnection sock,
2060 SilcPacketContext *packet)
2062 SilcBuffer buffer = packet->buffer;
2063 SilcClientEntry client;
2065 SilcSocketConnection dst_sock;
2067 SILC_LOG_DEBUG(("Start"));
2069 /* Source must be server or router */
2070 if (packet->src_id_type != SILC_ID_SERVER &&
2071 sock->type != SILC_SOCKET_TYPE_ROUTER)
2074 /* Destination must be client */
2075 if (packet->dst_id_type != SILC_ID_CLIENT)
2078 /* Execute command reply locally for the command */
2079 silc_server_command_reply_process(server, sock, buffer);
2081 id = silc_id_str2id(packet->dst_id, SILC_ID_CLIENT);
2083 /* Destination must be one of ours */
2084 client = silc_idlist_find_client_by_id(server->local_list, id);
2090 /* Relay the packet to the client */
2092 dst_sock = (SilcSocketConnection)client->connection;
2093 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2094 + packet->dst_id_len + packet->padlen);
2096 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2097 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2099 /* Encrypt packet */
2100 if (client && client->send_key)
2101 silc_packet_encrypt(client->send_key, client->hmac,
2102 dst_sock->outbuf, buffer->len);
2104 /* Send the packet */
2105 silc_server_packet_send_real(server, dst_sock, FALSE);
2110 silc_buffer_free(buffer);
2113 /* Closes connection to socket connection */
2115 void silc_server_close_connection(SilcServer server,
2116 SilcSocketConnection sock)
2119 SILC_LOG_DEBUG(("Closing connection %d", sock->sock));
2121 /* We won't listen for this connection anymore */
2122 silc_schedule_unset_listen_fd(sock->sock);
2124 /* Unregister all tasks */
2125 silc_task_unregister_by_fd(server->io_queue, sock->sock);
2126 silc_task_unregister_by_fd(server->timeout_queue, sock->sock);
2128 /* Close the actual connection */
2129 silc_net_close_connection(sock->sock);
2130 server->sockets[sock->sock] = NULL;
2131 silc_socket_free(sock);
2134 /* Sends disconnect message to remote connection and disconnects the
2137 void silc_server_disconnect_remote(SilcServer server,
2138 SilcSocketConnection sock,
2139 const char *fmt, ...)
2142 unsigned char buf[4096];
2144 memset(buf, 0, sizeof(buf));
2146 vsprintf(buf, fmt, ap);
2149 SILC_LOG_DEBUG(("Disconnecting remote host"));
2151 /* Notify remote end that the conversation is over. The notify message
2152 is tried to be sent immediately. */
2153 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2154 buf, strlen(buf), TRUE);
2156 /* Mark the connection to be disconnected */
2157 SILC_SET_DISCONNECTED(sock);
2158 silc_server_close_connection(server, sock);
2161 /* Free's user_data pointer from socket connection object. As this
2162 pointer maybe anything we wil switch here to find the correct
2163 data type and free it the way it needs to be free'd. */
2165 void silc_server_free_sock_user_data(SilcServer server,
2166 SilcSocketConnection sock)
2168 SILC_LOG_DEBUG(("Start"));
2170 switch(sock->type) {
2171 case SILC_SOCKET_TYPE_CLIENT:
2173 SilcClientEntry user_data = (SilcClientEntry )sock->user_data;
2175 /* Remove client from all channels */
2176 silc_server_remove_from_channels(server, sock, user_data);
2178 /* Free the client entry and everything in it */
2179 /* XXX must take some info to history before freeing */
2180 silc_idlist_del_client(server->local_list, user_data);
2183 case SILC_SOCKET_TYPE_SERVER:
2184 case SILC_SOCKET_TYPE_ROUTER:
2192 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2194 if (user_data->send_key)
2195 silc_cipher_free(user_data->send_key);
2196 if (user_data->receive_key)
2197 silc_cipher_free(user_data->receive_key);
2198 if (user_data->pkcs)
2199 silc_pkcs_free(user_data->pkcs);
2200 if (user_data->hmac) {
2201 silc_hmac_free(user_data->hmac);
2202 memset(user_data->hmac_key, 0, user_data->hmac_key_len);
2203 silc_free(user_data->hmac_key);
2205 silc_free(user_data);
2210 sock->user_data = NULL;
2215 /* Removes client from all channels it has joined. This is used when
2216 client connection is disconnected. If the client on a channel
2217 is last, the channel is removed as well. */
2219 void silc_server_remove_from_channels(SilcServer server,
2220 SilcSocketConnection sock,
2221 SilcClientEntry client)
2224 SilcChannelEntry channel;
2226 /* Remove the client from all channels. The client is removed from
2227 the channels' user list. */
2228 for (i = 0; i < client->channel_count; i++) {
2229 channel = client->channel[i];
2233 /* Remove from channel */
2234 for (k = 0; k < channel->user_list_count; k++) {
2235 if (channel->user_list[k].client == client) {
2237 /* If this client is last one on the channel the channel
2238 is removed all together. */
2239 if (channel->user_list_count == 1) {
2241 /* However, if the channel has marked global users then the
2242 channel is not created locally, and this does not remove the
2243 channel globally from SILC network, in this case we will
2244 notify that this client has left the channel. */
2245 if (channel->global_users)
2246 silc_server_send_notify_to_channel(server, channel,
2247 SILC_NOTIFY_TYPE_SIGNOFF,
2251 sock->hostname : sock->ip);
2253 silc_idlist_del_channel(server->local_list, channel);
2257 channel->user_list[k].client = NULL;
2258 channel->user_list[k].mode = SILC_CHANNEL_UMODE_NONE;
2260 /* Send notify to channel about client leaving SILC and thus
2261 the entire channel. */
2262 silc_server_send_notify_to_channel(server, channel,
2263 SILC_NOTIFY_TYPE_SIGNOFF,
2267 sock->hostname : sock->ip);
2272 if (client->channel_count)
2273 silc_free(client->channel);
2274 client->channel = NULL;
2277 /* Removes client from one channel. This is used for example when client
2278 calls LEAVE command to remove itself from the channel. Returns TRUE
2279 if channel still exists and FALSE if the channel is removed when
2280 last client leaves the channel. If `notify' is FALSE notify messages
2283 int silc_server_remove_from_one_channel(SilcServer server,
2284 SilcSocketConnection sock,
2285 SilcChannelEntry channel,
2286 SilcClientEntry client,
2290 SilcChannelEntry ch;
2292 /* Remove the client from the channel. The client is removed from
2293 the channel's user list. */
2294 for (i = 0; i < client->channel_count; i++) {
2295 ch = client->channel[i];
2296 if (!ch || ch != channel)
2300 client->channel[i] = NULL;
2302 /* Remove from channel */
2303 for (k = 0; k < channel->user_list_count; k++) {
2304 if (channel->user_list[k].client == client) {
2306 /* If this client is last one on the channel the channel
2307 is removed all together. */
2308 if (channel->user_list_count == 1) {
2309 /* Notify about leaving client if this channel has global users,
2310 ie. the channel is not created locally. */
2311 if (notify && channel->global_users)
2312 silc_server_send_notify_to_channel(server, channel,
2313 SILC_NOTIFY_TYPE_LEAVE,
2314 "%s@%s has left channel %s",
2317 sock->hostname : sock->ip,
2318 channel->channel_name);
2320 silc_idlist_del_channel(server->local_list, channel);
2324 channel->user_list[k].client = NULL;
2325 channel->user_list[k].mode = SILC_CHANNEL_UMODE_NONE;
2327 /* Send notify to channel about client leaving the channel */
2329 silc_server_send_notify_to_channel(server, channel,
2330 SILC_NOTIFY_TYPE_LEAVE,
2331 "%s@%s has left channel %s",
2332 client->nickname, sock->hostname ?
2333 sock->hostname : sock->ip,
2334 channel->channel_name);
2342 /* Returns TRUE if the given client is on the channel. FALSE if not.
2343 This works because we assure that the user list on the channel is
2344 always in up to date thus we can only check the channel list from
2345 `client' which is faster than checking the user list from `channel'. */
2346 /* XXX This really is utility function and should be in eg. serverutil.c */
2348 int silc_server_client_on_channel(SilcClientEntry client,
2349 SilcChannelEntry channel)
2353 if (!client || !channel)
2356 for (i = 0; i < client->channel_count; i++) {
2357 if (client->channel[i] == channel)
2364 /* Timeout callback. This is called if connection is idle or for some
2365 other reason is not responding within some period of time. This
2366 disconnects the remote end. */
2368 SILC_TASK_CALLBACK(silc_server_timeout_remote)
2370 SilcServer server = (SilcServer)context;
2371 SilcSocketConnection sock = server->sockets[fd];
2373 silc_server_disconnect_remote(server, sock,
2374 "Server closed connection: "
2375 "Connection timeout");
2378 /* Internal routine used to send (relay, route) private messages to some
2379 destination. If the private message key does not exist then the message
2380 is re-encrypted, otherwise we just pass it along. */
2383 silc_server_private_message_send_internal(SilcServer server,
2384 SilcSocketConnection dst_sock,
2387 SilcPacketContext *packet)
2389 SilcBuffer buffer = packet->buffer;
2391 /* Send and re-encrypt if private messge key does not exist */
2392 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
2394 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2395 + packet->dst_id_len + packet->padlen);
2396 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2397 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2399 /* Re-encrypt packet */
2400 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
2402 /* Send the packet */
2403 silc_server_packet_send_real(server, dst_sock, FALSE);
2406 /* Key exist so just send it */
2407 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2408 + packet->dst_id_len + packet->padlen);
2409 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
2410 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2411 silc_server_packet_send_real(server, dst_sock, FALSE);
2415 /* Received private message. This resolves the destination of the message
2416 and sends the packet. This is used by both server and router. If the
2417 destination is our locally connected client this sends the packet to
2418 the client. This may also send the message for further routing if
2419 the destination is not in our server (or router). */
2421 void silc_server_private_message(SilcServer server,
2422 SilcSocketConnection sock,
2423 SilcPacketContext *packet)
2425 SilcBuffer buffer = packet->buffer;
2427 SilcServerEntry router;
2428 SilcSocketConnection dst_sock;
2429 SilcClientEntry client;
2431 SILC_LOG_DEBUG(("Start"));
2433 if (!packet->dst_id) {
2434 SILC_LOG_ERROR(("Bad Client ID in private message packet, dropped"));
2438 /* Decode destination Client ID */
2439 id = silc_id_str2id(packet->dst_id, SILC_ID_CLIENT);
2441 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
2445 /* If the destination belongs to our server we don't have to route
2446 the message anywhere but to send it to the local destination. */
2447 client = silc_idlist_find_client_by_id(server->local_list, id);
2449 /* It exists, now deliver the message to the destination */
2450 dst_sock = (SilcSocketConnection)client->connection;
2452 /* If we are router and the client has router then the client is in
2453 our cell but not directly connected to us. */
2454 if (server->server_type == SILC_ROUTER && client->router) {
2455 /* We are of course in this case the client's router thus the real
2456 "router" of the client is the server who owns the client. Thus
2457 we will send the packet to that server. */
2458 router = (SilcServerEntry)dst_sock->user_data;
2459 // assert(client->router == server->id_entry);
2461 silc_server_private_message_send_internal(server, dst_sock,
2468 /* Seems that client really is directly connected to us */
2469 silc_server_private_message_send_internal(server, dst_sock,
2471 client->hmac, packet);
2475 /* Destination belongs to someone not in this server. If we are normal
2476 server our action is to send the packet to our router. */
2477 if (server->server_type == SILC_SERVER && !server->standalone) {
2478 router = server->id_entry->router;
2480 /* Send to primary route */
2482 dst_sock = (SilcSocketConnection)router->connection;
2483 silc_server_private_message_send_internal(server, dst_sock,
2485 router->hmac, packet);
2490 /* We are router and we will perform route lookup for the destination
2491 and send the message to fastest route. */
2492 if (server->server_type == SILC_ROUTER && !server->standalone) {
2493 dst_sock = silc_server_get_route(server, id, SILC_ID_CLIENT);
2494 router = (SilcServerEntry)dst_sock->user_data;
2496 /* Get fastest route and send packet. */
2498 silc_server_private_message_send_internal(server, dst_sock,
2500 router->hmac, packet);
2506 silc_server_send_error(server, sock,
2507 "No such nickname: Private message not sent");
2509 silc_buffer_free(buffer);
2512 /* Process received channel message. The message can be originated from
2513 client or server. */
2515 void silc_server_channel_message(SilcServer server,
2516 SilcSocketConnection sock,
2517 SilcPacketContext *packet)
2519 SilcChannelEntry channel = NULL;
2520 SilcClientEntry client = NULL;
2521 SilcChannelID *id = NULL;
2522 void *sender = NULL;
2523 SilcBuffer buffer = packet->buffer;
2526 SILC_LOG_DEBUG(("Processing channel message"));
2529 if (packet->dst_id_type != SILC_ID_CHANNEL) {
2530 SILC_LOG_ERROR(("Received bad message for channel, dropped"));
2531 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
2535 /* Find channel entry */
2536 id = silc_id_str2id(packet->dst_id, SILC_ID_CHANNEL);
2537 channel = silc_idlist_find_channel_by_id(server->local_list, id);
2539 SILC_LOG_DEBUG(("Could not find channel"));
2543 /* See that this client is on the channel. If the message is coming
2544 from router we won't do the check as the message is from client that
2545 we don't know about. Also, if the original sender is not client
2546 (as it can be server as well) we don't do the check. */
2547 sender = silc_id_str2id(packet->src_id, packet->src_id_type);
2548 if (sock->type != SILC_SOCKET_TYPE_ROUTER &&
2549 packet->src_id_type == SILC_ID_CLIENT) {
2550 for (i = 0; i < channel->user_list_count; i++) {
2551 client = channel->user_list[i].client;
2552 if (client && !SILC_ID_CLIENT_COMPARE(client->id, sender))
2555 if (i >= channel->user_list_count)
2559 /* Distribute the packet to our local clients. This will send the
2560 packet for further routing as well, if needed. */
2561 silc_server_packet_relay_to_channel(server, sock, channel, sender,
2562 packet->src_id_type,
2563 packet->buffer->data,
2564 packet->buffer->len, FALSE);
2571 silc_buffer_free(buffer);
2574 /* Received channel key packet. We distribute the key to all of our locally
2575 connected clients on the channel. */
2576 /* XXX Router must accept this packet and distribute the key to all its
2577 server that has clients on the channel */
2579 void silc_server_channel_key(SilcServer server,
2580 SilcSocketConnection sock,
2581 SilcPacketContext *packet)
2583 SilcBuffer buffer = packet->buffer;
2584 SilcChannelKeyPayload payload = NULL;
2585 SilcChannelID *id = NULL;
2586 SilcChannelEntry channel;
2587 SilcClientEntry client;
2589 unsigned int key_len;
2593 if (packet->src_id_type != SILC_ID_SERVER &&
2594 sock->type != SILC_SOCKET_TYPE_ROUTER)
2597 /* Decode channel key payload */
2598 payload = silc_channel_key_parse_payload(buffer);
2600 SILC_LOG_ERROR(("Bad channel key payload, dropped"));
2604 /* Get channel ID */
2605 id = silc_id_str2id(silc_channel_key_get_id(payload, NULL), SILC_ID_CHANNEL);
2609 /* Get the channel entry */
2610 channel = silc_idlist_find_channel_by_id(server->local_list, id);
2612 SILC_LOG_ERROR(("Received key for non-existent channel"));
2616 /* Save the key for us as well */
2617 key = silc_channel_key_get_key(payload, &key_len);
2620 cipher = silc_channel_key_get_cipher(payload, NULL);;
2623 channel->key_len = key_len * 8;
2624 channel->key = silc_calloc(key_len, sizeof(unsigned char));
2625 memcpy(channel->key, key, key_len);
2626 silc_cipher_alloc(cipher, &channel->channel_key);
2627 channel->channel_key->cipher->set_key(channel->channel_key->context,
2630 /* Distribute the key to all clients on the channel */
2631 for (i = 0; i < channel->user_list_count; i++) {
2632 client = channel->user_list[i].client;
2635 silc_server_packet_send_dest(server, client->connection,
2636 SILC_PACKET_CHANNEL_KEY, 0,
2637 client->id, SILC_ID_CLIENT,
2638 buffer->data, buffer->len, FALSE);
2645 silc_channel_key_free_payload(payload);
2646 silc_buffer_free(buffer);
2649 /* Sends error message. Error messages may or may not have any
2652 void silc_server_send_error(SilcServer server,
2653 SilcSocketConnection sock,
2654 const char *fmt, ...)
2657 unsigned char buf[4096];
2659 memset(buf, 0, sizeof(buf));
2661 vsprintf(buf, fmt, ap);
2664 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
2665 buf, strlen(buf), FALSE);
2668 /* Sends notify message */
2670 void silc_server_send_notify(SilcServer server,
2671 SilcSocketConnection sock,
2672 SilcNotifyType type,
2673 const char *fmt, ...)
2676 unsigned char buf[4096];
2679 memset(buf, 0, sizeof(buf));
2681 vsprintf(buf, fmt, ap);
2684 packet = silc_buffer_alloc(2 + strlen(buf));
2685 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2686 silc_buffer_format(packet,
2687 SILC_STR_UI_SHORT(type),
2688 SILC_STR_UI16_STRING(buf),
2691 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0,
2692 packet->data, packet->len, FALSE);
2693 silc_buffer_free(packet);
2696 /* Sends notify message destined to specific entity. */
2698 void silc_server_send_notify_dest(SilcServer server,
2699 SilcSocketConnection sock,
2701 SilcIdType dest_id_type,
2702 SilcNotifyType type,
2703 const char *fmt, ...)
2706 unsigned char buf[4096];
2709 memset(buf, 0, sizeof(buf));
2711 vsprintf(buf, fmt, ap);
2714 packet = silc_buffer_alloc(2 + strlen(buf));
2715 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2716 silc_buffer_format(packet,
2717 SILC_STR_UI_SHORT(type),
2718 SILC_STR_UI16_STRING(buf),
2721 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
2722 dest_id, dest_id_type,
2723 packet->data, packet->len, FALSE);
2724 silc_buffer_free(packet);
2727 /* Sends notify message to a channel. The notify message sent is
2728 distributed to all clients on the channel. */
2730 void silc_server_send_notify_to_channel(SilcServer server,
2731 SilcChannelEntry channel,
2732 SilcNotifyType type,
2733 const char *fmt, ...)
2736 unsigned char buf[4096];
2739 memset(buf, 0, sizeof(buf));
2741 vsprintf(buf, fmt, ap);
2744 packet = silc_buffer_alloc(2 + strlen(buf));
2745 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2746 silc_buffer_format(packet,
2747 SILC_STR_UI_SHORT(type),
2748 SILC_STR_UI16_STRING(buf),
2751 silc_server_packet_send_to_channel(server, channel,
2753 packet->data, packet->len, FALSE);
2754 silc_buffer_free(packet);
2757 /* Sends New ID Payload to remote end. The packet is used to distribute
2758 information about new registered clients, servers, channel etc. usually
2759 to routers so that they can keep these information up to date.
2760 If the argument `broadcast' is TRUE then the packet is sent as
2761 broadcast packet. */
2763 void silc_server_send_new_id(SilcServer server,
2764 SilcSocketConnection sock,
2766 void *id, SilcIdType id_type,
2767 unsigned int id_len)
2770 unsigned char *id_string;
2772 id_string = silc_id_id2str(id, id_type);
2776 packet = silc_buffer_alloc(2 + 2 + id_len);
2777 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2778 silc_buffer_format(packet,
2779 SILC_STR_UI_SHORT(id_type),
2780 SILC_STR_UI_SHORT(id_len),
2781 SILC_STR_UI_XNSTRING(id_string, id_len),
2784 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
2785 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
2786 packet->data, packet->len, FALSE);
2787 silc_free(id_string);
2788 silc_buffer_free(packet);
2791 /* Sends Replace ID payload to remote end. This is used to replace old
2792 ID with new ID sent in the packet. This is called for example when
2793 user changes nickname and we create new ID for the user. If the
2794 argument `broadcast' is TRUE then the packet is sent as
2795 broadcast packet. */
2796 /* XXX It would be expected that the new id is same type as the old
2799 void silc_server_send_replace_id(SilcServer server,
2800 SilcSocketConnection sock,
2802 void *old_id, SilcIdType old_id_type,
2803 unsigned int old_id_len,
2804 void *new_id, SilcIdType new_id_type,
2805 unsigned int new_id_len)
2811 oid = silc_id_id2str(old_id, old_id_type);
2815 nid = silc_id_id2str(new_id, new_id_type);
2819 packet = silc_buffer_alloc(2 + 2 + 2 + 2 + old_id_len + new_id_len);
2820 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2821 silc_buffer_format(packet,
2822 SILC_STR_UI_SHORT(old_id_type),
2823 SILC_STR_UI_SHORT(old_id_len),
2824 SILC_STR_UI_XNSTRING(oid, old_id_len),
2825 SILC_STR_UI_SHORT(new_id_type),
2826 SILC_STR_UI_SHORT(new_id_len),
2827 SILC_STR_UI_XNSTRING(nid, new_id_len),
2830 silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID,
2831 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
2832 packet->data, packet->len, FALSE);
2835 silc_buffer_free(packet);
2838 /* This function is used to send Remove Channel User payload. This may sent
2839 by server but is usually used only by router to notify other routers that
2840 user has left a channel. Normal server sends this packet to its router
2841 to notify that the router should not hold a record about this client
2842 on a channel anymore. Router distributes it further to other routers. */
2844 void silc_server_send_remove_channel_user(SilcServer server,
2845 SilcSocketConnection sock,
2847 void *client_id, void *channel_id)
2850 unsigned char *clid, *chid;
2852 clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
2856 chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
2860 packet = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN + SILC_ID_CHANNEL_LEN);
2861 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2862 silc_buffer_format(packet,
2863 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
2864 SILC_STR_UI_XNSTRING(clid, SILC_ID_CLIENT_LEN),
2865 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
2866 SILC_STR_UI_XNSTRING(chid, SILC_ID_CHANNEL_LEN),
2869 silc_server_packet_send(server, sock, SILC_PACKET_REMOVE_CHANNEL_USER,
2870 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
2871 packet->data, packet->len, FALSE);
2874 silc_buffer_free(packet);
2877 /* Received packet to replace a ID. This checks that the requested ID
2878 exists and replaces it with the new one. */
2880 void silc_server_replace_id(SilcServer server,
2881 SilcSocketConnection sock,
2882 SilcPacketContext *packet)
2884 SilcBuffer buffer = packet->buffer;
2885 unsigned char *old_id = NULL, *new_id = NULL;
2886 SilcIdType old_id_type, new_id_type;
2887 unsigned short old_id_len, new_id_len;
2888 void *id = NULL, *id2 = NULL;
2890 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2891 packet->src_id_type == SILC_ID_CLIENT)
2894 SILC_LOG_DEBUG(("Replacing ID"));
2896 silc_buffer_unformat(buffer,
2897 SILC_STR_UI_SHORT(&old_id_type),
2898 SILC_STR_UI16_NSTRING_ALLOC(&old_id, &old_id_len),
2899 SILC_STR_UI_SHORT(&new_id_type),
2900 SILC_STR_UI16_NSTRING_ALLOC(&new_id, &new_id_len),
2903 if (old_id_type != new_id_type)
2906 if (old_id_len != silc_id_get_len(old_id_type) ||
2907 new_id_len != silc_id_get_len(new_id_type))
2910 id = silc_id_str2id(old_id, old_id_type);
2914 id2 = silc_id_str2id(new_id, new_id_type);
2918 /* Replace the old ID */
2919 switch(old_id_type) {
2920 case SILC_ID_CLIENT:
2921 if (silc_idlist_replace_client_id(server->local_list, id, id2) == NULL)
2922 if (server->server_type == SILC_ROUTER)
2923 silc_idlist_replace_client_id(server->global_list, id, id2);
2926 case SILC_ID_SERVER:
2927 if (silc_idlist_replace_server_id(server->local_list, id, id2) == NULL)
2928 if (server->server_type == SILC_ROUTER)
2929 silc_idlist_replace_server_id(server->global_list, id, id2);
2932 case SILC_ID_CHANNEL:
2933 /* XXX Hmm... Basically this cannot occur. Channel ID's cannot be
2952 /* Creates new channel. */
2954 SilcChannelEntry silc_server_new_channel(SilcServer server,
2955 SilcServerID *router_id,
2956 char *cipher, char *channel_name)
2958 int i, channel_len, key_len;
2959 SilcChannelID *channel_id;
2960 SilcChannelEntry entry;
2962 unsigned char channel_key[32], *id_string;
2965 SILC_LOG_DEBUG(("Creating new channel"));
2967 /* Create channel key */
2968 for (i = 0; i < 32; i++)
2969 channel_key[i] = silc_rng_get_byte(server->rng);
2976 silc_cipher_alloc(cipher, &key);
2977 key->cipher->set_key(key->context, channel_key, key_len);
2979 /* Create the channel */
2980 silc_id_create_channel_id(router_id, server->rng, &channel_id);
2981 entry = silc_idlist_add_channel(server->local_list, channel_name,
2982 SILC_CHANNEL_MODE_NONE, channel_id,
2988 silc_idcache_add(server->local_list->channels, channel_name,
2989 SILC_ID_CHANNEL, channel_id, (void *)entry, TRUE);
2991 entry->key = silc_calloc(key_len, sizeof(*entry->key));
2992 entry->key_len = key_len * 8;
2993 memcpy(entry->key, channel_key, key_len);
2994 memset(channel_key, 0, sizeof(channel_key));
2996 /* Notify other routers about the new channel. We send the packet
2997 to our primary route. */
2998 if (server->standalone == FALSE) {
2999 channel_len = strlen(channel_name);
3000 id_string = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3001 packet = silc_buffer_alloc(2 + SILC_ID_CHANNEL_LEN);
3003 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
3004 silc_buffer_format(packet,
3005 SILC_STR_UI_SHORT(channel_len),
3006 SILC_STR_UI_XNSTRING(channel_name, channel_len),
3007 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
3008 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CHANNEL_LEN),
3011 /* Send the packet to our router. */
3012 silc_server_packet_send(server, (SilcSocketConnection)
3013 server->id_entry->router->connection,
3014 SILC_PACKET_NEW_CHANNEL_USER, 0,
3015 packet->data, packet->len, TRUE);
3017 silc_free(id_string);
3018 silc_buffer_free(packet);
3024 /* Create new client. This processes incoming NEW_CLIENT packet and creates
3025 Client ID for the client. Client becomes registered after calling this
3028 SilcClientEntry silc_server_new_client(SilcServer server,
3029 SilcSocketConnection sock,
3030 SilcPacketContext *packet)
3032 SilcBuffer buffer = packet->buffer;
3033 SilcClientEntry client;
3034 SilcIDCacheEntry cache;
3035 SilcClientID *client_id;
3037 char *username = NULL, *realname = NULL, *id_string;
3039 SILC_LOG_DEBUG(("Creating new client"));
3041 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
3044 /* Take client entry */
3045 client = (SilcClientEntry)sock->user_data;
3047 /* Fetch the old client cache entry so that we can update it. */
3048 if (!silc_idcache_find_by_context(server->local_list->clients,
3049 sock->user_data, &cache)) {
3050 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
3054 /* Parse incoming packet */
3055 silc_buffer_unformat(buffer,
3056 SILC_STR_UI16_STRING_ALLOC(&username),
3057 SILC_STR_UI16_STRING_ALLOC(&realname),
3060 /* Create Client ID */
3061 silc_id_create_client_id(server->id, server->rng, server->md5hash,
3062 username, &client_id);
3064 /* Update client entry */
3065 client->registered = TRUE;
3066 client->nickname = strdup(username);
3067 client->username = username;
3068 client->userinfo = realname;
3069 client->id = client_id;
3071 /* Update the cache entry */
3072 cache->id = (void *)client_id;
3073 cache->type = SILC_ID_CLIENT;
3074 cache->data = username;
3075 silc_idcache_sort_by_data(server->local_list->clients);
3077 /* Notify our router about new client on the SILC network */
3078 if (!server->standalone)
3079 silc_server_send_new_id(server, (SilcSocketConnection)
3080 server->id_entry->router->connection,
3081 server->server_type == SILC_SERVER ? TRUE : FALSE,
3082 client->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
3084 /* Send the new client ID to the client. */
3085 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
3086 reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
3087 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
3088 silc_buffer_format(reply,
3089 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
3090 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
3091 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
3093 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
3094 reply->data, reply->len, FALSE);
3095 silc_free(id_string);
3096 silc_buffer_free(reply);
3098 /* Send some nice info to the client */
3099 silc_server_send_notify(server, sock,
3100 SILC_NOTIFY_TYPE_NONE,
3101 "Welcome to the SILC Network %s@%s",
3103 sock->hostname ? sock->hostname : sock->ip);
3104 silc_server_send_notify(server, sock,
3105 SILC_NOTIFY_TYPE_NONE,
3106 "Your host is %s, running version %s",
3107 server->config->server_info->server_name,
3109 silc_server_send_notify(server, sock,
3110 SILC_NOTIFY_TYPE_NONE,
3111 "Your connection is secured with %s cipher, "
3112 "key length %d bits",
3113 client->send_key->cipher->name,
3114 client->send_key->cipher->key_len);
3115 silc_server_send_notify(server, sock,
3116 SILC_NOTIFY_TYPE_NONE,
3117 "Your current nickname is %s",
3125 /* Create new server. This processes incoming NEW_SERVER packet and
3126 saves the received Server ID. The server is our locally connected
3127 server thus we save all the information and save it to local list.
3128 This funtion can be used by both normal server and router server.
3129 If normal server uses this it means that its router has connected
3130 to the server. If router uses this it means that one of the cell's
3131 servers is connected to the router. */
3133 SilcServerEntry silc_server_new_server(SilcServer server,
3134 SilcSocketConnection sock,
3135 SilcPacketContext *packet)
3137 SilcBuffer buffer = packet->buffer;
3138 SilcServerEntry new_server;
3139 SilcIDCacheEntry cache;
3140 SilcServerID *server_id;
3141 unsigned char *server_name, *id_string;
3143 SILC_LOG_DEBUG(("Creating new server"));
3145 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
3146 sock->type != SILC_SOCKET_TYPE_ROUTER)
3149 /* Take server entry */
3150 new_server = (SilcServerEntry)sock->user_data;
3152 /* Fetch the old server cache entry so that we can update it. */
3153 if (!silc_idcache_find_by_context(server->local_list->servers,
3154 sock->user_data, &cache)) {
3155 SILC_LOG_ERROR(("Lost server's cache entry - bad thing"));
3159 /* Parse the incoming packet */
3160 silc_buffer_unformat(buffer,
3161 SILC_STR_UI16_STRING_ALLOC(&id_string),
3162 SILC_STR_UI16_STRING_ALLOC(&server_name),
3166 server_id = silc_id_str2id(id_string, SILC_ID_SERVER);
3167 silc_free(id_string);
3169 /* Update client entry */
3170 new_server->registered = TRUE;
3171 new_server->server_name = server_name;
3172 new_server->id = server_id;
3174 /* Update the cache entry */
3175 cache->id = (void *)server_id;
3176 cache->type = SILC_ID_SERVER;
3177 cache->data = server_name;
3178 silc_idcache_sort_by_data(server->local_list->servers);
3180 /* Distribute the information about new server in the SILC network
3181 to our router. If we are normal server we won't send anything
3182 since this connection must be our router connection. */
3183 if (server->server_type == SILC_ROUTER && !server->standalone)
3184 silc_server_send_new_id(server, server->id_entry->router->connection,
3185 TRUE, new_server->id, SILC_ID_SERVER,
3186 SILC_ID_SERVER_LEN);
3191 /* Processes incoming New ID Payload. New ID Payload is used to distribute
3192 information about newly registered clients, servers and created
3195 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
3196 SilcPacketContext *packet)
3198 SilcBuffer buffer = packet->buffer;
3200 SilcServerEntry tmpserver, router;
3201 SilcSocketConnection router_sock;
3203 unsigned char *id_string;
3206 SILC_LOG_DEBUG(("Processing new ID"));
3208 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3209 server->server_type == SILC_SERVER ||
3210 packet->src_id_type != SILC_ID_SERVER)
3213 silc_buffer_unformat(buffer,
3214 SILC_STR_UI_SHORT(&id_type),
3215 SILC_STR_UI16_STRING_ALLOC(&id_string),
3218 /* Normal server cannot have other normal server connections */
3219 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
3222 id = silc_id_str2id(id_string, id_type);
3226 /* If the packet is originated from the one who sent it to us we know
3227 that the ID belongs to our cell, unless the sender was router. */
3228 tmpid = silc_id_str2id(packet->src_id, SILC_ID_SERVER);
3229 tmpserver = (SilcServerEntry)sock->user_data;
3231 if (!SILC_ID_SERVER_COMPARE(tmpid, tmpserver->id) &&
3232 sock->type == SILC_SOCKET_TYPE_SERVER) {
3233 id_list = server->local_list;
3235 router = sock->user_data;
3236 /* router = server->id_entry; */
3238 id_list = server->global_list;
3239 router_sock = (SilcSocketConnection)server->id_entry->router->connection;
3240 router = server->id_entry->router;
3246 case SILC_ID_CLIENT:
3248 SilcClientEntry idlist;
3250 /* Add the client to our local list. We are router and we keep
3251 cell specific local database of all clients in the cell. */
3252 idlist = silc_idlist_add_client(id_list, NULL, NULL, NULL,
3253 id, router, NULL, NULL,
3254 NULL, NULL, NULL, router_sock);
3258 case SILC_ID_SERVER:
3260 SilcServerEntry idlist;
3262 /* Add the server to our local list. We are router and we keep
3263 cell specific local database of all servers in the cell. */
3264 idlist = silc_idlist_add_server(id_list, NULL, 0,
3265 id, router, NULL, NULL,
3266 NULL, NULL, NULL, router_sock);
3270 case SILC_ID_CHANNEL:
3271 /* Add the channel to our local list. We are router and we keep
3272 cell specific local database of all channels in the cell. */
3273 silc_idlist_add_channel(id_list, NULL, 0, id, router, NULL);
3282 silc_free(id_string);
3285 /* Received packet to remove a user from a channel. Routers notify other
3286 routers that user has left a channel. Client must not send this packet.
3287 Normal server may send this packet but ignores if it receives one. */
3289 void silc_server_remove_channel_user(SilcServer server,
3290 SilcSocketConnection sock,
3291 SilcPacketContext *packet)
3293 SilcBuffer buffer = packet->buffer;
3294 unsigned char *tmp1 = NULL, *tmp2 = NULL;
3295 SilcClientID *client_id = NULL;
3296 SilcChannelID *channel_id = NULL;
3297 SilcChannelEntry channel;
3298 SilcClientEntry client;
3300 SILC_LOG_DEBUG(("Removing user from channel"));
3302 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3303 server->server_type == SILC_SERVER)
3306 silc_buffer_unformat(buffer,
3307 SILC_STR_UI16_STRING_ALLOC(&tmp1),
3308 SILC_STR_UI16_STRING_ALLOC(&tmp2),
3314 client_id = silc_id_str2id(tmp1, SILC_ID_CLIENT);
3315 channel_id = silc_id_str2id(tmp2, SILC_ID_CHANNEL);
3316 if (!client_id || !channel_id)
3319 /* XXX routers should check server->global_list as well */
3320 /* Get channel entry */
3321 channel = silc_idlist_find_channel_by_id(server->local_list, channel_id);
3325 /* XXX routers should check server->global_list as well */
3326 /* Get client entry */
3327 client = silc_idlist_find_client_by_id(server->local_list, client_id);
3331 /* Remove from channel */
3332 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
3340 silc_free(client_id);
3342 silc_free(channel_id);