5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * This is the actual SILC server than handles everything relating to
22 * servicing the SILC connections. This is also a SILC router as a router
23 * is also normal server.
28 * Revision 1.4 2000/07/05 06:14:01 priikone
29 * Global costemic changes.
31 * Revision 1.3 2000/07/04 08:13:53 priikone
32 * Changed message route discovery to use silc_server_get_route.
33 * Added silc_server_client_on_channel function.
35 * Revision 1.1.1.1 2000/06/27 11:36:56 priikone
36 * Imported from internal CVS/Added Log headers.
41 #include "serverincludes.h"
42 #include "server_internal.h"
44 /* Static prototypes */
45 SILC_TASK_CALLBACK(silc_server_connect_to_router);
46 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
47 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
48 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
49 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
50 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
51 SILC_TASK_CALLBACK(silc_server_packet_process);
52 SILC_TASK_CALLBACK(silc_server_packet_parse);
53 SILC_TASK_CALLBACK(silc_server_timeout_remote);
56 void silc_server_packet_parse_type(SilcServer server,
57 SilcSocketConnection sock,
58 SilcPacketContext *packet);
60 static int silc_server_packet_check_mac(SilcServer server,
61 SilcSocketConnection sock,
63 static int silc_server_packet_decrypt_rest(SilcServer server,
64 SilcSocketConnection sock,
66 static int silc_server_packet_decrypt_rest_special(SilcServer server,
67 SilcSocketConnection sock,
70 extern char server_version[];
72 /* Allocates a new SILC server object. This has to be done before the server
73 can be used. After allocation one must call silc_server_init to initialize
74 the server. The new allocated server object is returned to the new_server
77 int silc_server_alloc(SilcServer *new_server)
79 SILC_LOG_DEBUG(("Allocating new server object"));
81 *new_server = silc_calloc(1, sizeof(**new_server));
82 if (*new_server == NULL) {
83 SILC_LOG_ERROR(("Could not allocate new server object"));
87 /* Set default values */
88 (*new_server)->server_name = NULL;
89 (*new_server)->server_type = SILC_SERVER;
90 (*new_server)->standalone = FALSE;
91 (*new_server)->id = NULL;
92 (*new_server)->io_queue = NULL;
93 (*new_server)->timeout_queue = NULL;
94 (*new_server)->local_list = silc_calloc(1, sizeof(SilcIDListObject));
95 (*new_server)->global_list = silc_calloc(1, sizeof(SilcIDListObject));
96 (*new_server)->rng = NULL;
97 (*new_server)->md5hash = NULL;
98 (*new_server)->sha1hash = NULL;
99 /* (*new_server)->public_key = NULL;*/
104 /* Free's the SILC server object. This is called at the very end before
107 void silc_server_free(SilcServer server)
110 if (server->local_list)
111 silc_free(server->local_list);
112 if (server->global_list)
113 silc_free(server->global_list);
115 silc_rng_free(server->rng);
117 silc_math_primegen_uninit(); /* XXX */
122 /* Initializes the entire SILC server. This is called always before running
123 the server. This is called only once at the initialization of the program.
124 This binds the server to its listenning port. After this function returns
125 one should call silc_server_run to start the server. This returns TRUE
126 when everything is ok to run the server. Configuration file must be
127 read and parsed before calling this. */
129 int silc_server_init(SilcServer server)
131 int *sock = NULL, sock_count, i;
133 SilcServerList *id_entry;
136 SILC_LOG_DEBUG(("Initializing server"));
138 assert(server->config);
140 /* Set log files where log message should be saved. */
141 server->config->server = server;
142 silc_config_server_setlogfiles(server->config);
144 /* Register all configured ciphers, PKCS and hash functions. */
145 silc_config_server_register_ciphers(server->config);
146 silc_config_server_register_pkcs(server->config);
147 silc_config_server_register_hashfuncs(server->config);
149 /* Initialize random number generator for the server. */
150 server->rng = silc_rng_alloc();
151 silc_rng_init(server->rng);
152 silc_math_primegen_init(); /* XXX */
154 /* Initialize hash functions for server to use */
155 silc_hash_alloc("md5", &server->md5hash);
156 silc_hash_alloc("sha1", &server->sha1hash);
158 /* Initialize none cipher */
159 silc_cipher_alloc("none", &server->none_cipher);
161 /* XXXXX Generate RSA key pair */
163 unsigned char *public_key;
164 unsigned char *private_key;
165 unsigned int pk_len, prv_len;
166 SilcPublicKey pub_key;
167 SilcPrivateKey prv_key;
169 if (silc_pkcs_alloc("rsa", &server->public_key) == FALSE) {
170 SILC_LOG_ERROR(("Could not create RSA key pair"));
174 if (server->public_key->pkcs->init(server->public_key->context,
175 1024, server->rng) == FALSE) {
176 SILC_LOG_ERROR(("Could not generate RSA key pair"));
181 server->public_key->pkcs->get_public_key(server->public_key->context,
184 server->public_key->pkcs->get_private_key(server->public_key->context,
187 SILC_LOG_HEXDUMP(("public key"), public_key, pk_len);
188 SILC_LOG_HEXDUMP(("private key"), private_key, prv_len);
190 pub_key = silc_pkcs_public_key_alloc("rsa", "UN=root, HN=dummy",
192 prv_key = silc_pkcs_private_key_alloc("rsa", private_key, prv_len);
195 silc_pkcs_save_public_key("pubkey.pub", pub_key);
196 silc_pkcs_save_private_key("privkey.prv", prv_key, NULL);
198 memset(public_key, 0, pk_len);
199 memset(private_key, 0, prv_len);
200 silc_free(public_key);
201 silc_free(private_key);
202 silc_pkcs_public_key_free(pub_key);
203 silc_pkcs_private_key_free(prv_key);
206 /* Create a listening server. Note that our server can listen on
207 multiple ports. All listeners are created here and now. */
208 /* XXX Still check this whether to use server_info or listen_port. */
210 while(server->config->listen_port) {
213 tmp = silc_net_create_server(server->config->listen_port->port,
214 server->config->listen_port->host);
218 sock = silc_realloc(sock, (sizeof(int *) * (sock_count + 1)));
219 sock[sock_count] = tmp;
220 server->config->listen_port = server->config->listen_port->next;
224 /* Allocate the entire socket list that is used in server. Eventually
225 all connections will have entry in this table (it is a table of
226 pointers to the actual object that is allocated individually
228 server->sockets = silc_calloc(SILC_SERVER_MAX_CONNECTIONS,
229 sizeof(*server->sockets));
231 for (i = 0; i < sock_count; i++) {
232 SilcSocketConnection newsocket = NULL;
234 /* Set socket to non-blocking mode */
235 silc_net_set_socket_nonblock(sock[i]);
236 server->sock = sock[i];
238 /* Create a Server ID for the server. */
239 silc_id_create_server_id(sock[i], server->rng, &id);
245 server->id_type = SILC_ID_SERVER;
246 server->server_name = server->config->server_info->server_name;
248 /* Add ourselves to the server list. We don't have a router yet
249 beacuse we haven't established a route yet. It will be done later.
250 For now, NULL is sent as router. This allocates new entry to
252 silc_idlist_add_server(&server->local_list->servers,
253 server->config->server_info->server_name,
254 server->server_type, server->id, NULL,
255 server->send_key, server->receive_key,
256 NULL, NULL, &id_entry);
260 /* Add ourselves also to the socket table. The entry allocated above
261 is sent as argument for fast referencing in the future. */
262 silc_socket_alloc(sock[i], SILC_SOCKET_TYPE_SERVER, id_entry,
267 server->sockets[sock[i]] = newsocket;
269 /* Put the allocated socket pointer also to the entry allocated above
270 for fast back-referencing to the socket list. */
271 id_entry->connection = (void *)server->sockets[sock[i]];
272 server->id_entry = id_entry;
275 /* Register the task queues. In SILC we have by default three task queues.
276 One task queue for non-timeout tasks which perform different kind of
277 I/O on file descriptors, timeout task queue for timeout tasks, and,
278 generic non-timeout task queue whose tasks apply to all connections. */
279 silc_task_queue_alloc(&server->io_queue, TRUE);
280 if (!server->io_queue) {
283 silc_task_queue_alloc(&server->timeout_queue, TRUE);
284 if (!server->timeout_queue) {
287 silc_task_queue_alloc(&server->generic_queue, TRUE);
288 if (!server->generic_queue) {
292 /* Initialize the scheduler */
293 silc_schedule_init(server->io_queue, server->timeout_queue,
294 server->generic_queue,
295 SILC_SERVER_MAX_CONNECTIONS);
297 /* Add the first task to the queue. This is task that is executed by
298 timeout. It expires as soon as the caller calls silc_server_run. This
299 task performs authentication protocol and key exchange with our
301 if (silc_task_register(server->timeout_queue, sock[0],
302 silc_server_connect_to_router,
303 (void *)server, 0, 1,
305 SILC_TASK_PRI_NORMAL) == NULL) {
309 /* If server connections has been configured then we must be router as
310 normal server cannot have server connections, only router connections. */
311 if (server->config->servers)
312 server->server_type = SILC_ROUTER;
314 SILC_LOG_DEBUG(("Server initialized"));
316 /* We are done here, return succesfully */
320 silc_task_queue_free(server->timeout_queue);
322 silc_task_queue_free(server->io_queue);
324 for (i = 0; i < sock_count; i++)
325 silc_net_close_server(sock[i]);
330 /* Stops the SILC server. This function is used to shutdown the server.
331 This is usually called after the scheduler has returned. After stopping
332 the server one should call silc_server_free. */
334 void silc_server_stop(SilcServer server)
336 SILC_LOG_DEBUG(("Stopping server"));
338 /* Stop the scheduler, although it might be already stopped. This
339 doesn't hurt anyone. This removes all the tasks and task queues,
341 silc_schedule_stop();
342 silc_schedule_uninit();
344 SILC_LOG_DEBUG(("Server stopped"));
347 /* The heart of the server. This runs the scheduler thus runs the server. */
349 void silc_server_run(SilcServer server)
351 SILC_LOG_DEBUG(("Running server"));
353 /* Start the scheduler, the heart of the SILC server. When this returns
354 the program will be terminated. */
358 /* This function connects to our primary router or if we are a router this
359 establishes all our primary routes. This is called at the start of the
360 server to do authentication and key exchange with our router - called
363 SILC_TASK_CALLBACK(silc_server_connect_to_router)
365 SilcServer server = (SilcServer)context;
366 SilcSocketConnection newsocket;
369 SILC_LOG_DEBUG(("Connecting to router(s)"));
371 /* if we are normal SILC server we need to connect to our cell's
373 if (server->server_type == SILC_SERVER) {
374 SilcProtocol protocol;
375 SilcServerKEInternalContext *proto_ctx;
377 /* Create connection to the router, if configured. */
378 if (server->config->routers) {
379 sock = silc_net_create_connection(server->config->routers->port,
380 server->config->routers->host);
382 SILC_LOG_ERROR(("Could not connect to router"));
383 silc_schedule_stop();
387 /* Set socket options */
388 silc_net_set_socket_nonblock(sock);
389 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
391 /* Create socket connection for the connection. Even though we
392 know that we are connecting to a router we will mark the socket
393 to be unknown connection until we have executed authentication
395 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
396 server->sockets[sock] = newsocket;
397 newsocket->hostname = server->config->routers->host;
398 newsocket->port = server->config->routers->port;
400 /* Allocate internal protocol context. This is sent as context
402 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
403 proto_ctx->server = context;
404 proto_ctx->sock = newsocket;
405 proto_ctx->rng = server->rng;
406 proto_ctx->responder = FALSE;
408 /* Perform key exchange protocol. silc_server_connect_to_router_second
409 will be called after the protocol is finished. */
410 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
411 &protocol, proto_ctx,
412 silc_server_connect_to_router_second);
413 newsocket->protocol = protocol;
415 /* Register a timeout task that will be executed if the protocol
416 is not executed within 15 seconds. For now, this is a hard coded
417 limit. After 15 secs the connection will be closed if the key
418 exchange protocol has not been executed. */
419 proto_ctx->timeout_task =
420 silc_task_register(server->timeout_queue, sock,
421 silc_server_timeout_remote,
426 /* Register the connection for network input and output. This sets
427 that scheduler will listen for incoming packets for this connection
428 and sets that outgoing packets may be sent to this connection as
429 well. However, this doesn't set the scheduler for outgoing traffic,
430 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
431 later when outgoing data is available. */
432 SILC_REGISTER_CONNECTION_FOR_IO(sock);
434 /* Run the protocol */
435 protocol->execute(server->timeout_queue, 0, protocol, sock, 0, 0);
440 /* if we are a SILC router we need to establish all of our primary
442 if (server->server_type == SILC_ROUTER) {
443 SilcConfigServerSectionServerConnection *ptr;
445 /* Create the connections to all our routes */
446 ptr = server->config->routers;
448 SilcProtocol protocol;
449 SilcServerKEInternalContext *proto_ctx;
451 /* Create the connection to the remote end */
452 sock = silc_net_create_connection(ptr->port, ptr->host);
454 SILC_LOG_ERROR(("Could not connect to router"));
455 silc_schedule_stop();
459 /* Set socket options */
460 silc_net_set_socket_nonblock(sock);
461 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
463 /* Create socket connection for the connection. Even though we
464 know that we are connecting to a router we will mark the socket
465 to be unknown connection until we have executed authentication
467 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
468 server->sockets[sock] = newsocket;
469 newsocket->hostname = ptr->host;
470 newsocket->port = ptr->port;
472 /* Allocate internal protocol context. This is sent as context
474 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
475 proto_ctx->server = context;
476 proto_ctx->sock = newsocket;
477 proto_ctx->rng = server->rng;
478 proto_ctx->responder = FALSE;
480 /* Perform key exchange protocol. silc_server_connect_to_router_final
481 will be called after the protocol is finished. */
482 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
483 &protocol, proto_ctx,
484 silc_server_connect_to_router_second);
485 newsocket->protocol = protocol;
487 /* Register a timeout task that will be executed if the protocol
488 is not executed within 15 seconds. For now, this is a hard coded
489 limit. After 15 secs the connection will be closed if the key
490 exchange protocol has not been executed. */
491 proto_ctx->timeout_task =
492 silc_task_register(server->timeout_queue, sock,
493 silc_server_timeout_remote,
498 /* Register the connection for network input and output. This sets
499 that scheduler will listen for incoming packets for this connection
500 and sets that outgoing packets may be sent to this connection as
501 well. However, this doesn't set the scheduler for outgoing traffic,
502 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
503 later when outgoing data is available. */
504 SILC_REGISTER_CONNECTION_FOR_IO(sock);
506 /* Run the protocol */
507 protocol->execute(server->timeout_queue, 0, protocol, sock, 0, 0);
516 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
518 /* There wasn't a configured router, we will continue but we don't
519 have a connection to outside world. We will be standalone server. */
520 server->standalone = TRUE;
522 /* Add a task to the queue. This task receives new connections to the
523 server. This task remains on the queue until the end of the program. */
524 if (silc_task_register(server->io_queue, fd,
525 silc_server_accept_new_connection,
526 (void *)server, 0, 0,
528 SILC_TASK_PRI_NORMAL) == NULL) {
529 silc_schedule_stop();
534 /* Second part of connecting to router(s). Key exchange protocol has been
535 executed and now we will execute authentication protocol. */
537 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
539 SilcProtocol protocol = (SilcProtocol)context;
540 SilcServerKEInternalContext *ctx =
541 (SilcServerKEInternalContext *)protocol->context;
542 SilcServer server = (SilcServer)ctx->server;
543 SilcSocketConnection sock = NULL;
544 SilcServerConnAuthInternalContext *proto_ctx;
546 SILC_LOG_DEBUG(("Start"));
548 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
549 /* Error occured during protocol */
550 silc_protocol_free(protocol);
552 silc_buffer_free(ctx->packet);
554 silc_ske_free(ctx->ske);
556 silc_free(ctx->dest_id);
558 sock->protocol = NULL;
559 silc_server_disconnect_remote(server, sock, "Server closed connection: "
560 "Key exchange failed");
564 /* Allocate internal context for the authentication protocol. This
565 is sent as context for the protocol. */
566 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
567 proto_ctx->server = (void *)server;
568 proto_ctx->sock = sock = server->sockets[fd];
569 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
570 proto_ctx->dest_id_type = ctx->dest_id_type;
571 proto_ctx->dest_id = ctx->dest_id;
573 /* Resolve the authentication method used in this connection */
574 proto_ctx->auth_meth = SILC_PROTOCOL_CONN_AUTH_PASSWORD;
575 if (server->config->routers) {
576 SilcConfigServerSectionServerConnection *conn = NULL;
578 /* Check if we find a match from user configured connections */
579 conn = silc_config_server_find_router_conn(server->config,
583 /* Match found. Use the configured authentication method */
584 proto_ctx->auth_meth = conn->auth_meth;
585 if (conn->auth_data) {
586 proto_ctx->auth_data = strdup(conn->auth_data);
587 proto_ctx->auth_data_len = strlen(conn->auth_data);
590 /* No match found. */
597 /* Free old protocol as it is finished now */
598 silc_protocol_free(protocol);
600 silc_buffer_free(ctx->packet);
602 sock->protocol = NULL;
604 /* Allocate the authentication protocol. This is allocated here
605 but we won't start it yet. We will be receiving party of this
606 protocol thus we will wait that connecting party will make
608 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
609 &sock->protocol, proto_ctx,
610 silc_server_connect_to_router_final);
612 /* Register timeout task. If the protocol is not executed inside
613 this timelimit the connection will be terminated. Currently
614 this is 15 seconds and is hard coded limit (XXX). */
615 proto_ctx->timeout_task =
616 silc_task_register(server->timeout_queue, sock->sock,
617 silc_server_timeout_remote,
618 (void *)server, 15, 0,
622 /* Run the protocol */
623 sock->protocol->execute(server->timeout_queue, 0,
624 sock->protocol, sock->sock, 0, 0);
627 /* Finalizes the connection to router. Registers a server task to the
628 queue so that we can accept new connections. */
630 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
632 SilcProtocol protocol = (SilcProtocol)context;
633 SilcServerConnAuthInternalContext *ctx =
634 (SilcServerConnAuthInternalContext *)protocol->context;
635 SilcServer server = (SilcServer)ctx->server;
636 SilcSocketConnection sock = ctx->sock;
637 SilcServerList *id_entry;
638 SilcIDListUnknown *conn_data;
640 unsigned char *id_string;
642 SILC_LOG_DEBUG(("Start"));
644 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
645 /* Error occured during protocol */
646 silc_protocol_free(protocol);
648 silc_buffer_free(ctx->packet);
650 silc_ske_free(ctx->ske);
652 silc_free(ctx->dest_id);
654 sock->protocol = NULL;
655 silc_server_disconnect_remote(server, sock, "Server closed connection: "
656 "Authentication failed");
660 /* Add a task to the queue. This task receives new connections to the
661 server. This task remains on the queue until the end of the program. */
662 if (!server->listenning) {
663 if (silc_task_register(server->io_queue, server->sock,
664 silc_server_accept_new_connection,
665 (void *)server, 0, 0,
667 SILC_TASK_PRI_NORMAL) == NULL) {
668 silc_schedule_stop();
671 server->listenning = TRUE;
675 /* Send NEW_SERVER packet to the router. We will become registered
676 to the SILC network after sending this packet. */
677 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
678 packet = silc_buffer_alloc(2 + 2 + SILC_ID_SERVER_LEN +
679 strlen(server->server_name));
680 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
681 silc_buffer_format(packet,
682 SILC_STR_UI_SHORT(SILC_ID_SERVER_LEN),
683 SILC_STR_UI_XNSTRING(id_string, SILC_ID_SERVER_LEN),
684 SILC_STR_UI_SHORT(strlen(server->server_name)),
685 SILC_STR_UI_XNSTRING(server->server_name,
686 strlen(server->server_name)),
689 /* Send the packet */
690 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
691 packet->data, packet->len, TRUE);
692 silc_buffer_free(packet);
693 silc_free(id_string);
695 SILC_LOG_DEBUG(("Connected to router %s", sock->hostname));
697 /* Add the connected router to local server list */
698 server->standalone = FALSE;
699 conn_data = (SilcIDListUnknown *)sock->user_data;
700 silc_idlist_add_server(&server->local_list->servers,
701 sock->hostname ? sock->hostname : sock->ip,
702 SILC_ROUTER, ctx->dest_id, NULL,
703 conn_data->send_key, conn_data->receive_key,
704 conn_data->pkcs, conn_data->hmac, &id_entry);
706 id_entry->hmac_key = conn_data->hmac_key;
707 id_entry->hmac_key_len = conn_data->hmac_key_len;
708 id_entry->connection = sock;
709 sock->user_data = (void *)id_entry;
710 sock->type = SILC_SOCKET_TYPE_ROUTER;
711 server->id_entry->router = id_entry;
713 /* Free the temporary connection data context from key exchange */
714 silc_free(conn_data);
716 /* Free the protocol object */
717 silc_protocol_free(protocol);
719 silc_buffer_free(ctx->packet);
721 silc_ske_free(ctx->ske);
723 sock->protocol = NULL;
726 /* Accepts new connections to the server. Accepting new connections are
727 done in three parts to make it async. */
729 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
731 SilcServer server = (SilcServer)context;
732 SilcSocketConnection newsocket;
733 SilcServerKEInternalContext *proto_ctx;
736 SILC_LOG_DEBUG(("Accepting new connection"));
738 sock = silc_net_accept_connection(server->sock);
740 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
744 /* Check max connections */
745 if (sock > SILC_SERVER_MAX_CONNECTIONS) {
746 if (server->config->redirect) {
747 /* XXX Redirecting connection to somewhere else now?? */
748 /*silc_server_send_notify("Server is full, trying to redirect..."); */
750 SILC_LOG_ERROR(("Refusing connection, server is full"));
755 /* Set socket options */
756 silc_net_set_socket_nonblock(sock);
757 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
759 /* We don't create a ID yet, since we don't know what type of connection
760 this is yet. But, we do add the connection to the socket table. */
761 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
762 server->sockets[sock] = newsocket;
764 /* XXX This MUST be done async as this will block the entire server
765 process. Either we have to do our own resolver stuff or in the future
766 we can use threads. */
767 /* Perform mandatory name and address lookups for the remote host. */
768 silc_net_check_host_by_sock(sock, &newsocket->hostname, &newsocket->ip);
769 if (!newsocket->ip || !newsocket->hostname) {
770 SILC_LOG_DEBUG(("IP lookup/DNS lookup failed"));
771 SILC_LOG_ERROR(("IP lookup/DNS lookup failed"));
775 SILC_LOG_INFO(("Incoming connection from %s (%s)", newsocket->hostname,
778 /* Allocate internal context for key exchange protocol. This is
779 sent as context for the protocol. */
780 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
781 proto_ctx->server = context;
782 proto_ctx->sock = newsocket;
783 proto_ctx->rng = server->rng;
784 proto_ctx->responder = TRUE;
786 /* Prepare the connection for key exchange protocol. We allocate the
787 protocol but will not start it yet. The connector will be the
788 initiator of the protocol thus we will wait for initiation from
789 there before we start the protocol. */
790 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
791 &newsocket->protocol, proto_ctx,
792 silc_server_accept_new_connection_second);
794 /* Register a timeout task that will be executed if the connector
795 will not start the key exchange protocol within 15 seconds. For
796 now, this is a hard coded limit. After 15 secs the connection will
797 be closed if the key exchange protocol has not been started. */
798 proto_ctx->timeout_task =
799 silc_task_register(server->timeout_queue, newsocket->sock,
800 silc_server_timeout_remote,
805 /* Register the connection for network input and output. This sets
806 that scheduler will listen for incoming packets for this connection
807 and sets that outgoing packets may be sent to this connection as well.
808 However, this doesn't set the scheduler for outgoing traffic, it
809 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
810 later when outgoing data is available. */
811 SILC_REGISTER_CONNECTION_FOR_IO(sock);
814 /* Second part of accepting new connection. Key exchange protocol has been
815 performed and now it is time to do little connection authentication
816 protocol to figure out whether this connection is client or server
817 and whether it has right to access this server (especially server
818 connections needs to be authenticated). */
820 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
822 SilcProtocol protocol = (SilcProtocol)context;
823 SilcServerKEInternalContext *ctx =
824 (SilcServerKEInternalContext *)protocol->context;
825 SilcServer server = (SilcServer)ctx->server;
826 SilcSocketConnection sock = NULL;
827 SilcServerConnAuthInternalContext *proto_ctx;
829 SILC_LOG_DEBUG(("Start"));
831 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
832 /* Error occured during protocol */
833 silc_protocol_free(protocol);
835 silc_buffer_free(ctx->packet);
837 silc_ske_free(ctx->ske);
839 silc_free(ctx->dest_id);
841 sock->protocol = NULL;
842 silc_server_disconnect_remote(server, sock, "Server closed connection: "
843 "Key exchange failed");
847 /* Allocate internal context for the authentication protocol. This
848 is sent as context for the protocol. */
849 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
850 proto_ctx->server = (void *)server;
851 proto_ctx->sock = sock = server->sockets[fd];
852 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
853 proto_ctx->responder = TRUE;
854 proto_ctx->dest_id_type = ctx->dest_id_type;
855 proto_ctx->dest_id = ctx->dest_id;
857 /* Free old protocol as it is finished now */
858 silc_protocol_free(protocol);
860 silc_buffer_free(ctx->packet);
862 sock->protocol = NULL;
864 /* Allocate the authentication protocol. This is allocated here
865 but we won't start it yet. We will be receiving party of this
866 protocol thus we will wait that connecting party will make
868 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
869 &sock->protocol, proto_ctx,
870 silc_server_accept_new_connection_final);
872 /* Register timeout task. If the protocol is not executed inside
873 this timelimit the connection will be terminated. Currently
874 this is 60 seconds and is hard coded limit (XXX). */
875 proto_ctx->timeout_task =
876 silc_task_register(server->timeout_queue, sock->sock,
877 silc_server_timeout_remote,
878 (void *)server, 60, 0,
883 /* Final part of accepting new connection. The connection has now
884 been authenticated and keys has been exchanged. We also know whether
885 this is client or server connection. */
887 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
889 SilcProtocol protocol = (SilcProtocol)context;
890 SilcServerConnAuthInternalContext *ctx =
891 (SilcServerConnAuthInternalContext *)protocol->context;
892 SilcServer server = (SilcServer)ctx->server;
893 SilcSocketConnection sock = ctx->sock;
895 SILC_LOG_DEBUG(("Start"));
897 if (protocol->state == SILC_PROTOCOL_STATE_ERROR) {
898 /* Error occured during protocol */
899 silc_protocol_free(protocol);
901 silc_buffer_free(ctx->packet);
903 silc_ske_free(ctx->ske);
905 silc_free(ctx->dest_id);
907 sock->protocol = NULL;
908 silc_server_disconnect_remote(server, sock, "Server closed connection: "
909 "Authentication failed");
913 sock->type = ctx->conn_type;
915 case SILC_SOCKET_TYPE_CLIENT:
917 SilcClientList *id_entry = NULL;
918 SilcIDListUnknown *conn_data = sock->user_data;
920 SILC_LOG_DEBUG(("Remote host is client"));
922 SILC_LOG_INFO(("Connection from %s (%s) is client", sock->hostname,
925 /* Add the client to the client ID list. We have not created the
926 client ID for the client yet. This is done when client registers
927 itself by sending NEW_CLIENT packet. */
928 silc_idlist_add_client(&server->local_list->clients,
929 NULL, NULL, NULL, NULL, NULL,
930 conn_data->send_key, conn_data->receive_key,
931 conn_data->pkcs, conn_data->hmac, &id_entry);
933 id_entry->hmac_key = conn_data->hmac_key;
934 id_entry->hmac_key_len = conn_data->hmac_key_len;
935 id_entry->connection = sock;
937 /* Free the temporary connection data context from key exchange */
938 silc_free(conn_data);
940 /* Mark the entry to the ID list to the socket connection for
941 fast referencing in the future. */
942 sock->user_data = (void *)id_entry;
945 case SILC_SOCKET_TYPE_SERVER:
946 case SILC_SOCKET_TYPE_ROUTER:
948 SilcServerList *id_entry;
949 SilcIDListUnknown *conn_data = sock->user_data;
951 SILC_LOG_DEBUG(("Remote host is %s",
952 sock->type == SILC_SOCKET_TYPE_SERVER ?
953 "server" : "router"));
955 SILC_LOG_INFO(("Connection from %s (%s) is %s", sock->hostname,
956 sock->ip, sock->type == SILC_SOCKET_TYPE_SERVER ?
957 "server" : "router"));
959 /* Add the server to the ID list. We don't have the server's ID
960 yet but we will receive it after the server sends NEW_SERVER
962 silc_idlist_add_server(&server->local_list->servers, NULL,
963 sock->type == SILC_SOCKET_TYPE_SERVER ?
964 SILC_SERVER : SILC_ROUTER, NULL, NULL,
965 conn_data->send_key, conn_data->receive_key,
966 conn_data->pkcs, conn_data->hmac, &id_entry);
968 id_entry->hmac_key = conn_data->hmac_key;
969 id_entry->hmac_key_len = conn_data->hmac_key_len;
970 id_entry->connection = sock;
972 /* Free the temporary connection data context from key exchange */
973 silc_free(conn_data);
975 /* Mark the entry to the ID list to the socket connection for
976 fast referencing in the future. */
977 sock->user_data = (void *)id_entry;
979 /* There is connection to other server now, if it is router then
980 we will have connection to outside world. If we are router but
981 normal server connected to us then we will remain standalone,
982 if we are standlone. */
983 if (server->standalone && sock->type == SILC_SOCKET_TYPE_ROUTER) {
984 SILC_LOG_DEBUG(("We are not standalone server anymore"));
985 server->standalone = FALSE;
993 /* Connection has been fully established now. Everything is ok. */
994 SILC_LOG_DEBUG(("New connection authenticated"));
996 silc_protocol_free(protocol);
998 silc_buffer_free(ctx->packet);
1000 silc_ske_free(ctx->ske);
1002 silc_free(ctx->dest_id);
1004 sock->protocol = NULL;
1008 SilcPacketContext *packetdata;
1010 SilcSocketConnection sock;
1013 } SilcServerInternalPacket;
1015 /* This function is used to read packets from network and send packets to
1016 network. This is usually a generic task. */
1018 SILC_TASK_CALLBACK(silc_server_packet_process)
1020 SilcServer server = (SilcServer)context;
1021 SilcSocketConnection sock = server->sockets[fd];
1022 int ret, packetlen, paddedlen;
1024 SILC_LOG_DEBUG(("Processing packet"));
1026 /* Packet sending */
1027 if (type == SILC_TASK_WRITE) {
1028 SILC_LOG_DEBUG(("Writing data to connection"));
1030 if (sock->outbuf->data - sock->outbuf->head)
1031 silc_buffer_push(sock->outbuf,
1032 sock->outbuf->data - sock->outbuf->head);
1034 /* Write the packet out to the connection */
1035 ret = silc_packet_write(fd, sock->outbuf);
1037 /* If returned -2 could not write to connection now, will do
1044 SILC_LOG_ERROR(("Could not write, packet dropped"));
1046 /* The packet has been sent and now it is time to set the connection
1047 back to only for input. When there is again some outgoing data
1048 available for this connection it will be set for output as well.
1049 This call clears the output setting and sets it only for input. */
1050 SILC_SET_CONNECTION_FOR_INPUT(fd);
1051 SILC_UNSET_OUTBUF_PENDING(sock);
1053 silc_buffer_clear(sock->outbuf);
1057 /* Packet receiving */
1058 if (type == SILC_TASK_READ) {
1059 SILC_LOG_DEBUG(("Reading data from connection"));
1061 /* Allocate the incoming data buffer if not done already. */
1063 sock->inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE);
1065 /* Read some data from connection */
1066 ret = silc_packet_read(fd, sock->inbuf);
1068 /* If returned -2 data was not available now, will read it later. */
1074 SILC_LOG_ERROR(("Could not read, packet dropped"));
1080 SILC_LOG_DEBUG(("Read EOF"));
1082 /* If connection is disconnecting already we will finally
1083 close the connection */
1084 if (SILC_IS_DISCONNECTING(sock)) {
1085 if (sock->user_data)
1086 silc_server_free_sock_user_data(server, sock);
1087 silc_server_close_connection(server, sock);
1091 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1093 if (sock->user_data)
1094 silc_server_free_sock_user_data(server, sock);
1095 silc_server_close_connection(server, sock);
1099 /* If connection is disconnecting or disconnected we will ignore
1101 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1102 SILC_LOG_DEBUG(("Ignoring read data from invalid connection"));
1106 /* Check whether we received a whole packet. If reading went without
1107 errors we either read a whole packet or the read packet is
1108 incorrect and will be dropped. */
1109 SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
1110 if (sock->inbuf->len < paddedlen || (packetlen < SILC_PACKET_MIN_LEN)) {
1111 SILC_LOG_DEBUG(("Received incorrect packet, dropped"));
1112 silc_buffer_clear(sock->inbuf);
1113 silc_server_disconnect_remote(server, sock, "Incorrect packet");
1117 /* Decrypt a packet coming from client. */
1118 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
1119 SilcClientList *clnt = (SilcClientList *)sock->user_data;
1120 SilcServerInternalPacket *packet;
1124 mac_len = clnt->hmac->hash->hash->hash_len;
1126 if (sock->inbuf->len - 2 > (paddedlen + mac_len)) {
1127 /* Received possibly many packets at once */
1129 while(sock->inbuf->len > 0) {
1130 SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
1131 if (sock->inbuf->len < paddedlen) {
1132 SILC_LOG_DEBUG(("Receive incorrect packet, dropped"));
1137 packet = silc_calloc(1, sizeof(*packet));
1138 packet->server = server;
1139 packet->sock = sock;
1140 packet->packetdata = silc_calloc(1, sizeof(*packet->packetdata));
1141 packet->packetdata->buffer = silc_buffer_alloc(paddedlen + mac_len);
1142 silc_buffer_pull_tail(packet->packetdata->buffer,
1143 SILC_BUFFER_END(packet->packetdata->buffer));
1144 silc_buffer_put(packet->packetdata->buffer, sock->inbuf->data,
1145 paddedlen + mac_len);
1147 packet->cipher = clnt->receive_key;
1148 packet->hmac = clnt->hmac;
1151 SILC_LOG_HEXDUMP(("Incoming packet, len %d",
1152 packet->packetdata->buffer->len),
1153 packet->packetdata->buffer->data,
1154 packet->packetdata->buffer->len);
1156 /* Parse the packet with timeout */
1157 silc_task_register(server->timeout_queue, fd,
1158 silc_server_packet_parse,
1159 (void *)packet, 0, 100000,
1161 SILC_TASK_PRI_NORMAL);
1163 /* Pull the packet from inbuf thus we'll get the next one
1165 silc_buffer_pull(sock->inbuf, paddedlen);
1167 silc_buffer_pull(sock->inbuf, mac_len);
1169 silc_buffer_clear(sock->inbuf);
1172 SILC_LOG_HEXDUMP(("An incoming packet, len %d", sock->inbuf->len),
1173 sock->inbuf->data, sock->inbuf->len);
1175 SILC_LOG_DEBUG(("Packet from client, length %d", paddedlen));
1177 packet = silc_calloc(1, sizeof(*packet));
1178 packet->packetdata = silc_calloc(1, sizeof(*packet->packetdata));
1179 packet->packetdata->buffer = silc_buffer_copy(sock->inbuf);
1180 packet->server = server;
1181 packet->sock = sock;
1183 packet->cipher = clnt->receive_key;
1184 packet->hmac = clnt->hmac;
1186 silc_buffer_clear(sock->inbuf);
1188 /* The packet is ready to be parsed now. However, this is a client
1189 connection so we will parse the packet with timeout. */
1190 silc_task_register(server->timeout_queue, fd,
1191 silc_server_packet_parse,
1192 (void *)packet, 0, 100000,
1194 SILC_TASK_PRI_NORMAL);
1199 /* Decrypt a packet coming from server connection */
1200 if (sock->type == SILC_SOCKET_TYPE_SERVER ||
1201 sock->type == SILC_SOCKET_TYPE_ROUTER) {
1202 SilcServerList *srvr = (SilcServerList *)sock->user_data;
1203 SilcServerInternalPacket *packet;
1207 mac_len = srvr->hmac->hash->hash->hash_len;
1209 if (sock->inbuf->len - 2 > (paddedlen + mac_len)) {
1210 /* Received possibly many packets at once */
1212 while(sock->inbuf->len > 0) {
1213 SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
1214 if (sock->inbuf->len < paddedlen) {
1215 SILC_LOG_DEBUG(("Received incorrect packet, dropped"));
1220 packet = silc_calloc(1, sizeof(*packet));
1221 packet->server = server;
1222 packet->sock = sock;
1223 packet->packetdata = silc_calloc(1, sizeof(*packet->packetdata));
1224 packet->packetdata->buffer = silc_buffer_alloc(paddedlen + mac_len);
1225 silc_buffer_pull_tail(packet->packetdata->buffer,
1226 SILC_BUFFER_END(packet->packetdata->buffer));
1227 silc_buffer_put(packet->packetdata->buffer, sock->inbuf->data,
1228 paddedlen + mac_len);
1230 packet->cipher = srvr->receive_key;
1231 packet->hmac = srvr->hmac;
1234 SILC_LOG_HEXDUMP(("Incoming packet, len %d",
1235 packet->packetdata->buffer->len),
1236 packet->packetdata->buffer->data,
1237 packet->packetdata->buffer->len);
1239 SILC_LOG_DEBUG(("Packet from %s %s, packet length %d",
1240 srvr->server_type == SILC_SERVER ?
1241 "server" : "router",
1242 srvr->server_name, paddedlen));
1244 /* Parse it real soon as the packet is from server. */
1245 silc_task_register(server->timeout_queue, fd,
1246 silc_server_packet_parse,
1247 (void *)packet, 0, 1,
1249 SILC_TASK_PRI_NORMAL);
1251 /* Pull the packet from inbuf thus we'll get the next one
1253 silc_buffer_pull(sock->inbuf, paddedlen);
1255 silc_buffer_pull(sock->inbuf, mac_len);
1257 silc_buffer_clear(sock->inbuf);
1261 SILC_LOG_HEXDUMP(("An incoming packet, len %d", sock->inbuf->len),
1262 sock->inbuf->data, sock->inbuf->len);
1264 SILC_LOG_DEBUG(("Packet from %s %s, packet length %d",
1265 srvr->server_type == SILC_SERVER ?
1266 "server" : "router",
1267 srvr->server_name, paddedlen));
1269 packet = silc_calloc(1, sizeof(*packet));
1270 packet->packetdata = silc_calloc(1, sizeof(*packet->packetdata));
1271 packet->packetdata->buffer = silc_buffer_copy(sock->inbuf);
1272 packet->server = server;
1273 packet->sock = sock;
1275 packet->cipher = srvr->receive_key;
1276 packet->hmac = srvr->hmac;
1278 silc_buffer_clear(sock->inbuf);
1280 /* The packet is ready to be parsed now. However, this is a client
1281 connection so we will parse the packet with timeout. */
1282 silc_task_register(server->timeout_queue, fd,
1283 silc_server_packet_parse,
1284 (void *)packet, 0, 1,
1286 SILC_TASK_PRI_NORMAL);
1291 /* Decrypt a packet coming from client. */
1292 if (sock->type == SILC_SOCKET_TYPE_UNKNOWN) {
1293 SilcIDListUnknown *conn_data = (SilcIDListUnknown *)sock->user_data;
1294 SilcServerInternalPacket *packet;
1296 SILC_LOG_HEXDUMP(("Incoming packet, len %d", sock->inbuf->len),
1297 sock->inbuf->data, sock->inbuf->len);
1299 SILC_LOG_DEBUG(("Packet from unknown connection, length %d",
1302 packet = silc_calloc(1, sizeof(*packet));
1303 packet->packetdata = silc_calloc(1, sizeof(*packet->packetdata));
1304 packet->packetdata->buffer = silc_buffer_copy(sock->inbuf);
1305 packet->server = server;
1306 packet->sock = sock;
1308 packet->cipher = conn_data->receive_key;
1309 packet->hmac = conn_data->hmac;
1312 silc_buffer_clear(sock->inbuf);
1314 /* The packet is ready to be parsed now. However, this is unknown
1315 connection so we will parse the packet with timeout. */
1316 silc_task_register(server->timeout_queue, fd,
1317 silc_server_packet_parse,
1318 (void *)packet, 0, 100000,
1320 SILC_TASK_PRI_NORMAL);
1325 SILC_LOG_ERROR(("Weird, nothing happened - ignoring"));
1328 /* Checks MAC in the packet. Returns TRUE if MAC is Ok. This is called
1329 after packet has been totally decrypted and parsed. */
1331 static int silc_server_packet_check_mac(SilcServer server,
1332 SilcSocketConnection sock,
1335 SilcHmac hmac = NULL;
1336 unsigned char *hmac_key = NULL;
1337 unsigned int hmac_key_len = 0;
1338 unsigned int mac_len = 0;
1340 switch(sock->type) {
1341 case SILC_SOCKET_TYPE_CLIENT:
1342 if (sock->user_data) {
1343 hmac = ((SilcClientList *)sock->user_data)->hmac;
1344 hmac_key = ((SilcClientList *)sock->user_data)->hmac_key;
1345 hmac_key_len = ((SilcClientList *)sock->user_data)->hmac_key_len;
1348 case SILC_SOCKET_TYPE_SERVER:
1349 case SILC_SOCKET_TYPE_ROUTER:
1350 if (sock->user_data) {
1351 hmac = ((SilcServerList *)sock->user_data)->hmac;
1352 hmac_key = ((SilcServerList *)sock->user_data)->hmac_key;
1353 hmac_key_len = ((SilcServerList *)sock->user_data)->hmac_key_len;
1357 if (sock->user_data) {
1358 hmac = ((SilcIDListUnknown *)sock->user_data)->hmac;
1359 hmac_key = ((SilcIDListUnknown *)sock->user_data)->hmac_key;
1360 hmac_key_len = ((SilcIDListUnknown *)sock->user_data)->hmac_key_len;
1366 int headlen = buffer->data - buffer->head;
1367 unsigned char *packet_mac, mac[32];
1369 SILC_LOG_DEBUG(("Verifying MAC"));
1371 mac_len = hmac->hash->hash->hash_len;
1373 silc_buffer_push(buffer, headlen);
1375 /* Take mac from packet */
1376 packet_mac = buffer->tail;
1378 /* Make MAC and compare */
1379 memset(mac, 0, sizeof(mac));
1380 silc_hmac_make_with_key(hmac,
1381 buffer->data, buffer->len,
1382 hmac_key, hmac_key_len, mac);
1384 SILC_LOG_HEXDUMP(("PMAC"), packet_mac, mac_len);
1385 SILC_LOG_HEXDUMP(("CMAC"), mac, mac_len);
1387 if (memcmp(mac, packet_mac, mac_len)) {
1388 SILC_LOG_DEBUG(("MAC failed"));
1392 SILC_LOG_DEBUG(("MAC is Ok"));
1393 memset(mac, 0, sizeof(mac));
1395 silc_buffer_pull(buffer, headlen);
1401 /* Decrypts rest of the packet (after decrypting just the SILC header).
1402 After calling this function the packet is ready to be parsed by calling
1403 silc_packet_parse. */
1405 static int silc_server_packet_decrypt_rest(SilcServer server,
1406 SilcSocketConnection sock,
1409 SilcCipher session_key = NULL;
1410 SilcHmac hmac = NULL;
1411 unsigned int mac_len = 0;
1413 switch(sock->type) {
1414 case SILC_SOCKET_TYPE_CLIENT:
1415 if (sock->user_data) {
1416 session_key = ((SilcClientList *)sock->user_data)->receive_key;
1417 hmac = ((SilcClientList *)sock->user_data)->hmac;
1420 case SILC_SOCKET_TYPE_SERVER:
1421 case SILC_SOCKET_TYPE_ROUTER:
1422 if (sock->user_data) {
1423 session_key = ((SilcServerList *)sock->user_data)->receive_key;
1424 hmac = ((SilcServerList *)sock->user_data)->hmac;
1428 if (sock->user_data) {
1429 session_key = ((SilcIDListUnknown *)sock->user_data)->receive_key;
1430 hmac = ((SilcIDListUnknown *)sock->user_data)->hmac;
1437 /* Pull MAC from packet before decryption */
1439 mac_len = hmac->hash->hash->hash_len;
1440 if ((buffer->len - mac_len) > SILC_PACKET_MIN_LEN) {
1441 silc_buffer_push_tail(buffer, mac_len);
1443 SILC_LOG_DEBUG(("Bad MAC length in packet, packet dropped"));
1448 SILC_LOG_DEBUG(("Decrypting rest of the packet"));
1450 /* Decrypt rest of the packet */
1451 silc_buffer_pull(buffer, SILC_PACKET_MIN_HEADER_LEN - 2);
1452 silc_packet_decrypt(session_key, buffer, buffer->len);
1453 silc_buffer_push(buffer, SILC_PACKET_MIN_HEADER_LEN - 2);
1455 SILC_LOG_HEXDUMP(("Fully decrypted packet, len %d", buffer->len),
1456 buffer->data, buffer->len);
1462 /* Decrypts rest of the SILC Packet header that has been decrypted partly
1463 already. This decrypts the padding of the packet also. After calling
1464 this function the packet is ready to be parsed by calling function
1465 silc_packet_parse. */
1467 static int silc_server_packet_decrypt_rest_special(SilcServer server,
1468 SilcSocketConnection sock,
1471 SilcCipher session_key = NULL;
1472 SilcHmac hmac = NULL;
1473 unsigned int mac_len = 0;
1475 switch(sock->type) {
1476 case SILC_SOCKET_TYPE_CLIENT:
1477 if (sock->user_data) {
1478 session_key = ((SilcClientList *)sock->user_data)->receive_key;
1479 hmac = ((SilcClientList *)sock->user_data)->hmac;
1482 case SILC_SOCKET_TYPE_SERVER:
1483 case SILC_SOCKET_TYPE_ROUTER:
1484 if (sock->user_data) {
1485 session_key = ((SilcServerList *)sock->user_data)->receive_key;
1486 hmac = ((SilcServerList *)sock->user_data)->hmac;
1490 if (sock->user_data) {
1491 session_key = ((SilcIDListUnknown *)sock->user_data)->receive_key;
1492 hmac = ((SilcIDListUnknown *)sock->user_data)->hmac;
1496 /* Decrypt rest of the header plus padding */
1498 unsigned short truelen, len1, len2, padlen;
1500 /* Pull MAC from packet before decryption */
1502 mac_len = hmac->hash->hash->hash_len;
1503 if ((buffer->len - mac_len) > SILC_PACKET_MIN_LEN) {
1504 silc_buffer_push_tail(buffer, mac_len);
1506 SILC_LOG_DEBUG(("Bad MAC length in packet, packet dropped"));
1511 SILC_LOG_DEBUG(("Decrypting rest of the header"));
1513 SILC_GET16_MSB(len1, &buffer->data[4]);
1514 SILC_GET16_MSB(len2, &buffer->data[6]);
1516 truelen = SILC_PACKET_HEADER_LEN + len1 + len2;
1517 padlen = SILC_PACKET_PADLEN(truelen);
1518 len1 = (truelen + padlen) - (SILC_PACKET_MIN_HEADER_LEN - 2);
1520 silc_buffer_pull(buffer, SILC_PACKET_MIN_HEADER_LEN - 2);
1521 silc_packet_decrypt(session_key, buffer, len1);
1522 silc_buffer_push(buffer, SILC_PACKET_MIN_HEADER_LEN - 2);
1528 /* Parses whole packet, received earlier. This packet is usually received
1531 SILC_TASK_CALLBACK(silc_server_packet_parse)
1533 SilcServerInternalPacket *packet = (SilcServerInternalPacket *)context;
1534 SilcServer server = packet->server;
1535 SilcSocketConnection sock = packet->sock;
1536 SilcBuffer buffer = packet->packetdata->buffer;
1539 SILC_LOG_DEBUG(("Start"));
1541 /* Decrypt start of the packet header */
1543 silc_packet_decrypt(packet->cipher, buffer, SILC_PACKET_MIN_HEADER_LEN);
1545 /* If the packet type is not any special type lets decrypt rest
1546 of the packet here. */
1547 if (buffer->data[3] != SILC_PACKET_CHANNEL_MESSAGE &&
1548 buffer->data[3] != SILC_PACKET_PRIVATE_MESSAGE) {
1550 /* Normal packet, decrypt rest of the packet */
1551 if (!silc_server_packet_decrypt_rest(server, sock, buffer))
1554 /* Parse the packet. Packet type is returned. */
1555 ret = silc_packet_parse(packet->packetdata);
1556 if (ret == SILC_PACKET_NONE)
1560 if (!silc_server_packet_check_mac(server, sock, buffer))
1563 /* If private message key is not set for private message it is
1564 handled as normal packet. Go back up. */
1565 if (buffer->data[3] == SILC_PACKET_PRIVATE_MESSAGE &&
1566 !(buffer->data[2] & SILC_PACKET_FLAG_PRIVMSG_KEY))
1569 /* Packet requires special handling, decrypt rest of the header.
1570 This only decrypts. This does not do any MAC checking, it must
1571 be done individually later when doing the special processing. */
1572 silc_server_packet_decrypt_rest_special(server, sock, buffer);
1574 /* Parse the packet header in special way as this is "special"
1576 ret = silc_packet_parse_special(packet->packetdata);
1577 if (ret == SILC_PACKET_NONE)
1581 /* Parse the incoming packet type */
1582 silc_server_packet_parse_type(server, sock, packet->packetdata);
1585 silc_buffer_clear(sock->inbuf);
1586 // silc_buffer_free(packetdata->packetdata->buffer);
1587 silc_free(packet->packetdata);
1591 /* Parses the packet type and calls what ever routines the packet type
1592 requires. This is done for all incoming packets. */
1594 void silc_server_packet_parse_type(SilcServer server,
1595 SilcSocketConnection sock,
1596 SilcPacketContext *packet)
1598 SilcBuffer buffer = packet->buffer;
1599 SilcPacketType type = packet->type;
1601 SILC_LOG_DEBUG(("Parsing packet type %d", type));
1603 /* Parse the packet type */
1605 case SILC_PACKET_DISCONNECT:
1606 SILC_LOG_DEBUG(("Disconnect packet"));
1608 case SILC_PACKET_SUCCESS:
1610 * Success received for something. For now we can have only
1611 * one protocol for connection executing at once hence this
1612 * success message is for whatever protocol is executing currently.
1614 SILC_LOG_DEBUG(("Success packet"));
1615 if (sock->protocol) {
1616 sock->protocol->execute(server->timeout_queue, 0,
1617 sock->protocol, sock->sock, 0, 0);
1620 case SILC_PACKET_FAILURE:
1621 SILC_LOG_DEBUG(("Failure packet"));
1623 case SILC_PACKET_REJECT:
1624 SILC_LOG_DEBUG(("Reject packet"));
1631 case SILC_PACKET_CHANNEL_MESSAGE:
1633 * Received channel message. Channel messages are special packets
1634 * (although probably most common ones) hence they are handled
1637 SILC_LOG_DEBUG(("Channel Message packet"));
1638 silc_server_channel_message(server, sock, packet);
1641 case SILC_PACKET_CHANNEL_KEY:
1643 * Received key for channel. As channels are created by the router
1644 * the keys are as well. We will distribute the key to all of our
1645 * locally connected clients on the particular channel. Router
1646 * never receives this channel and thus is ignored.
1648 SILC_LOG_DEBUG(("Channel Key packet"));
1649 silc_server_channel_key(server, sock, packet);
1655 case SILC_PACKET_COMMAND:
1658 * Recived command. Allocate command context and execute the command.
1660 SilcServerCommandContext ctx;
1662 SILC_LOG_DEBUG(("Command packet"));
1664 /* Router cannot send command packet */
1665 if (sock->type == SILC_SOCKET_TYPE_ROUTER)
1668 /* Allocate command context. This must be free'd by the
1669 command routine receiving it. */
1670 ctx = silc_calloc(1, sizeof(*ctx));
1671 ctx->server = server;
1673 ctx->packet = packet; /* Save original packet */
1675 /* Parse the command payload in the packet */
1676 ctx->payload = silc_command_parse_payload(buffer);
1677 if (!ctx->payload) {
1678 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
1683 /* Execute command. If this fails the packet is dropped. */
1684 SILC_SERVER_COMMAND_EXEC(ctx);
1685 silc_buffer_free(buffer);
1689 case SILC_PACKET_COMMAND_REPLY:
1691 * Received command reply packet. Servers never send commands thus
1692 * they don't receive command reply packets either, except in cases
1693 * where server has forwarded command packet coming from client.
1694 * This must be the case here or we will ignore the packet.
1696 SILC_LOG_DEBUG(("Command Reply packet"));
1697 silc_server_packet_relay_command_reply(server, sock, packet);
1701 * Private Message packets
1703 case SILC_PACKET_PRIVATE_MESSAGE:
1705 * Received private message packet. The packet is coming from either
1708 SILC_LOG_DEBUG(("Private Message packet"));
1709 silc_server_private_message(server, sock, packet);
1712 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1716 * Key Exchange protocol packets
1718 case SILC_PACKET_KEY_EXCHANGE:
1719 SILC_LOG_DEBUG(("KE packet"));
1720 if (sock->protocol && sock->protocol->protocol->type
1721 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1723 SilcServerKEInternalContext *proto_ctx =
1724 (SilcServerKEInternalContext *)sock->protocol->context;
1726 proto_ctx->packet = buffer;
1728 /* Let the protocol handle the packet */
1729 sock->protocol->execute(server->timeout_queue, 0,
1730 sock->protocol, sock->sock, 0, 100000);
1732 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1733 "protocol active, packet dropped."));
1735 /* XXX Trigger KE protocol?? Rekey actually, maybe. */
1739 case SILC_PACKET_KEY_EXCHANGE_1:
1740 SILC_LOG_DEBUG(("KE 1 packet"));
1741 if (sock->protocol && sock->protocol->protocol->type
1742 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1744 SilcServerKEInternalContext *proto_ctx =
1745 (SilcServerKEInternalContext *)sock->protocol->context;
1747 if (proto_ctx->packet)
1748 silc_buffer_free(proto_ctx->packet);
1750 proto_ctx->packet = buffer;
1751 proto_ctx->dest_id_type = packet->src_id_type;
1752 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
1754 /* Let the protocol handle the packet */
1755 sock->protocol->execute(server->timeout_queue, 0,
1756 sock->protocol, sock->sock,
1759 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1760 "protocol active, packet dropped."));
1764 case SILC_PACKET_KEY_EXCHANGE_2:
1765 SILC_LOG_DEBUG(("KE 2 packet"));
1766 if (sock->protocol && sock->protocol->protocol->type
1767 == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1769 SilcServerKEInternalContext *proto_ctx =
1770 (SilcServerKEInternalContext *)sock->protocol->context;
1772 if (proto_ctx->packet)
1773 silc_buffer_free(proto_ctx->packet);
1775 proto_ctx->packet = buffer;
1776 proto_ctx->dest_id_type = packet->src_id_type;
1777 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
1779 /* Let the protocol handle the packet */
1780 sock->protocol->execute(server->timeout_queue, 0,
1781 sock->protocol, sock->sock,
1784 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1785 "protocol active, packet dropped."));
1789 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1790 /* If we receive this packet we will send to the other end information
1791 about our mandatory authentication method for the connection.
1792 This packet maybe received at any time. */
1795 * Connection Authentication protocol packets
1797 case SILC_PACKET_CONNECTION_AUTH:
1798 /* Start of the authentication protocol. We receive here the
1799 authentication data and will verify it. */
1800 SILC_LOG_DEBUG(("Connection auth packet"));
1801 if (sock->protocol && sock->protocol->protocol->type
1802 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
1804 SilcServerConnAuthInternalContext *proto_ctx =
1805 (SilcServerConnAuthInternalContext *)sock->protocol->context;
1807 proto_ctx->packet = buffer;
1809 /* Let the protocol handle the packet */
1810 sock->protocol->execute(server->timeout_queue, 0,
1811 sock->protocol, sock->sock, 0, 0);
1813 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
1814 "protocol active, packet dropped."));
1818 case SILC_PACKET_NEW_ID:
1820 * Received New ID packet. This includes some new ID that has been
1821 * created. It may be for client, server or channel. This is the way
1822 * to distribute information about new registered entities in the
1825 SILC_LOG_DEBUG(("New ID packet"));
1826 silc_server_new_id(server, sock, packet);
1829 case SILC_PACKET_NEW_CLIENT:
1831 * Received new client packet. This includes client information that
1832 * we will use to create initial client ID. After creating new
1833 * ID we will send it to the client.
1835 SILC_LOG_DEBUG(("New Client packet"));
1836 silc_server_new_client(server, sock, packet);
1839 case SILC_PACKET_NEW_SERVER:
1841 * Received new server packet. This includes Server ID and some other
1842 * information that we may save. This is after server as connected to us.
1844 SILC_LOG_DEBUG(("New Server packet"));
1845 silc_server_new_server(server, sock, packet);
1849 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
1855 /* Internal routine that sends packet or marks packet to be sent. This
1856 is used directly only in special cases. Normal cases should use
1857 silc_server_packet_send. Returns < 0 error. */
1859 static int silc_server_packet_send_real(SilcServer server,
1860 SilcSocketConnection sock,
1863 /* Send now if forced to do so */
1864 if (force_send == TRUE) {
1866 SILC_LOG_DEBUG(("Forcing packet send, packet sent immediately"));
1867 ret = silc_packet_write(sock->sock, sock->outbuf);
1869 silc_buffer_clear(sock->outbuf);
1872 SILC_LOG_ERROR(("Could not write, packet dropped"));
1874 silc_buffer_clear(sock->outbuf);
1878 SILC_LOG_DEBUG(("Could not force the send, packet put to queue"));
1881 SILC_LOG_DEBUG(("Packet in queue"));
1883 /* Mark that there is some outgoing data available for this connection.
1884 This call sets the connection both for input and output (the input
1885 is set always and this call keeps the input setting, actually).
1886 Actual data sending is performed by silc_server_packet_process. */
1887 SILC_SET_CONNECTION_FOR_OUTPUT(sock->sock);
1889 /* Mark to socket that data is pending in outgoing buffer. This flag
1890 is needed if new data is added to the buffer before the earlier
1891 put data is sent to the network. */
1892 SILC_SET_OUTBUF_PENDING(sock);
1897 /* Prepare outgoing data buffer for packet sending. This is internal
1898 routine and must always be called before sending any packets out. */
1900 static void silc_server_packet_send_prepare(SilcServer server,
1901 SilcSocketConnection sock,
1902 unsigned int header_len,
1903 unsigned int padlen,
1904 unsigned int data_len)
1908 totlen = header_len + padlen + data_len;
1910 /* Prepare the outgoing buffer for packet sending. */
1911 if (!sock->outbuf) {
1912 /* Allocate new buffer. This is done only once per connection. */
1913 SILC_LOG_DEBUG(("Allocating outgoing data buffer"));
1915 sock->outbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE);
1916 silc_buffer_pull_tail(sock->outbuf, totlen);
1917 silc_buffer_pull(sock->outbuf, header_len + padlen);
1919 if (SILC_IS_OUTBUF_PENDING(sock)) {
1920 /* There is some pending data in the buffer. */
1922 if ((sock->outbuf->end - sock->outbuf->tail) < data_len) {
1923 SILC_LOG_DEBUG(("Reallocating outgoing data buffer"));
1924 /* XXX: not done yet */
1926 oldlen = sock->outbuf->len;
1927 silc_buffer_pull_tail(sock->outbuf, totlen);
1928 silc_buffer_pull(sock->outbuf, header_len + padlen + oldlen);
1930 /* Buffer is free for use */
1931 silc_buffer_clear(sock->outbuf);
1932 silc_buffer_pull_tail(sock->outbuf, totlen);
1933 silc_buffer_pull(sock->outbuf, header_len + padlen);
1938 /* Assembles a new packet to be sent out to network. This doesn't actually
1939 send the packet but creates the packet and fills the outgoing data
1940 buffer and marks the packet ready to be sent to network. However, If
1941 argument force_send is TRUE the packet is sent immediately and not put
1942 to queue. Normal case is that the packet is not sent immediately. */
1944 void silc_server_packet_send(SilcServer server,
1945 SilcSocketConnection sock,
1946 SilcPacketType type,
1947 SilcPacketFlags flags,
1948 unsigned char *data,
1949 unsigned int data_len,
1952 void *dst_id = NULL;
1953 SilcIdType dst_id_type = SILC_ID_NONE;
1955 /* Get data used in the packet sending, keys and stuff */
1956 switch(sock->type) {
1957 case SILC_SOCKET_TYPE_CLIENT:
1958 if (((SilcClientList *)sock->user_data)->id) {
1959 dst_id = ((SilcClientList *)sock->user_data)->id;
1960 dst_id_type = SILC_ID_CLIENT;
1963 case SILC_SOCKET_TYPE_SERVER:
1964 case SILC_SOCKET_TYPE_ROUTER:
1965 if (((SilcServerList *)sock->user_data)->id) {
1966 dst_id = ((SilcServerList *)sock->user_data)->id;
1967 dst_id_type = SILC_ID_SERVER;
1974 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
1975 dst_id_type, data, data_len, force_send);
1978 /* Assembles a new packet to be sent out to network. This doesn't actually
1979 send the packet but creates the packet and fills the outgoing data
1980 buffer and marks the packet ready to be sent to network. However, If
1981 argument force_send is TRUE the packet is sent immediately and not put
1982 to queue. Normal case is that the packet is not sent immediately.
1983 Destination information is sent as argument for this function. */
1985 void silc_server_packet_send_dest(SilcServer server,
1986 SilcSocketConnection sock,
1987 SilcPacketType type,
1988 SilcPacketFlags flags,
1990 SilcIdType dst_id_type,
1991 unsigned char *data,
1992 unsigned int data_len,
1995 SilcPacketContext packetdata;
1996 SilcCipher cipher = NULL;
1997 SilcHmac hmac = NULL;
1998 unsigned char *hmac_key = NULL;
1999 unsigned int hmac_key_len = 0;
2000 unsigned char mac[32];
2001 unsigned int mac_len = 0;
2002 unsigned char *dst_id_data = NULL;
2003 unsigned int dst_id_len = 0;
2005 SILC_LOG_DEBUG(("Sending packet, type %d", type));
2007 /* Get data used in the packet sending, keys and stuff */
2008 switch(sock->type) {
2009 case SILC_SOCKET_TYPE_CLIENT:
2010 if (sock->user_data) {
2011 cipher = ((SilcClientList *)sock->user_data)->send_key;
2012 hmac = ((SilcClientList *)sock->user_data)->hmac;
2014 mac_len = hmac->hash->hash->hash_len;
2015 hmac_key = ((SilcClientList *)sock->user_data)->hmac_key;
2016 hmac_key_len = ((SilcClientList *)sock->user_data)->hmac_key_len;
2020 case SILC_SOCKET_TYPE_SERVER:
2021 case SILC_SOCKET_TYPE_ROUTER:
2022 if (sock->user_data) {
2023 cipher = ((SilcServerList *)sock->user_data)->send_key;
2024 hmac = ((SilcServerList *)sock->user_data)->hmac;
2026 mac_len = hmac->hash->hash->hash_len;
2027 hmac_key = ((SilcServerList *)sock->user_data)->hmac_key;
2028 hmac_key_len = ((SilcServerList *)sock->user_data)->hmac_key_len;
2033 if (sock->user_data) {
2034 /* We don't know what type of connection this is thus it must
2035 be in authentication phase. */
2036 cipher = ((SilcIDListUnknown *)sock->user_data)->send_key;
2037 hmac = ((SilcIDListUnknown *)sock->user_data)->hmac;
2039 mac_len = hmac->hash->hash->hash_len;
2040 hmac_key = ((SilcIDListUnknown *)sock->user_data)->hmac_key;
2041 hmac_key_len = ((SilcIDListUnknown *)sock->user_data)->hmac_key_len;
2048 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
2049 dst_id_len = silc_id_get_len(dst_id_type);
2052 /* Set the packet context pointers */
2053 packetdata.type = type;
2054 packetdata.flags = flags;
2055 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
2056 packetdata.src_id_len = SILC_ID_SERVER_LEN;
2057 packetdata.src_id_type = server->id_type;
2058 packetdata.dst_id = dst_id_data;
2059 packetdata.dst_id_len = dst_id_len;
2060 packetdata.dst_id_type = dst_id_type;
2061 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
2062 packetdata.src_id_len + dst_id_len;
2063 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
2064 packetdata.rng = server->rng;
2066 /* Prepare outgoing data buffer for packet sending */
2067 silc_server_packet_send_prepare(server, sock,
2068 SILC_PACKET_HEADER_LEN +
2069 packetdata.src_id_len +
2070 packetdata.dst_id_len,
2074 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
2076 packetdata.buffer = sock->outbuf;
2078 /* Put the data to the buffer */
2079 if (data && data_len)
2080 silc_buffer_put(sock->outbuf, data, data_len);
2082 /* Create the outgoing packet */
2083 silc_packet_assemble(&packetdata);
2085 /* Compute MAC of the packet */
2087 silc_hmac_make_with_key(hmac, sock->outbuf->data, sock->outbuf->len,
2088 hmac_key, hmac_key_len, mac);
2089 silc_buffer_put_tail(sock->outbuf, mac, mac_len);
2090 memset(mac, 0, sizeof(mac));
2093 /* Encrypt the packet */
2095 silc_packet_encrypt(cipher, sock->outbuf, sock->outbuf->len);
2097 /* Pull MAC into the visible data area */
2099 silc_buffer_pull_tail(sock->outbuf, mac_len);
2101 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
2102 sock->outbuf->data, sock->outbuf->len);
2104 /* Now actually send the packet */
2105 silc_server_packet_send_real(server, sock, force_send);
2107 if (packetdata.src_id)
2108 silc_free(packetdata.src_id);
2109 if (packetdata.dst_id)
2110 silc_free(packetdata.dst_id);
2113 /* Forwards packet. Packets sent with this function will be marked as
2114 forwarded (in the SILC header flags) so that the receiver knows that
2115 we have forwarded the packet to it. Forwarded packets are handled
2116 specially by the receiver as they are not destined to the receiver
2117 originally. However, the receiver knows this because the forwarded
2118 flag has been set (and the flag is authenticated). */
2120 void silc_server_packet_forward(SilcServer server,
2121 SilcSocketConnection sock,
2122 unsigned char *data, unsigned int data_len,
2125 SilcCipher cipher = NULL;
2126 SilcHmac hmac = NULL;
2127 unsigned char *hmac_key = NULL;
2128 unsigned int hmac_key_len = 0;
2129 unsigned char mac[32];
2130 unsigned int mac_len = 0;
2132 SILC_LOG_DEBUG(("Forwarding packet"));
2134 /* Get data used in the packet sending, keys and stuff */
2135 switch(sock->type) {
2136 case SILC_SOCKET_TYPE_CLIENT:
2137 if (sock->user_data) {
2138 cipher = ((SilcClientList *)sock->user_data)->send_key;
2139 hmac = ((SilcClientList *)sock->user_data)->hmac;
2141 mac_len = hmac->hash->hash->hash_len;
2142 hmac_key = ((SilcClientList *)sock->user_data)->hmac_key;
2143 hmac_key_len = ((SilcClientList *)sock->user_data)->hmac_key_len;
2147 case SILC_SOCKET_TYPE_SERVER:
2148 case SILC_SOCKET_TYPE_ROUTER:
2149 if (sock->user_data) {
2150 cipher = ((SilcServerList *)sock->user_data)->send_key;
2151 hmac = ((SilcServerList *)sock->user_data)->hmac;
2153 mac_len = hmac->hash->hash->hash_len;
2154 hmac_key = ((SilcServerList *)sock->user_data)->hmac_key;
2155 hmac_key_len = ((SilcServerList *)sock->user_data)->hmac_key_len;
2160 /* We won't forward to unknown destination - keys must exist with
2161 the destination before forwarding. */
2165 /* Prepare outgoing data buffer for packet sending */
2166 silc_server_packet_send_prepare(server, sock, 0, 0, data_len);
2168 /* Mungle the packet flags and add the FORWARDED flag */
2170 data[2] |= (unsigned char)SILC_PACKET_FLAG_FORWARDED;
2172 /* Put the data to the buffer */
2173 if (data && data_len)
2174 silc_buffer_put(sock->outbuf, data, data_len);
2176 /* Compute MAC of the packet */
2178 silc_hmac_make_with_key(hmac, sock->outbuf->data, sock->outbuf->len,
2179 hmac_key, hmac_key_len, mac);
2180 silc_buffer_put_tail(sock->outbuf, mac, mac_len);
2181 memset(mac, 0, sizeof(mac));
2184 /* Encrypt the packet */
2186 silc_packet_encrypt(cipher, sock->outbuf, sock->outbuf->len);
2188 /* Pull MAC into the visible data area */
2190 silc_buffer_pull_tail(sock->outbuf, mac_len);
2192 SILC_LOG_HEXDUMP(("Forwarded packet, len %d", sock->outbuf->len),
2193 sock->outbuf->data, sock->outbuf->len);
2195 /* Now actually send the packet */
2196 silc_server_packet_send_real(server, sock, force_send);
2199 /* This routine is used by the server to send packets to channel. The
2200 packet sent with this function is distributed to all clients on
2201 the channel. Usually this is used to send notify messages to the
2202 channel, things like notify about new user joining to the channel. */
2204 void silc_server_packet_send_to_channel(SilcServer server,
2205 SilcChannelList *channel,
2206 unsigned char *data,
2207 unsigned int data_len,
2211 SilcSocketConnection sock = NULL;
2212 SilcPacketContext packetdata;
2213 SilcClientList *client = NULL;
2214 SilcServerList **routed = NULL;
2215 unsigned int routed_count = 0;
2216 unsigned char *hmac_key = NULL;
2217 unsigned int hmac_key_len = 0;
2218 unsigned char mac[32];
2219 unsigned int mac_len = 0;
2224 SILC_LOG_DEBUG(("Sending packet to channel"));
2227 for (i = 0; i < 16; i++)
2228 channel->iv[i] = silc_rng_get_byte(server->rng);
2230 /* Encode the channel payload */
2231 payload = silc_channel_encode_payload(0, "", data_len, data,
2232 16, channel->iv, server->rng);
2236 /* Encrypt payload of the packet. This is encrypted with the
2238 channel->channel_key->cipher->encrypt(channel->channel_key->context,
2239 payload->data, payload->data,
2240 payload->len - 16, /* -IV_LEN */
2243 /* Set the packet context pointers. */
2244 packetdata.flags = 0;
2245 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
2246 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
2247 packetdata.src_id_len = SILC_ID_SERVER_LEN;
2248 packetdata.src_id_type = SILC_ID_SERVER;
2249 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2250 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
2251 packetdata.dst_id_type = SILC_ID_CHANNEL;
2252 packetdata.rng = server->rng;
2253 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
2254 packetdata.src_id_len +
2255 packetdata.dst_id_len));
2257 /* If there are global users in the channel we will send the message
2258 first to our router for further routing. */
2259 if (server->server_type == SILC_SERVER && !server->standalone &&
2260 channel->global_users) {
2261 SilcServerList *router;
2263 /* Get data used in packet header encryption, keys and stuff. */
2264 router = server->id_entry->router;
2265 sock = (SilcSocketConnection)router->connection;
2266 cipher = router->send_key;
2267 hmac = router->hmac;
2268 mac_len = hmac->hash->hash->hash_len;
2269 hmac_key = router->hmac_key;
2270 hmac_key_len = router->hmac_key_len;
2272 SILC_LOG_DEBUG(("Sending packet to router for routing"));
2274 packetdata.truelen = payload->len + SILC_PACKET_HEADER_LEN +
2275 packetdata.src_id_len + packetdata.dst_id_len;
2277 /* Prepare outgoing data buffer for packet sending */
2278 silc_server_packet_send_prepare(server, sock,
2279 SILC_PACKET_HEADER_LEN +
2280 packetdata.src_id_len +
2281 packetdata.dst_id_len,
2284 packetdata.buffer = sock->outbuf;
2286 /* Put the original packet into the buffer. */
2287 silc_buffer_put(sock->outbuf, payload->data, payload->len);
2289 /* Create the outgoing packet */
2290 silc_packet_assemble(&packetdata);
2292 /* Compute MAC of the packet. MAC is computed from the header,
2293 padding and the relayed packet. */
2294 silc_hmac_make_with_key(hmac, sock->outbuf->data, sock->outbuf->len,
2295 hmac_key, hmac_key_len, mac);
2296 silc_buffer_put_tail(sock->outbuf, mac, mac_len);
2297 memset(mac, 0, sizeof(mac));
2299 /* Encrypt the header and padding of the packet. This is encrypted
2300 with normal session key shared with the client. */
2301 silc_packet_encrypt(cipher, sock->outbuf, SILC_PACKET_HEADER_LEN +
2302 packetdata.src_id_len + packetdata.dst_id_len +
2305 /* Pull MAC into the visible data area */
2306 silc_buffer_pull_tail(sock->outbuf, mac_len);
2308 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
2309 sock->outbuf->data, sock->outbuf->len);
2311 /* Now actually send the packet */
2312 silc_server_packet_send_real(server, sock, force_send);
2315 /* Send the message to clients on the channel's client list. */
2316 for (i = 0; i < channel->user_list_count; i++) {
2317 client = channel->user_list[i].client;
2319 /* If client has router set it is not locally connected client and
2320 we will route the message to the router set in the client. */
2321 if (client && client->router && server->server_type == SILC_ROUTER) {
2324 /* Check if we have sent the packet to this route already */
2325 for (k = 0; k < routed_count; k++)
2326 if (routed[k] == client->router)
2329 /* Get data used in packet header encryption, keys and stuff. */
2330 sock = (SilcSocketConnection)client->router->connection;
2331 cipher = client->router->send_key;
2332 hmac = client->router->hmac;
2333 mac_len = hmac->hash->hash->hash_len;
2334 hmac_key = client->router->hmac_key;
2335 hmac_key_len = client->router->hmac_key_len;
2337 packetdata.truelen = payload->len + SILC_PACKET_HEADER_LEN +
2338 packetdata.src_id_len + packetdata.dst_id_len;
2340 /* Prepare outgoing data buffer for packet sending */
2341 silc_server_packet_send_prepare(server, sock,
2342 SILC_PACKET_HEADER_LEN +
2343 packetdata.src_id_len +
2344 packetdata.dst_id_len,
2347 packetdata.buffer = sock->outbuf;
2349 /* Put the encrypted payload data into the buffer. */
2350 silc_buffer_put(sock->outbuf, payload->data, payload->len);
2352 /* Create the outgoing packet */
2353 silc_packet_assemble(&packetdata);
2355 /* Compute MAC of the packet. MAC is computed from the header,
2356 padding and the relayed packet. */
2357 silc_hmac_make_with_key(hmac, sock->outbuf->data, sock->outbuf->len,
2358 hmac_key, hmac_key_len, mac);
2359 silc_buffer_put_tail(sock->outbuf, mac, mac_len);
2360 memset(mac, 0, sizeof(mac));
2362 /* Encrypt the header and padding of the packet. This is encrypted
2363 with normal session key shared with the client. */
2364 silc_packet_encrypt(cipher, sock->outbuf, SILC_PACKET_HEADER_LEN +
2365 packetdata.src_id_len + packetdata.dst_id_len +
2368 /* Pull MAC into the visible data area */
2369 silc_buffer_pull_tail(sock->outbuf, mac_len);
2371 SILC_LOG_HEXDUMP(("Packet to channel, len %d", sock->outbuf->len),
2372 sock->outbuf->data, sock->outbuf->len);
2374 /* Now actually send the packet */
2375 silc_server_packet_send_real(server, sock, force_send);
2377 /* We want to make sure that the packet is routed to same router
2378 only once. Mark this route as sent route. */
2380 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
2381 routed[k] = client->router;
2387 /* Send to locally connected client */
2390 /* XXX Check client's mode on the channel. */
2392 /* Get data used in packet header encryption, keys and stuff. */
2393 sock = (SilcSocketConnection)client->connection;
2394 cipher = client->send_key;
2395 hmac = client->hmac;
2396 mac_len = hmac->hash->hash->hash_len;
2397 hmac_key = client->hmac_key;
2398 hmac_key_len = client->hmac_key_len;
2400 packetdata.truelen = payload->len + SILC_PACKET_HEADER_LEN +
2401 packetdata.src_id_len + packetdata.dst_id_len;
2403 /* Prepare outgoing data buffer for packet sending */
2404 silc_server_packet_send_prepare(server, sock,
2405 SILC_PACKET_HEADER_LEN +
2406 packetdata.src_id_len +
2407 packetdata.dst_id_len,
2410 packetdata.buffer = sock->outbuf;
2412 /* Put the encrypted payload data into the buffer. */
2413 silc_buffer_put(sock->outbuf, payload->data, payload->len);
2415 /* Create the outgoing packet */
2416 silc_packet_assemble(&packetdata);
2418 /* Compute MAC of the packet. MAC is computed from the header,
2419 padding and the relayed packet. */
2420 silc_hmac_make_with_key(hmac, sock->outbuf->data, sock->outbuf->len,
2421 hmac_key, hmac_key_len, mac);
2422 silc_buffer_put_tail(sock->outbuf, mac, mac_len);
2423 memset(mac, 0, sizeof(mac));
2425 /* Encrypt the header and padding of the packet. This is encrypted
2426 with normal session key shared with the client. */
2427 silc_packet_encrypt(cipher, sock->outbuf, SILC_PACKET_HEADER_LEN +
2428 packetdata.src_id_len + packetdata.dst_id_len +
2431 /* Pull MAC into the visible data area */
2432 silc_buffer_pull_tail(sock->outbuf, mac_len);
2434 SILC_LOG_HEXDUMP(("Packet to channel, len %d", sock->outbuf->len),
2435 sock->outbuf->data, sock->outbuf->len);
2437 /* Now actually send the packet */
2438 silc_server_packet_send_real(server, sock, force_send);
2444 silc_free(packetdata.src_id);
2445 silc_free(packetdata.dst_id);
2446 silc_buffer_free(payload);
2449 /* This routine is explicitly used to relay messages to some channel.
2450 Packets sent with this function we have received earlier and are
2451 totally encrypted. This just sends the packet to all clients on
2452 the channel. If the sender of the packet is someone on the channel
2453 the message will not be sent to that client. The SILC Packet header
2454 is encrypted with the session key shared between us and the client.
2455 MAC is also computed before encrypting the header. Rest of the
2456 packet will be untouched. */
2458 void silc_server_packet_relay_to_channel(SilcServer server,
2459 SilcSocketConnection sender_sock,
2460 SilcChannelList *channel,
2462 SilcIdType sender_type,
2463 unsigned char *data,
2464 unsigned int data_len,
2467 int i, found = FALSE;
2468 SilcSocketConnection sock = NULL;
2469 SilcPacketContext packetdata;
2470 SilcClientList *client = NULL;
2471 SilcServerList **routed = NULL;
2472 unsigned int routed_count = 0;
2473 unsigned char *hmac_key = NULL;
2474 unsigned int hmac_key_len = 0;
2475 unsigned char mac[32];
2476 unsigned int mac_len = 0;
2480 SILC_LOG_DEBUG(("Relaying packet to channel"));
2482 SILC_LOG_HEXDUMP(("XXX %d", data_len), data, data_len);
2484 /* Set the packet context pointers. */
2485 packetdata.flags = 0;
2486 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
2487 packetdata.src_id = silc_id_id2str(sender, sender_type);
2488 packetdata.src_id_len = silc_id_get_len(sender_type);
2489 packetdata.src_id_type = sender_type;
2490 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2491 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
2492 packetdata.dst_id_type = SILC_ID_CHANNEL;
2493 packetdata.rng = server->rng;
2494 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
2495 packetdata.src_id_len +
2496 packetdata.dst_id_len));
2498 /* If there are global users in the channel we will send the message
2499 first to our router for further routing. */
2500 if (server->server_type == SILC_SERVER && !server->standalone &&
2501 channel->global_users) {
2502 SilcServerList *router;
2504 router = server->id_entry->router;
2506 /* Check that the sender is not our router. */
2507 if (sender_sock != (SilcSocketConnection)router->connection) {
2509 /* Get data used in packet header encryption, keys and stuff. */
2510 sock = (SilcSocketConnection)router->connection;
2511 cipher = router->send_key;
2512 hmac = router->hmac;
2513 mac_len = hmac->hash->hash->hash_len;
2514 hmac_key = router->hmac_key;
2515 hmac_key_len = router->hmac_key_len;
2517 SILC_LOG_DEBUG(("Sending packet to router for routing"));
2519 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
2520 packetdata.src_id_len + packetdata.dst_id_len;
2522 /* Prepare outgoing data buffer for packet sending */
2523 silc_server_packet_send_prepare(server, sock,
2524 SILC_PACKET_HEADER_LEN +
2525 packetdata.src_id_len +
2526 packetdata.dst_id_len,
2529 packetdata.buffer = sock->outbuf;
2531 /* Put the original packet into the buffer. */
2532 silc_buffer_put(sock->outbuf, data, data_len);
2534 /* Create the outgoing packet */
2535 silc_packet_assemble(&packetdata);
2537 /* Compute MAC of the packet. MAC is computed from the header,
2538 padding and the relayed packet. */
2539 silc_hmac_make_with_key(hmac, sock->outbuf->data, sock->outbuf->len,
2540 hmac_key, hmac_key_len, mac);
2541 silc_buffer_put_tail(sock->outbuf, mac, mac_len);
2542 memset(mac, 0, sizeof(mac));
2544 /* Encrypt the header and padding of the packet. This is encrypted
2545 with normal session key shared with the client. */
2546 silc_packet_encrypt(cipher, sock->outbuf, SILC_PACKET_HEADER_LEN +
2547 packetdata.src_id_len + packetdata.dst_id_len +
2550 /* Pull MAC into the visible data area */
2551 silc_buffer_pull_tail(sock->outbuf, mac_len);
2553 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
2554 sock->outbuf->data, sock->outbuf->len);
2556 /* Now actually send the packet */
2557 silc_server_packet_send_real(server, sock, force_send);
2561 /* Send the message to clients on the channel's client list. */
2562 for (i = 0; i < channel->user_list_count; i++) {
2563 client = channel->user_list[i].client;
2567 /* If sender is one on the channel do not send it the packet. */
2568 if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
2573 /* If the client has set router it means that it is not locally
2574 connected client and we will route the packet further. */
2575 if (server->server_type == SILC_ROUTER && client->router) {
2578 /* Sender maybe server as well so we want to make sure that
2579 we won't send the message to the server it came from. */
2580 if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
2585 /* Check if we have sent the packet to this route already */
2586 for (k = 0; k < routed_count; k++)
2587 if (routed[k] == client->router)
2590 /* Get data used in packet header encryption, keys and stuff. */
2591 sock = (SilcSocketConnection)client->router->connection;
2592 cipher = client->router->send_key;
2593 hmac = client->router->hmac;
2594 mac_len = hmac->hash->hash->hash_len;
2595 hmac_key = client->router->hmac_key;
2596 hmac_key_len = client->router->hmac_key_len;
2598 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
2599 packetdata.src_id_len + packetdata.dst_id_len;
2601 /* Prepare outgoing data buffer for packet sending */
2602 silc_server_packet_send_prepare(server, sock,
2603 SILC_PACKET_HEADER_LEN +
2604 packetdata.src_id_len +
2605 packetdata.dst_id_len,
2608 packetdata.buffer = sock->outbuf;
2610 /* Put the original packet into the buffer. */
2611 silc_buffer_put(sock->outbuf, data, data_len);
2613 /* Create the outgoing packet */
2614 silc_packet_assemble(&packetdata);
2616 /* Compute MAC of the packet. MAC is computed from the header,
2617 padding and the relayed packet. */
2618 silc_hmac_make_with_key(hmac, sock->outbuf->data, sock->outbuf->len,
2619 hmac_key, hmac_key_len, mac);
2620 silc_buffer_put_tail(sock->outbuf, mac, mac_len);
2621 memset(mac, 0, sizeof(mac));
2623 /* Encrypt the header and padding of the packet. This is encrypted
2624 with normal session key shared with the client. */
2625 silc_packet_encrypt(cipher, sock->outbuf, SILC_PACKET_HEADER_LEN +
2626 packetdata.src_id_len + packetdata.dst_id_len +
2629 /* Pull MAC into the visible data area */
2630 silc_buffer_pull_tail(sock->outbuf, mac_len);
2632 SILC_LOG_HEXDUMP(("Packet to channel, len %d", sock->outbuf->len),
2633 sock->outbuf->data, sock->outbuf->len);
2635 /* Now actually send the packet */
2636 silc_server_packet_send_real(server, sock, force_send);
2638 /* We want to make sure that the packet is routed to same router
2639 only once. Mark this route as sent route. */
2641 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
2642 routed[k] = client->router;
2648 /* XXX Check client's mode on the channel. */
2650 /* Get data used in packet header encryption, keys and stuff. */
2651 sock = (SilcSocketConnection)client->connection;
2652 cipher = client->send_key;
2653 hmac = client->hmac;
2654 mac_len = hmac->hash->hash->hash_len;
2655 hmac_key = client->hmac_key;
2656 hmac_key_len = client->hmac_key_len;
2658 SILC_LOG_DEBUG(("Sending packet to client %s",
2659 sock->hostname ? sock->hostname : sock->ip));
2661 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
2662 packetdata.src_id_len + packetdata.dst_id_len;
2664 /* Prepare outgoing data buffer for packet sending */
2665 silc_server_packet_send_prepare(server, sock,
2666 SILC_PACKET_HEADER_LEN +
2667 packetdata.src_id_len +
2668 packetdata.dst_id_len,
2671 packetdata.buffer = sock->outbuf;
2673 /* Put the original packet into the buffer. */
2674 silc_buffer_put(sock->outbuf, data, data_len);
2676 /* Create the outgoing packet */
2677 silc_packet_assemble(&packetdata);
2679 /* Compute MAC of the packet. MAC is computed from the header,
2680 padding and the relayed packet. */
2681 silc_hmac_make_with_key(hmac, sock->outbuf->data, sock->outbuf->len,
2682 hmac_key, hmac_key_len, mac);
2683 silc_buffer_put_tail(sock->outbuf, mac, mac_len);
2684 memset(mac, 0, sizeof(mac));
2686 /* Encrypt the header and padding of the packet. This is encrypted
2687 with normal session key shared with the client. */
2688 silc_packet_encrypt(cipher, sock->outbuf, SILC_PACKET_HEADER_LEN +
2689 packetdata.src_id_len + packetdata.dst_id_len +
2692 /* Pull MAC into the visible data area */
2693 silc_buffer_pull_tail(sock->outbuf, mac_len);
2695 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
2696 sock->outbuf->data, sock->outbuf->len);
2698 /* Now actually send the packet */
2699 silc_server_packet_send_real(server, sock, force_send);
2703 silc_free(packetdata.src_id);
2704 silc_free(packetdata.dst_id);
2707 /* This function is used to send packets strictly to all local clients
2708 on a particular channel. This is used for example to distribute new
2709 channel key to all our locally connected clients on the channel.
2710 The packets are always encrypted with the session key shared between
2713 void silc_server_packet_send_local_channel(SilcServer server,
2714 SilcChannelList *channel,
2715 SilcPacketType type,
2716 SilcPacketFlags flags,
2717 unsigned char *data,
2718 unsigned int data_len,
2722 SilcClientList *client;
2723 SilcSocketConnection sock = NULL;
2725 SILC_LOG_DEBUG(("Start"));
2727 /* Send the message to clients on the channel's client list. */
2728 for (i = 0; i < channel->user_list_count; i++) {
2729 client = channel->user_list[i].client;
2732 sock = (SilcSocketConnection)client->connection;
2734 /* Send the packet to the client */
2735 silc_server_packet_send_dest(server, sock, type, flags, client->id,
2736 SILC_ID_CLIENT, data, data_len,
2742 /* Relays received command reply packet to the correct destination. The
2743 destination must be one of our locally connected client or the packet
2744 will be ignored. This is called when server has forwarded one of
2745 client's command request to router and router has now replied to the
2748 void silc_server_packet_relay_command_reply(SilcServer server,
2749 SilcSocketConnection sock,
2750 SilcPacketContext *packet)
2752 SilcBuffer buffer = packet->buffer;
2753 SilcClientList *client;
2755 SilcSocketConnection dst_sock;
2756 unsigned char mac[32];
2757 unsigned int mac_len = 0;
2759 SILC_LOG_DEBUG(("Start"));
2761 /* Source must be server or router */
2762 /* XXX: actually it must be only router */
2763 if (packet->src_id_type != SILC_ID_SERVER &&
2764 (sock->type != SILC_SOCKET_TYPE_SERVER ||
2765 sock->type != SILC_SOCKET_TYPE_ROUTER))
2768 /* Destination must be client */
2769 if (packet->dst_id_type != SILC_ID_CLIENT)
2772 /* Execute command reply locally for the command */
2773 silc_server_command_reply_process(server, sock, buffer);
2775 id = silc_id_str2id(packet->dst_id, SILC_ID_CLIENT);
2777 /* Destination must be one of ours */
2778 client = silc_idlist_find_client_by_id(server->local_list->clients, id);
2784 /* Relay the packet to the client */
2786 mac_len = client->hmac->hash->hash->hash_len;
2788 dst_sock = (SilcSocketConnection)client->connection;
2790 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2791 + packet->dst_id_len + packet->padlen);
2792 silc_server_packet_send_prepare(server, dst_sock, 0, 0, buffer->len);
2793 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
2795 /* Compute new HMAC */
2797 memset(mac, 0, sizeof(mac));
2798 silc_hmac_make_with_key(client->hmac,
2799 dst_sock->outbuf->data,
2800 dst_sock->outbuf->len,
2802 client->hmac_key_len,
2804 silc_buffer_put_tail(dst_sock->outbuf, mac, mac_len);
2805 memset(mac, 0, sizeof(mac));
2809 if (client && client->send_key)
2810 silc_packet_encrypt(client->send_key, dst_sock->outbuf, buffer->len);
2813 silc_buffer_pull_tail(dst_sock->outbuf, mac_len);
2815 /* Send the packet */
2816 silc_server_packet_send_real(server, dst_sock, FALSE);
2821 silc_buffer_free(buffer);
2824 /* Closes connection to socket connection */
2826 void silc_server_close_connection(SilcServer server,
2827 SilcSocketConnection sock)
2830 SILC_LOG_DEBUG(("Closing connection %d", sock->sock));
2832 /* We won't listen for this connection anymore */
2833 silc_schedule_unset_listen_fd(sock->sock);
2835 /* Unregister all tasks */
2836 silc_task_unregister_by_fd(server->io_queue, sock->sock);
2837 silc_task_unregister_by_fd(server->timeout_queue, sock->sock);
2839 /* Close the actual connection */
2840 silc_net_close_connection(sock->sock);
2841 server->sockets[sock->sock] = NULL;
2842 silc_socket_free(sock);
2845 /* Sends disconnect message to remote connection and disconnects the
2848 void silc_server_disconnect_remote(SilcServer server,
2849 SilcSocketConnection sock,
2850 const char *fmt, ...)
2853 unsigned char buf[4096];
2855 memset(buf, 0, sizeof(buf));
2857 vsprintf(buf, fmt, ap);
2860 SILC_LOG_DEBUG(("Disconnecting remote host"));
2862 /* Notify remote end that the conversation is over. The notify message
2863 is tried to be sent immediately. */
2864 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2865 buf, strlen(buf), TRUE);
2867 /* Mark the connection to be disconnected */
2868 SILC_SET_DISCONNECTED(sock);
2869 silc_server_close_connection(server, sock);
2872 /* Free's user_data pointer from socket connection object. As this
2873 pointer maybe anything we wil switch here to find the corrent
2874 data type and free it the way it needs to be free'd. */
2876 void silc_server_free_sock_user_data(SilcServer server,
2877 SilcSocketConnection sock)
2879 SILC_LOG_DEBUG(("Start"));
2881 #define LCC(x) server->local_list->client_cache[(x) - 32]
2882 #define LCCC(x) server->local_list->client_cache_count[(x) - 32]
2884 switch(sock->type) {
2885 case SILC_SOCKET_TYPE_CLIENT:
2887 SilcClientList *user_data = (SilcClientList *)sock->user_data;
2889 /* Remove client from all channels */
2890 silc_server_remove_from_channels(server, sock, user_data);
2892 /* Clear ID cache */
2893 if (user_data->nickname && user_data->id)
2894 silc_idcache_del_by_id(LCC(user_data->nickname[0]),
2895 LCCC(user_data->nickname[0]),
2896 SILC_ID_CLIENT, user_data->id);
2898 /* Free the client entry and everything in it */
2899 /* XXX must take some info to history before freeing */
2900 silc_idlist_del_client(&server->local_list->clients, user_data);
2903 case SILC_SOCKET_TYPE_SERVER:
2904 case SILC_SOCKET_TYPE_ROUTER:
2912 SilcIDListUnknown *user_data = (SilcIDListUnknown *)sock->user_data;
2914 if (user_data->send_key)
2915 silc_cipher_free(user_data->send_key);
2916 if (user_data->receive_key)
2917 silc_cipher_free(user_data->receive_key);
2918 if (user_data->pkcs)
2919 silc_pkcs_free(user_data->pkcs);
2920 if (user_data->hmac) {
2921 silc_hmac_free(user_data->hmac);
2922 memset(user_data->hmac_key, 0, user_data->hmac_key_len);
2923 silc_free(user_data->hmac_key);
2925 silc_free(user_data);
2930 sock->user_data = NULL;
2935 /* Removes client from all channels it has joined. This is used when
2936 client connection is disconnected. If the client on a channel
2937 is last, the channel is removed as well. */
2939 void silc_server_remove_from_channels(SilcServer server,
2940 SilcSocketConnection sock,
2941 SilcClientList *client)
2944 SilcChannelList *channel;
2946 #define LCC(x) server->local_list->channel_cache[(x) - 32]
2947 #define LCCC(x) server->local_list->channel_cache_count[(x) - 32]
2949 /* Remove the client from all channels. The client is removed from
2950 the channels' user list. */
2951 for (i = 0; i < client->channel_count; i++) {
2952 channel = client->channel[i];
2956 /* Remove from channel */
2957 for (k = 0; k < channel->user_list_count; k++) {
2958 if (channel->user_list[k].client == client) {
2960 /* If this client is last one on the channel the channel
2961 is removed all together. */
2962 if (channel->user_list_count == 1) {
2963 silc_idcache_del_by_id(LCC(channel->channel_name[0]),
2964 LCCC(channel->channel_name[0]),
2965 SILC_ID_CHANNEL, channel->id);
2966 silc_idlist_del_channel(&server->local_list->channels, channel);
2970 channel->user_list[k].client = NULL;
2971 channel->user_list[k].mode = SILC_CHANNEL_UMODE_NONE;
2973 /* Send notify to channel about client leaving SILC and thus
2974 the entire channel. */
2975 silc_server_send_notify_to_channel(server, channel,
2982 if (client->channel_count)
2983 silc_free(client->channel);
2984 client->channel = NULL;
2989 /* Removes client from one channel. This is used for example when client
2990 calls LEAVE command to remove itself from the channel. Returns TRUE
2991 if channel still exists and FALSE if the channel is removed when
2992 last client leaves the channel. */
2994 int silc_server_remove_from_one_channel(SilcServer server,
2995 SilcSocketConnection sock,
2996 SilcChannelList *channel,
2997 SilcClientList *client)
3000 SilcChannelList *ch;
3002 #define LCC(x) server->local_list->channel_cache[(x) - 32]
3003 #define LCCC(x) server->local_list->channel_cache_count[(x) - 32]
3005 /* Remove the client from the channel. The client is removed from
3006 the channel's user list. */
3007 for (i = 0; i < client->channel_count; i++) {
3008 ch = client->channel[i];
3009 if (!ch || ch != channel)
3013 client->channel[i] = NULL;
3015 /* Remove from channel */
3016 for (k = 0; k < channel->user_list_count; k++) {
3017 if (channel->user_list[k].client == client) {
3019 /* If this client is last one on the channel the channel
3020 is removed all together. */
3021 if (channel->user_list_count == 1) {
3022 silc_idcache_del_by_id(LCC(channel->channel_name[0]),
3023 LCCC(channel->channel_name[0]),
3024 SILC_ID_CHANNEL, channel->id);
3025 silc_idlist_del_channel(&server->local_list->channels, channel);
3029 channel->user_list[k].client = NULL;
3030 channel->user_list[k].mode = SILC_CHANNEL_UMODE_NONE;
3032 /* Send notify to channel about client leaving the channel */
3033 silc_server_send_notify_to_channel(server, channel,
3034 "%s has left channel %s",
3036 channel->channel_name);
3046 /* Returns TRUE if the given client is on the channel. FALSE if not.
3047 This works because we assure that the user list on the channel is
3048 always in up to date thus we can only check the channel list from
3049 `client' which is faster than checking the user list from `channel'. */
3050 /* XXX This really is utility function and should be in eg. serverutil.c */
3052 int silc_server_client_on_channel(SilcClientList *client,
3053 SilcChannelList *channel)
3057 for (i = 0; i < client->channel_count; i++) {
3058 if (client->channel[i] == channel)
3065 /* Timeout callback. This is called if connection is idle or for some
3066 other reason is not responding within some period of time. This
3067 disconnects the remote end. */
3069 SILC_TASK_CALLBACK(silc_server_timeout_remote)
3071 SilcServer server = (SilcServer)context;
3072 SilcSocketConnection sock = server->sockets[fd];
3074 silc_server_disconnect_remote(server, sock,
3075 "Server closed connection: "
3076 "Connection timeout");
3079 /* Internal routine used to send (relay, route) private messages to some
3080 destination. This is used to by normal server to send the message to
3081 its primary route and router uses this to send it to any route it
3082 wants. If the private message key does not exist then the message
3083 is re-encrypted, otherwise we just pass it along. */
3085 silc_server_private_message_send_internal(SilcServer server,
3086 SilcSocketConnection dst_sock,
3087 SilcServerList *router,
3088 SilcPacketContext *packet)
3090 SilcBuffer buffer = packet->buffer;
3092 /* Send and re-encrypt if private messge key does not exist */
3093 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
3094 unsigned char mac[32];
3095 unsigned int mac_len = 0;
3098 mac_len = router->hmac->hash->hash->hash_len;
3100 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
3101 + packet->dst_id_len + packet->padlen);
3102 silc_server_packet_send_prepare(server, dst_sock, 0, 0, buffer->len);
3103 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
3105 /* Compute new HMAC */
3107 mac_len = router->hmac->hash->hash->hash_len;
3108 memset(mac, 0, sizeof(mac));
3109 silc_hmac_make_with_key(router->hmac,
3110 dst_sock->outbuf->data,
3111 dst_sock->outbuf->len,
3113 router->hmac_key_len,
3115 silc_buffer_put_tail(dst_sock->outbuf, mac, mac_len);
3116 memset(mac, 0, sizeof(mac));
3119 silc_packet_encrypt(router->send_key, dst_sock->outbuf, buffer->len);
3122 silc_buffer_pull_tail(dst_sock->outbuf, mac_len);
3124 /* Send the packet */
3125 silc_server_packet_send_real(server, dst_sock, FALSE);
3128 /* Key exist so just send it */
3129 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
3130 + packet->dst_id_len + packet->padlen);
3131 silc_server_packet_send_prepare(server, dst_sock, 0, 0, buffer->len);
3132 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
3133 silc_server_packet_send_real(server, dst_sock, FALSE);
3137 /* Internal routine to send the received private message packet to
3138 our locally connected client. */
3140 silc_server_private_message_send_local(SilcServer server,
3141 SilcSocketConnection dst_sock,
3142 SilcClientList *client,
3143 SilcPacketContext *packet)
3145 SilcBuffer buffer = packet->buffer;
3147 /* Re-encrypt packet if needed */
3148 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
3149 unsigned char mac[32];
3150 unsigned int mac_len = 0;
3153 mac_len = client->hmac->hash->hash->hash_len;
3155 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
3156 + packet->dst_id_len + packet->padlen);
3157 silc_server_packet_send_prepare(server, dst_sock, 0, 0, buffer->len);
3158 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
3160 /* Compute new HMAC */
3162 memset(mac, 0, sizeof(mac));
3163 silc_hmac_make_with_key(client->hmac,
3164 dst_sock->outbuf->data,
3165 dst_sock->outbuf->len,
3167 client->hmac_key_len,
3169 silc_buffer_put_tail(dst_sock->outbuf, mac, mac_len);
3170 memset(mac, 0, sizeof(mac));
3174 if (client && client->send_key)
3175 silc_packet_encrypt(client->send_key, dst_sock->outbuf,
3179 silc_buffer_pull_tail(dst_sock->outbuf, mac_len);
3181 /* Send the packet */
3182 silc_server_packet_send_real(server, dst_sock, FALSE);
3184 /* Key exist so just send it */
3185 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
3186 + packet->dst_id_len + packet->padlen);
3187 silc_server_packet_send_prepare(server, dst_sock, 0, 0, buffer->len);
3188 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
3189 silc_server_packet_send_real(server, dst_sock, FALSE);
3193 /* Received private message. This resolves the destination of the message
3194 and sends the packet. This is used by both server and router. If the
3195 destination is our locally connected client this sends the packet to
3196 the client. This may also send the message for further routing if
3197 the destination is not in our server (or router). */
3199 void silc_server_private_message(SilcServer server,
3200 SilcSocketConnection sock,
3201 SilcPacketContext *packet)
3203 SilcBuffer buffer = packet->buffer;
3205 SilcServerList *router;
3206 SilcSocketConnection dst_sock;
3207 SilcClientList *client;
3209 SILC_LOG_DEBUG(("Start"));
3211 if (!packet->dst_id) {
3212 SILC_LOG_DEBUG(("Bad Client ID in private message packet"));
3216 /* Decode destination Client ID */
3217 id = silc_id_str2id(packet->dst_id, SILC_ID_CLIENT);
3219 SILC_LOG_DEBUG(("Could not decode destination Client ID"));
3223 /* If the destination belongs to our server we don't have to route
3224 the message anywhere but to send it to the local destination. */
3225 /* XXX: Should use local cache to search but the current idcache system
3226 is so sucky that it cannot be used... it MUST be rewritten! Using
3227 this search is probably faster than if we'd use here the current
3229 client = silc_idlist_find_client_by_id(server->local_list->clients, id);
3231 /* It exists, now deliver the message to the destination */
3232 dst_sock = (SilcSocketConnection)client->connection;
3234 /* If we are router and the client has router then the client is in
3235 our cell but not directly connected to us. */
3236 if (server->server_type == SILC_ROUTER && client->router) {
3237 silc_server_private_message_send_internal(server, dst_sock,
3238 client->router, packet);
3242 /* Seems that client really is directly connected to us */
3243 silc_server_private_message_send_local(server, dst_sock, client, packet);
3247 /* Destination belongs to someone not in this server. If we are normal
3248 server our action is to send the packet to our router. */
3249 if (server->server_type == SILC_SERVER && !server->standalone) {
3250 router = server->id_entry->router;
3251 dst_sock = (SilcSocketConnection)router->connection;
3253 /* Send to primary route */
3254 silc_server_private_message_send_internal(server, dst_sock, router,
3259 /* We are router and we will perform route lookup for the destination
3260 and send the message to fastest route. */
3261 if (server->server_type == SILC_ROUTER && !server->standalone) {
3263 /* Get fastest route and send packet. */
3264 dst_sock = silc_server_get_route(server, id, SILC_ID_CLIENT);
3265 silc_server_private_message_send_internal(server, dst_sock,
3266 dst_sock->user_data, packet);
3271 silc_server_send_error(server, sock,
3272 "No such nickname: Private message not sent");
3274 silc_buffer_free(buffer);
3277 SilcChannelList *silc_find_channel(SilcServer server, SilcChannelID *id)
3280 SilcIDCache *id_cache;
3282 #define LCC(x) server->local_list->channel_cache[(x)]
3283 #define LCCC(x) server->local_list->channel_cache_count[(x)]
3285 for (i = 0; i < 96; i++) {
3288 if (silc_idcache_find_by_id(LCC(i), LCCC(i), (void *)id,
3289 SILC_ID_CHANNEL, &id_cache))
3290 return (SilcChannelList *)id_cache->context;
3298 /* Process received channel message. */
3300 void silc_server_channel_message(SilcServer server,
3301 SilcSocketConnection sock,
3302 SilcPacketContext *packet)
3304 SilcChannelList *channel = NULL;
3305 SilcClientList *client = NULL;
3306 SilcChannelID *id = NULL;
3307 SilcClientID *sender = NULL;
3308 SilcBuffer buffer = packet->buffer;
3311 SILC_LOG_DEBUG(("Processing channel message"));
3314 if (!silc_server_packet_check_mac(server, sock, buffer))
3318 if (packet->dst_id_type != SILC_ID_CHANNEL) {
3319 SILC_LOG_ERROR(("Received bad message for channel, dropped"));
3320 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
3324 /* Find channel entry */
3325 id = silc_id_str2id(packet->dst_id, SILC_ID_CHANNEL);
3326 channel = silc_find_channel(server, id);
3328 SILC_LOG_DEBUG(("Could not find channel"));
3332 /* See that this client is on the channel */
3333 sender = silc_id_str2id(packet->src_id, packet->src_id_type);
3334 for (i = 0; i < channel->user_list_count; i++) {
3335 client = channel->user_list[i].client;
3336 if (client && !SILC_ID_CLIENT_COMPARE(client->id, sender))
3339 if (i >= channel->user_list_count)
3342 /* Distribute the packet to our local clients. This will send the
3343 packet for further routing as well, if needed. */
3344 silc_server_packet_relay_to_channel(server, sock, channel, sender,
3345 packet->src_id_type,
3346 packet->buffer->data,
3347 packet->buffer->len, FALSE);
3354 silc_buffer_free(buffer);
3357 /* Received channel key packet. We distribute the key to all of our locally
3358 connected clients on the channel. */
3359 /* XXX Router must accept this packet and distribute the key to all its
3360 server that has clients on the channel */
3362 void silc_server_channel_key(SilcServer server,
3363 SilcSocketConnection sock,
3364 SilcPacketContext *packet)
3366 SilcBuffer buffer = packet->buffer;
3367 SilcChannelKeyPayload payload = NULL;
3368 SilcChannelID *id = NULL;
3369 SilcChannelList *channel;
3370 SilcClientList *client;
3372 unsigned int key_len;
3376 if (packet->src_id_type != SILC_ID_SERVER &&
3377 sock->type != SILC_SOCKET_TYPE_ROUTER)
3380 /* Decode channel key payload */
3381 payload = silc_channel_key_parse_payload(buffer);
3383 SILC_LOG_ERROR(("Bad channel key payload, dropped"));
3384 SILC_LOG_DEBUG(("Bad channel key payload, dropped"));
3387 /* Get channel ID */
3388 id = silc_id_str2id(silc_channel_key_get_id(payload, NULL), SILC_ID_CHANNEL);
3392 /* Get the channel entry */
3393 channel = silc_idlist_find_channel_by_id(server->local_list->channels, id);
3395 SILC_LOG_ERROR(("Received key for non-existent channel"));
3396 SILC_LOG_DEBUG(("Received key for non-existent channel"));
3400 /* Save the key for us as well */
3401 key = silc_channel_key_get_key(payload, &key_len);
3404 cipher = silc_channel_key_get_cipher(payload, NULL);;
3407 channel->key_len = key_len * 8;
3408 channel->key = silc_calloc(key_len, sizeof(unsigned char));
3409 memcpy(channel->key, key, key_len);
3410 silc_cipher_alloc(cipher, &channel->channel_key);
3411 channel->channel_key->cipher->set_key(channel->channel_key->context,
3414 /* Distribute the key to all clients on the channel */
3415 for (i = 0; i < channel->user_list_count; i++) {
3416 client = channel->user_list[i].client;
3419 silc_server_packet_send_dest(server, client->connection,
3420 SILC_PACKET_CHANNEL_KEY, 0,
3421 client->id, SILC_ID_CLIENT,
3422 buffer->data, buffer->len, FALSE);
3429 silc_channel_key_free_payload(payload);
3430 silc_buffer_free(buffer);
3433 /* Sends error message. Error messages may or may not have any
3436 void silc_server_send_error(SilcServer server,
3437 SilcSocketConnection sock,
3438 const char *fmt, ...)
3441 unsigned char buf[4096];
3443 memset(buf, 0, sizeof(buf));
3445 vsprintf(buf, fmt, ap);
3448 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
3449 buf, strlen(buf), FALSE);
3452 /* Sends notify message */
3454 void silc_server_send_notify(SilcServer server,
3455 SilcSocketConnection sock,
3456 const char *fmt, ...)
3459 unsigned char buf[4096];
3461 memset(buf, 0, sizeof(buf));
3463 vsprintf(buf, fmt, ap);
3466 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0,
3467 buf, strlen(buf), FALSE);
3470 /* Sends notify message destined to specific entity. */
3472 void silc_server_send_notify_dest(SilcServer server,
3473 SilcSocketConnection sock,
3475 SilcIdType dest_id_type,
3476 const char *fmt, ...)
3479 unsigned char buf[4096];
3481 memset(buf, 0, sizeof(buf));
3483 vsprintf(buf, fmt, ap);
3486 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
3487 dest_id, dest_id_type,
3488 buf, strlen(buf), FALSE);
3491 /* Sends notify message to a channel. The notify message sent is
3492 distributed to all clients on the channel. Actually this is not real
3493 notify message, instead it is message to channel sent by server. But
3494 as server is sending it it will appear as notify type message on the
3497 void silc_server_send_notify_to_channel(SilcServer server,
3498 SilcChannelList *channel,
3499 const char *fmt, ...)
3502 unsigned char buf[4096];
3504 memset(buf, 0, sizeof(buf));
3506 vsprintf(buf, fmt, ap);
3509 silc_server_packet_send_to_channel(server, channel, buf,
3510 strlen(buf), FALSE);
3513 /* Sends New ID Payload to remote end. The packet is used to distribute
3514 information about new registered clients, servers, channel etc. usually
3515 to routers so that they can keep these information up to date.
3516 If the argument `broadcast' is TRUE then the packet is sent as
3517 broadcast packet. */
3519 void silc_server_send_new_id(SilcServer server,
3520 SilcSocketConnection sock,
3522 void *id, SilcIdType id_type,
3523 unsigned int id_len)
3526 unsigned char *id_string;
3528 id_string = silc_id_id2str(id, id_type);
3532 packet = silc_buffer_alloc(2 + 2 + id_len);
3533 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
3534 silc_buffer_format(packet,
3535 SILC_STR_UI_SHORT(id_type),
3536 SILC_STR_UI_SHORT(id_len),
3537 SILC_STR_UI_XNSTRING(id_string, id_len),
3540 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
3541 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
3542 packet->data, packet->len, FALSE);
3543 silc_free(id_string);
3544 silc_buffer_free(packet);
3547 /* Sends Replace ID payload to remote end. This is used to replace old
3548 ID with new ID sent in the packet. This is called for example when
3549 user changes nickname and we create new ID for the user. If the
3550 argument `broadcast' is TRUE then the packet is sent as
3551 broadcast packet. */
3552 /* XXX It would be expected that the new id is same type as the old
3555 void silc_server_send_replace_id(SilcServer server,
3556 SilcSocketConnection sock,
3558 void *old_id, SilcIdType old_id_type,
3559 unsigned int old_id_len,
3560 void *new_id, SilcIdType new_id_type,
3561 unsigned int new_id_len)
3567 oid = silc_id_id2str(old_id, old_id_type);
3571 nid = silc_id_id2str(new_id, new_id_type);
3575 packet = silc_buffer_alloc(2 + 2 + 2 + 2 + old_id_len + new_id_len);
3576 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
3577 silc_buffer_format(packet,
3578 SILC_STR_UI_SHORT(old_id_type),
3579 SILC_STR_UI_SHORT(old_id_len),
3580 SILC_STR_UI_XNSTRING(oid, old_id_len),
3581 SILC_STR_UI_SHORT(new_id_type),
3582 SILC_STR_UI_SHORT(new_id_len),
3583 SILC_STR_UI_XNSTRING(nid, new_id_len),
3586 silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID,
3587 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
3588 packet->data, packet->len, FALSE);
3591 silc_buffer_free(packet);
3594 /* Creates new channel. */
3596 SilcChannelList *silc_server_new_channel(SilcServer server,
3597 SilcServerID *router_id,
3598 char *cipher, char *channel_name)
3600 int i, channel_len, key_len;
3601 SilcChannelID *channel_id;
3602 SilcChannelList *entry;
3604 unsigned char channel_key[32], *id_string;
3607 SILC_LOG_DEBUG(("Creating new channel"));
3609 #define LCC(x) server->local_list->channel_cache[(x) - 32]
3610 #define LCCC(x) server->local_list->channel_cache_count[(x) - 32]
3612 /* Create channel key */
3613 for (i = 0; i < 32; i++)
3614 channel_key[i] = silc_rng_get_byte(server->rng);
3621 silc_cipher_alloc(cipher, &key);
3622 key->cipher->set_key(key->context, channel_key, key_len);
3624 /* Create the channel */
3625 silc_id_create_channel_id(router_id, server->rng, &channel_id);
3626 silc_idlist_add_channel(&server->local_list->channels, channel_name,
3627 SILC_CHANNEL_MODE_NONE, channel_id, NULL, /*XXX*/
3629 LCCC(channel_name[0]) = silc_idcache_add(&LCC(channel_name[0]),
3630 LCCC(channel_name[0]),
3631 channel_name, SILC_ID_CHANNEL,
3632 channel_id, (void *)entry);
3633 entry->key = silc_calloc(key_len, sizeof(*entry->key));
3634 entry->key_len = key_len * 8;
3635 memcpy(entry->key, channel_key, key_len);
3636 memset(channel_key, 0, sizeof(channel_key));
3638 /* Notify other routers about the new channel. We send the packet
3639 to our primary route. */
3640 if (server->standalone == FALSE) {
3641 channel_len = strlen(channel_name);
3642 id_string = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3643 packet = silc_buffer_alloc(2 + SILC_ID_CHANNEL_LEN);
3645 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
3646 silc_buffer_format(packet,
3647 SILC_STR_UI_SHORT(channel_len),
3648 SILC_STR_UI_XNSTRING(channel_name, channel_len),
3649 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
3650 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CHANNEL_LEN),
3653 /* Send the packet to our router. */
3654 silc_server_packet_send(server, (SilcSocketConnection)
3655 server->id_entry->router->connection,
3656 SILC_PACKET_NEW_CHANNEL_USER, 0,
3657 packet->data, packet->len, TRUE);
3659 silc_free(id_string);
3660 silc_buffer_free(packet);
3668 /* Create new client. This processes incoming NEW_CLIENT packet and creates
3669 Client ID for the client and adds it to lists and cache. */
3671 SilcClientList *silc_server_new_client(SilcServer server,
3672 SilcSocketConnection sock,
3673 SilcPacketContext *packet)
3675 SilcBuffer buffer = packet->buffer;
3676 SilcClientList *id_entry;
3677 char *username = NULL, *realname = NULL, *id_string;
3680 SILC_LOG_DEBUG(("Creating new client"));
3682 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
3685 #define LCC(x) server->local_list->client_cache[(x) - 32]
3686 #define LCCC(x) server->local_list->client_cache_count[(x) - 32]
3688 silc_buffer_unformat(buffer,
3689 SILC_STR_UI16_STRING_ALLOC(&username),
3690 SILC_STR_UI16_STRING_ALLOC(&realname),
3693 /* Set the pointers to the client list and create new client ID */
3694 id_entry = (SilcClientList *)sock->user_data;
3695 id_entry->nickname = strdup(username);
3696 id_entry->username = username;
3697 id_entry->userinfo = realname;
3698 silc_id_create_client_id(server->id, server->rng, server->md5hash,
3699 username, &id_entry->id);
3701 /* Add to client cache */
3702 LCCC(username[0]) = silc_idcache_add(&LCC(username[0]),
3704 username, SILC_ID_CLIENT,
3705 id_entry->id, (void *)id_entry);
3707 /* Notify our router about new client on the SILC network */
3708 if (!server->standalone)
3709 silc_server_send_new_id(server, (SilcSocketConnection)
3710 server->id_entry->router->connection,
3711 server->server_type == SILC_SERVER ? TRUE : FALSE,
3712 id_entry->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
3714 /* Send the new client ID to the client. */
3715 id_string = silc_id_id2str(id_entry->id, SILC_ID_CLIENT);
3716 reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
3717 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
3718 silc_buffer_format(reply,
3719 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
3720 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
3721 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
3723 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
3724 reply->data, reply->len, FALSE);
3725 silc_free(id_string);
3726 silc_buffer_free(reply);
3728 /* Send some nice info to the client */
3729 silc_server_send_notify(server, sock,
3730 "Welcome to the SILC Network %s@%s",
3732 sock->hostname ? sock->hostname : sock->ip);
3733 silc_server_send_notify(server, sock,
3734 "Your host is %s, running version %s",
3735 server->config->server_info->server_name,
3737 silc_server_send_notify(server, sock,
3738 "Your connection is secured with %s cipher, "
3739 "key length %d bits",
3740 id_entry->send_key->cipher->name,
3741 id_entry->send_key->cipher->key_len);
3742 silc_server_send_notify(server, sock,
3743 "Your current nickname is %s",
3744 id_entry->nickname);
3753 /* Create new server. This processes incoming NEW_SERVER packet and
3754 saves the received Server ID. The server is our locally connected
3755 server thus we save all the information and save it to local list.
3756 This funtion can be used by both normal server and router server.
3757 If normal server uses this it means that its router has connected
3758 to the server. If router uses this it means that one of the cell's
3759 servers is connected to the router. */
3761 SilcServerList *silc_server_new_server(SilcServer server,
3762 SilcSocketConnection sock,
3763 SilcPacketContext *packet)
3765 SilcBuffer buffer = packet->buffer;
3766 SilcServerList *id_entry;
3767 unsigned char *server_name, *id_string;
3769 SILC_LOG_DEBUG(("Creating new server"));
3771 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
3772 sock->type != SILC_SOCKET_TYPE_ROUTER)
3775 #define LSC(x) server->local_list->server_cache[(x) - 32]
3776 #define LSCC(x) server->local_list->server_cache_count[(x) - 32]
3778 silc_buffer_unformat(buffer,
3779 SILC_STR_UI16_STRING_ALLOC(&id_string),
3780 SILC_STR_UI16_STRING_ALLOC(&server_name),
3783 /* Save ID and name */
3784 id_entry = (SilcServerList *)sock->user_data;
3785 id_entry->id = silc_id_str2id(id_string, SILC_ID_SERVER);
3786 id_entry->server_name = server_name;
3788 /* Add to server cache */
3789 LSCC(server_name[0]) =
3790 silc_idcache_add(&LSC(server_name[0]),
3791 LSCC(server_name[0]),
3792 server_name, SILC_ID_SERVER,
3793 id_entry->id, (void *)id_entry);
3795 /* Distribute the information about new server in the SILC network
3796 to our router. If we are normal server we won't send anything
3797 since this connection must be our router connection. */
3798 if (server->server_type == SILC_ROUTER && !server->standalone)
3799 silc_server_send_new_id(server, server->id_entry->router->connection,
3800 TRUE, id_entry->id, SILC_ID_SERVER,
3801 SILC_ID_SERVER_LEN);
3803 silc_free(id_string);
3810 /* Processes incoming New ID Payload. New ID Payload is used to distribute
3811 information about newly registered clients, servers and created
3814 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
3815 SilcPacketContext *packet)
3817 SilcBuffer buffer = packet->buffer;
3819 unsigned char *id_string;
3822 SILC_LOG_DEBUG(("Processing new ID"));
3824 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3825 server->server_type == SILC_SERVER)
3828 silc_buffer_unformat(buffer,
3829 SILC_STR_UI_SHORT(&id_type),
3830 SILC_STR_UI16_STRING_ALLOC(&id_string),
3833 /* Normal server cannot have other normal server connections */
3834 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
3837 id = silc_id_str2id(id_string, id_type);
3841 /* XXX Do check whether the packet is coming outside the cell or
3842 from someone inside the cell. If outside use global lists otherwise
3844 /* XXX If using local list set the idlist->connection to the sender's
3845 socket connection as it is used in packet sending */
3848 case SILC_ID_CLIENT:
3850 SilcClientList *idlist;
3852 /* Add the client to our local list. We are router and we keep
3853 cell specific local database of all clients in the cell. */
3854 silc_idlist_add_client(&server->local_list->clients, NULL, NULL, NULL,
3855 id, sock->user_data, NULL, NULL,
3856 NULL, NULL, &idlist);
3857 idlist->connection = sock;
3861 case SILC_ID_SERVER:
3863 SilcServerList *idlist;
3865 /* Add the server to our local list. We are router and we keep
3866 cell specific local database of all servers in the cell. */
3867 silc_idlist_add_server(&server->local_list->servers, NULL, 0,
3868 id, server->id_entry, NULL, NULL,
3869 NULL, NULL, &idlist);
3870 idlist->connection = sock;
3874 case SILC_ID_CHANNEL:
3875 /* Add the channel to our local list. We are router and we keep
3876 cell specific local database of all channels in the cell. */
3877 silc_idlist_add_channel(&server->local_list->channels, NULL, 0,
3878 id, server->id_entry, NULL, NULL);
3887 silc_free(id_string);