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.
22 #include "clientlibincludes.h"
24 /* Static task callback prototypes */
25 SILC_TASK_CALLBACK(silc_client_connect_to_server_start);
26 SILC_TASK_CALLBACK(silc_client_connect_to_server_second);
27 SILC_TASK_CALLBACK(silc_client_connect_to_server_final);
28 SILC_TASK_CALLBACK(silc_client_packet_process);
29 SILC_TASK_CALLBACK(silc_client_packet_parse_real);
31 static void silc_client_packet_parse(SilcPacketParserContext *parser_context);
32 static void silc_client_packet_parse_type(SilcClient client,
33 SilcSocketConnection sock,
34 SilcPacketContext *packet);
36 /* Allocates new client object. This has to be done before client may
37 work. After calling this one must call silc_client_init to initialize
38 the client. The `application' is application specific user data pointer
39 and caller must free it. */
41 SilcClient silc_client_alloc(SilcClientOperations *ops, void *application)
43 SilcClient new_client;
45 new_client = silc_calloc(1, sizeof(*new_client));
46 new_client->application = application;
47 new_client->ops = ops;
52 /* Free's client object */
54 void silc_client_free(SilcClient client)
58 silc_rng_free(client->rng);
64 /* Initializes the client. This makes all the necessary steps to make
65 the client ready to be run. One must call silc_client_run to run the
68 int silc_client_init(SilcClient client)
70 SILC_LOG_DEBUG(("Initializing client"));
72 /* Initialize hash functions for client to use */
73 silc_hash_alloc("md5", &client->md5hash);
74 silc_hash_alloc("sha1", &client->sha1hash);
76 /* Initialize none cipher */
77 silc_cipher_alloc("none", &client->none_cipher);
79 /* Initialize random number generator */
80 client->rng = silc_rng_alloc();
81 silc_rng_init(client->rng);
82 silc_rng_global_init(client->rng);
84 /* Register protocols */
85 silc_client_protocols_register();
87 /* Initialize the scheduler */
88 silc_schedule_init(&client->io_queue, &client->timeout_queue,
89 &client->generic_queue, 5000);
94 /* Stops the client. This is called to stop the client and thus to stop
97 void silc_client_stop(SilcClient client)
99 SILC_LOG_DEBUG(("Stopping client"));
101 /* Stop the scheduler, although it might be already stopped. This
102 doesn't hurt anyone. This removes all the tasks and task queues,
104 silc_schedule_stop();
105 silc_schedule_uninit();
107 silc_client_protocols_unregister();
109 SILC_LOG_DEBUG(("Client stopped"));
112 /* Runs the client. */
114 void silc_client_run(SilcClient client)
116 SILC_LOG_DEBUG(("Running client"));
118 /* Start the scheduler, the heart of the SILC client. When this returns
119 the program will be terminated. */
123 /* Allocates and adds new connection to the client. This adds the allocated
124 connection to the connection table and returns a pointer to it. A client
125 can have multiple connections to multiple servers. Every connection must
126 be added to the client using this function. User data `context' may
127 be sent as argument. */
129 SilcClientConnection silc_client_add_connection(SilcClient client,
134 SilcClientConnection conn;
137 conn = silc_calloc(1, sizeof(*conn));
139 /* Initialize ID caches */
140 conn->client_cache = silc_idcache_alloc(0);
141 conn->channel_cache = silc_idcache_alloc(0);
142 conn->server_cache = silc_idcache_alloc(0);
143 conn->client = client;
144 conn->remote_host = strdup(hostname);
145 conn->remote_port = port;
146 conn->context = context;
147 conn->pending_commands = silc_dlist_init();
149 /* Add the connection to connections table */
150 for (i = 0; i < client->conns_count; i++)
151 if (client->conns && !client->conns[i]) {
152 client->conns[i] = conn;
156 client->conns = silc_realloc(client->conns, sizeof(*client->conns)
157 * (client->conns_count + 1));
158 client->conns[client->conns_count] = conn;
159 client->conns_count++;
164 /* Removes connection from client. */
166 void silc_client_del_connection(SilcClient client, SilcClientConnection conn)
170 for (i = 0; i < client->conns_count; i++)
171 if (client->conns[i] == conn) {
172 if (conn->pending_commands)
173 silc_dlist_uninit(conn->pending_commands);
175 client->conns[i] = NULL;
179 /* Internal context for connection process. This is needed as we
180 doing asynchronous connecting. */
183 SilcClientConnection conn;
189 } SilcClientInternalConnectContext;
192 silc_client_connect_to_server_internal(SilcClientInternalConnectContext *ctx)
196 /* XXX In the future we should give up this non-blocking connect all
197 together and use threads instead. */
198 /* Create connection to server asynchronously */
199 sock = silc_net_create_connection_async(ctx->port, ctx->host);
203 /* Register task that will receive the async connect and will
205 ctx->task = silc_task_register(ctx->client->io_queue, sock,
206 silc_client_connect_to_server_start,
209 SILC_TASK_PRI_NORMAL);
210 silc_task_reset_iotype(ctx->task, SILC_TASK_WRITE);
211 silc_schedule_set_listen_fd(sock, ctx->task->iomask);
218 /* Connects to remote server. This is the main routine used to connect
219 to SILC server. Returns -1 on error and the created socket otherwise.
220 The `context' is user context that is saved into the SilcClientConnection
221 that is created after the connection is created. */
223 int silc_client_connect_to_server(SilcClient client, int port,
224 char *host, void *context)
226 SilcClientInternalConnectContext *ctx;
227 SilcClientConnection conn;
230 SILC_LOG_DEBUG(("Connecting to port %d of server %s",
233 conn = silc_client_add_connection(client, host, port, context);
235 client->ops->say(client, conn,
236 "Connecting to port %d of server %s", port, host);
238 /* Allocate internal context for connection process. This is
239 needed as we are doing async connecting. */
240 ctx = silc_calloc(1, sizeof(*ctx));
241 ctx->client = client;
243 ctx->host = strdup(host);
247 /* Do the actual connecting process */
248 sock = silc_client_connect_to_server_internal(ctx);
250 silc_client_del_connection(client, conn);
254 /* Start SILC Key Exchange (SKE) protocol to negotiate shared secret
255 key material between client and server. This function can be called
256 directly if application is performing its own connecting and does not
257 use the connecting provided by this library. */
259 int silc_client_start_key_exchange(SilcClient client,
260 SilcClientConnection conn,
263 SilcProtocol protocol;
264 SilcClientKEInternalContext *proto_ctx;
267 /* Allocate new socket connection object */
268 silc_socket_alloc(fd, SILC_SOCKET_TYPE_SERVER, (void *)conn, &conn->sock);
269 if (conn->sock == NULL) {
270 client->ops->say(client, conn,
271 "Error: Could not allocate connection socket");
275 conn->nickname = strdup(client->username);
276 conn->sock->hostname = conn->remote_host;
277 conn->sock->ip = strdup(conn->remote_host);
278 conn->sock->port = conn->remote_port;
280 /* Allocate internal Key Exchange context. This is sent to the
281 protocol as context. */
282 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
283 proto_ctx->client = (void *)client;
284 proto_ctx->sock = conn->sock;
285 proto_ctx->rng = client->rng;
286 proto_ctx->responder = FALSE;
288 /* Perform key exchange protocol. silc_client_connect_to_server_final
289 will be called after the protocol is finished. */
290 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
291 &protocol, (void *)proto_ctx,
292 silc_client_connect_to_server_second);
294 client->ops->say(client, conn,
295 "Error: Could not start authentication protocol");
298 conn->sock->protocol = protocol;
300 /* Register the connection for network input and output. This sets
301 that scheduler will listen for incoming packets for this connection
302 and sets that outgoing packets may be sent to this connection as well.
303 However, this doesn't set the scheduler for outgoing traffic, it will
304 be set separately by calling SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT,
305 later when outgoing data is available. */
306 context = (void *)client;
307 SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd);
309 /* Execute the protocol */
310 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
314 /* Start of the connection to the remote server. This is called after
315 succesful TCP/IP connection has been established to the remote host. */
317 SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
319 SilcClientInternalConnectContext *ctx =
320 (SilcClientInternalConnectContext *)context;
321 SilcClient client = ctx->client;
322 SilcClientConnection conn = ctx->conn;
323 int opt, opt_len = sizeof(opt);
325 SILC_LOG_DEBUG(("Start"));
327 /* Check the socket status as it might be in error */
328 getsockopt(fd, SOL_SOCKET, SO_ERROR, &opt, &opt_len);
330 if (ctx->tries < 2) {
331 /* Connection failed but lets try again */
332 client->ops->say(client, conn, "Could not connect to server %s: %s",
333 ctx->host, strerror(opt));
334 client->ops->say(client, conn,
335 "Connecting to port %d of server %s resumed",
336 ctx->port, ctx->host);
338 /* Unregister old connection try */
339 silc_schedule_unset_listen_fd(fd);
340 silc_net_close_connection(fd);
341 silc_task_unregister(client->io_queue, ctx->task);
344 silc_client_connect_to_server_internal(ctx);
347 /* Connection failed and we won't try anymore */
348 client->ops->say(client, conn, "Could not connect to server %s: %s",
349 ctx->host, strerror(opt));
350 silc_schedule_unset_listen_fd(fd);
351 silc_net_close_connection(fd);
352 silc_task_unregister(client->io_queue, ctx->task);
355 /* Notify application of failure */
356 client->ops->connect(client, conn, FALSE);
357 silc_client_del_connection(client, conn);
362 silc_schedule_unset_listen_fd(fd);
363 silc_task_unregister(client->io_queue, ctx->task);
366 if (!silc_client_start_key_exchange(client, conn, fd)) {
367 silc_net_close_connection(fd);
368 client->ops->connect(client, conn, FALSE);
372 /* Second part of the connecting to the server. This executed
373 authentication protocol. */
375 SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
377 SilcProtocol protocol = (SilcProtocol)context;
378 SilcClientKEInternalContext *ctx =
379 (SilcClientKEInternalContext *)protocol->context;
380 SilcClient client = (SilcClient)ctx->client;
381 SilcSocketConnection sock = NULL;
382 SilcClientConnAuthInternalContext *proto_ctx;
384 SILC_LOG_DEBUG(("Start"));
386 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
387 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
388 /* Error occured during protocol */
389 SILC_LOG_DEBUG(("Error during KE protocol"));
390 silc_protocol_free(protocol);
392 silc_ske_free(ctx->ske);
394 silc_free(ctx->dest_id);
395 ctx->sock->protocol = NULL;
397 /* Notify application of failure */
398 client->ops->connect(client, ctx->sock->user_data, FALSE);
403 /* Allocate internal context for the authentication protocol. This
404 is sent as context for the protocol. */
405 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
406 proto_ctx->client = (void *)client;
407 proto_ctx->sock = sock = ctx->sock;
408 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
409 proto_ctx->dest_id_type = ctx->dest_id_type;
410 proto_ctx->dest_id = ctx->dest_id;
412 /* Resolve the authentication method to be used in this connection */
413 if (!client->ops->get_auth_method(client, sock->user_data, sock->hostname,
414 sock->port, &proto_ctx->auth_meth,
415 &proto_ctx->auth_data,
416 &proto_ctx->auth_data_len))
418 /* XXX do AUTH_REQUEST resolcing with server */
419 proto_ctx->auth_meth = SILC_AUTH_NONE;
422 /* Free old protocol as it is finished now */
423 silc_protocol_free(protocol);
425 silc_packet_context_free(ctx->packet);
427 /* silc_free(ctx->keymat....); */
428 sock->protocol = NULL;
430 /* Allocate the authentication protocol. This is allocated here
431 but we won't start it yet. We will be receiving party of this
432 protocol thus we will wait that connecting party will make
434 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
435 &sock->protocol, (void *)proto_ctx,
436 silc_client_connect_to_server_final);
438 /* Execute the protocol */
439 sock->protocol->execute(client->timeout_queue, 0, sock->protocol, fd, 0, 0);
442 /* Finalizes the connection to the remote SILC server. This is called
443 after authentication protocol has been completed. This send our
444 user information to the server to receive our client ID from
447 SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
449 SilcProtocol protocol = (SilcProtocol)context;
450 SilcClientConnAuthInternalContext *ctx =
451 (SilcClientConnAuthInternalContext *)protocol->context;
452 SilcClient client = (SilcClient)ctx->client;
453 SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
456 SILC_LOG_DEBUG(("Start"));
458 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
459 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
460 /* Error occured during protocol */
461 SILC_LOG_DEBUG(("Error during authentication protocol"));
462 silc_protocol_free(protocol);
464 silc_free(ctx->auth_data);
466 silc_ske_free(ctx->ske);
468 silc_free(ctx->dest_id);
469 conn->sock->protocol = NULL;
471 /* Notify application of failure */
472 client->ops->connect(client, ctx->sock->user_data, FALSE);
477 /* Send NEW_CLIENT packet to the server. We will become registered
478 to the SILC network after sending this packet and we will receive
479 client ID from the server. */
480 packet = silc_buffer_alloc(2 + 2 + strlen(client->username) +
481 strlen(client->realname));
482 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
483 silc_buffer_format(packet,
484 SILC_STR_UI_SHORT(strlen(client->username)),
485 SILC_STR_UI_XNSTRING(client->username,
486 strlen(client->username)),
487 SILC_STR_UI_SHORT(strlen(client->realname)),
488 SILC_STR_UI_XNSTRING(client->realname,
489 strlen(client->realname)),
492 /* Send the packet */
493 silc_client_packet_send(client, ctx->sock, SILC_PACKET_NEW_CLIENT,
495 packet->data, packet->len, TRUE);
496 silc_buffer_free(packet);
498 /* Save remote ID. */
499 conn->remote_id = ctx->dest_id;
500 conn->remote_id_data = silc_id_id2str(ctx->dest_id, SILC_ID_SERVER);
501 conn->remote_id_data_len = SILC_ID_SERVER_LEN;
503 silc_protocol_free(protocol);
505 silc_free(ctx->auth_data);
507 silc_ske_free(ctx->ske);
509 conn->sock->protocol = NULL;
512 /* Internal routine that sends packet or marks packet to be sent. This
513 is used directly only in special cases. Normal cases should use
514 silc_server_packet_send. Returns < 0 on error. */
516 static int silc_client_packet_send_real(SilcClient client,
517 SilcSocketConnection sock,
522 /* Send the packet */
523 ret = silc_packet_send(sock, force_send);
527 /* Mark that there is some outgoing data available for this connection.
528 This call sets the connection both for input and output (the input
529 is set always and this call keeps the input setting, actually).
530 Actual data sending is performed by silc_client_packet_process. */
531 SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(sock->sock);
533 /* Mark to socket that data is pending in outgoing buffer. This flag
534 is needed if new data is added to the buffer before the earlier
535 put data is sent to the network. */
536 SILC_SET_OUTBUF_PENDING(sock);
541 /* Packet processing callback. This is used to send and receive packets
542 from network. This is generic task. */
544 SILC_TASK_CALLBACK(silc_client_packet_process)
546 SilcClient client = (SilcClient)context;
547 SilcSocketConnection sock = NULL;
548 SilcClientConnection conn;
551 SILC_LOG_DEBUG(("Processing packet"));
553 SILC_CLIENT_GET_SOCK(client, fd, sock);
557 conn = (SilcClientConnection)sock->user_data;
560 if (type == SILC_TASK_WRITE) {
561 SILC_LOG_DEBUG(("Writing data to connection"));
563 if (sock->outbuf->data - sock->outbuf->head)
564 silc_buffer_push(sock->outbuf,
565 sock->outbuf->data - sock->outbuf->head);
567 ret = silc_client_packet_send_real(client, sock, TRUE);
569 /* If returned -2 could not write to connection now, will do
574 /* The packet has been sent and now it is time to set the connection
575 back to only for input. When there is again some outgoing data
576 available for this connection it will be set for output as well.
577 This call clears the output setting and sets it only for input. */
578 SILC_CLIENT_SET_CONNECTION_FOR_INPUT(fd);
579 SILC_UNSET_OUTBUF_PENDING(sock);
581 silc_buffer_clear(sock->outbuf);
585 /* Packet receiving */
586 if (type == SILC_TASK_READ) {
587 SILC_LOG_DEBUG(("Reading data from connection"));
589 /* Read data from network */
590 ret = silc_packet_receive(sock);
596 SILC_LOG_DEBUG(("Read EOF"));
598 /* If connection is disconnecting already we will finally
599 close the connection */
600 if (SILC_IS_DISCONNECTING(sock)) {
601 client->ops->disconnect(client, conn);
602 silc_client_close_connection(client, sock);
606 SILC_LOG_DEBUG(("EOF from connection %d", sock->sock));
607 client->ops->disconnect(client, conn);
608 silc_client_close_connection(client, sock);
612 /* Process the packet. This will call the parser that will then
613 decrypt and parse the packet. */
614 silc_packet_receive_process(sock, conn->receive_key, conn->hmac,
615 silc_client_packet_parse, client);
619 /* Parses whole packet, received earlier. */
621 SILC_TASK_CALLBACK(silc_client_packet_parse_real)
623 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
624 SilcClient client = (SilcClient)parse_ctx->context;
625 SilcPacketContext *packet = parse_ctx->packet;
626 SilcBuffer buffer = packet->buffer;
627 SilcSocketConnection sock = parse_ctx->sock;
628 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
631 SILC_LOG_DEBUG(("Start"));
633 /* Decrypt the received packet */
634 ret = silc_packet_decrypt(conn->receive_key, conn->hmac, buffer, packet);
639 /* Parse the packet. Packet type is returned. */
640 ret = silc_packet_parse(packet);
642 /* Parse the packet header in special way as this is "special"
644 ret = silc_packet_parse_special(packet);
647 if (ret == SILC_PACKET_NONE)
650 /* Parse the incoming packet type */
651 silc_client_packet_parse_type(client, sock, packet);
654 silc_buffer_clear(sock->inbuf);
655 silc_packet_context_free(packet);
656 silc_free(parse_ctx);
659 /* Parser callback called by silc_packet_receive_process. Thie merely
660 registers timeout that will handle the actual parsing when appropriate. */
662 void silc_client_packet_parse(SilcPacketParserContext *parser_context)
664 SilcClient client = (SilcClient)parser_context->context;
666 /* Parse the packet */
667 silc_task_register(client->timeout_queue, parser_context->sock->sock,
668 silc_client_packet_parse_real,
669 (void *)parser_context, 0, 1,
671 SILC_TASK_PRI_NORMAL);
674 /* Parses the packet type and calls what ever routines the packet type
675 requires. This is done for all incoming packets. */
677 void silc_client_packet_parse_type(SilcClient client,
678 SilcSocketConnection sock,
679 SilcPacketContext *packet)
681 SilcBuffer buffer = packet->buffer;
682 SilcPacketType type = packet->type;
684 SILC_LOG_DEBUG(("Parsing packet type %d", type));
686 /* Parse the packet type */
688 case SILC_PACKET_DISCONNECT:
689 silc_client_disconnected_by_server(client, sock, buffer);
691 case SILC_PACKET_SUCCESS:
693 * Success received for something. For now we can have only
694 * one protocol for connection executing at once hence this
695 * success message is for whatever protocol is executing currently.
697 if (sock->protocol) {
698 sock->protocol->execute(client->timeout_queue, 0,
699 sock->protocol, sock->sock, 0, 0);
702 case SILC_PACKET_FAILURE:
704 * Failure received for some protocol. Set the protocol state to
705 * error and call the protocol callback. This fill cause error on
706 * protocol and it will call the final callback.
708 if (sock->protocol) {
709 sock->protocol->state = SILC_PROTOCOL_STATE_FAILURE;
710 sock->protocol->execute(client->timeout_queue, 0,
711 sock->protocol, sock->sock, 0, 0);
713 /* XXX We have only two protocols currently thus we know what this
714 failure indication is. */
715 if (buffer->len >= 4) {
716 unsigned int failure;
718 SILC_GET32_MSB(failure, buffer->data);
720 /* Notify application */
721 client->ops->failure(client, sock->user_data, sock->protocol,
726 case SILC_PACKET_REJECT:
729 case SILC_PACKET_NOTIFY:
731 * Received notify message
733 silc_client_notify_by_server(client, sock, packet);
736 case SILC_PACKET_ERROR:
738 * Received error message
740 silc_client_error_by_server(client, sock, buffer);
743 case SILC_PACKET_CHANNEL_MESSAGE:
745 * Received message to (from, actually) a channel
747 silc_client_channel_message(client, sock, packet);
749 case SILC_PACKET_CHANNEL_KEY:
751 * Received key for a channel. By receiving this key the client will be
752 * able to talk to the channel it has just joined. This can also be
753 * a new key for existing channel as keys expire peridiocally.
755 silc_client_receive_channel_key(client, sock, buffer);
758 case SILC_PACKET_PRIVATE_MESSAGE:
760 * Received private message
762 silc_client_private_message(client, sock, packet);
764 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
766 * Received private message key
770 case SILC_PACKET_COMMAND_REPLY:
772 * Recived reply for a command
774 silc_client_command_reply_process(client, sock, packet);
777 case SILC_PACKET_KEY_EXCHANGE:
778 if (sock->protocol) {
779 SilcClientKEInternalContext *proto_ctx =
780 (SilcClientKEInternalContext *)sock->protocol->context;
782 proto_ctx->packet = silc_packet_context_dup(packet);
783 proto_ctx->dest_id_type = packet->src_id_type;
784 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
785 packet->src_id_type);
786 if (!proto_ctx->dest_id)
789 /* Let the protocol handle the packet */
790 sock->protocol->execute(client->timeout_queue, 0,
791 sock->protocol, sock->sock, 0, 0);
793 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
794 "protocol active, packet dropped."));
796 /* XXX Trigger KE protocol?? Rekey actually! */
800 case SILC_PACKET_KEY_EXCHANGE_1:
801 if (sock->protocol) {
804 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
805 "protocol active, packet dropped."));
808 case SILC_PACKET_KEY_EXCHANGE_2:
809 if (sock->protocol) {
810 SilcClientKEInternalContext *proto_ctx =
811 (SilcClientKEInternalContext *)sock->protocol->context;
813 if (proto_ctx->packet)
814 silc_packet_context_free(proto_ctx->packet);
816 proto_ctx->packet = silc_packet_context_dup(packet);
817 proto_ctx->dest_id_type = packet->src_id_type;
818 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
819 packet->src_id_type);
820 if (!proto_ctx->dest_id)
823 /* Let the protocol handle the packet */
824 sock->protocol->execute(client->timeout_queue, 0,
825 sock->protocol, sock->sock, 0, 0);
827 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
828 "protocol active, packet dropped."));
832 case SILC_PACKET_NEW_ID:
835 * Received new ID from server. This packet is received at
836 * the connection to the server. New ID is also received when
837 * user changes nickname but in that case the new ID is received
838 * as command reply and not as this packet type.
842 idp = silc_id_payload_parse(buffer);
845 if (silc_id_payload_get_type(idp) != SILC_ID_CLIENT)
848 silc_client_receive_new_id(client, sock, idp);
849 silc_id_payload_free(idp);
853 case SILC_PACKET_HEARTBEAT:
855 * Received heartbeat packet
857 SILC_LOG_DEBUG(("Heartbeat packet"));
861 SILC_LOG_DEBUG(("Incorrect packet type %d, packet dropped", type));
866 /* Sends packet. This doesn't actually send the packet instead it assembles
867 it and marks it to be sent. However, if force_send is TRUE the packet
868 is sent immediately. if dst_id, cipher and hmac are NULL those parameters
869 will be derived from sock argument. Otherwise the valid arguments sent
872 void silc_client_packet_send(SilcClient client,
873 SilcSocketConnection sock,
876 SilcIdType dst_id_type,
880 unsigned int data_len,
883 SilcPacketContext packetdata;
885 SILC_LOG_DEBUG(("Sending packet, type %d", type));
887 /* Get data used in the packet sending, keys and stuff */
888 if ((!cipher || !hmac || !dst_id) && sock->user_data) {
889 if (!cipher && ((SilcClientConnection)sock->user_data)->send_key)
890 cipher = ((SilcClientConnection)sock->user_data)->send_key;
892 if (!hmac && ((SilcClientConnection)sock->user_data)->hmac)
893 hmac = ((SilcClientConnection)sock->user_data)->hmac;
895 if (!dst_id && ((SilcClientConnection)sock->user_data)->remote_id) {
896 dst_id = ((SilcClientConnection)sock->user_data)->remote_id;
897 dst_id_type = SILC_ID_SERVER;
901 /* Set the packet context pointers */
902 packetdata.flags = 0;
903 packetdata.type = type;
904 if (((SilcClientConnection)sock->user_data)->local_id_data)
905 packetdata.src_id = ((SilcClientConnection)sock->user_data)->local_id_data;
907 packetdata.src_id = silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char));
908 packetdata.src_id_len = SILC_ID_CLIENT_LEN;
909 packetdata.src_id_type = SILC_ID_CLIENT;
911 packetdata.dst_id = silc_id_id2str(dst_id, dst_id_type);
912 packetdata.dst_id_len = silc_id_get_len(dst_id_type);
913 packetdata.dst_id_type = dst_id_type;
915 packetdata.dst_id = NULL;
916 packetdata.dst_id_len = 0;
917 packetdata.dst_id_type = SILC_ID_NONE;
919 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
920 packetdata.src_id_len + packetdata.dst_id_len;
921 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
923 /* Prepare outgoing data buffer for packet sending */
924 silc_packet_send_prepare(sock,
925 SILC_PACKET_HEADER_LEN +
926 packetdata.src_id_len +
927 packetdata.dst_id_len,
931 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
933 packetdata.buffer = sock->outbuf;
935 /* Put the data to the buffer */
936 if (data && data_len)
937 silc_buffer_put(sock->outbuf, data, data_len);
939 /* Create the outgoing packet */
940 silc_packet_assemble(&packetdata);
942 /* Encrypt the packet */
944 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
946 SILC_LOG_HEXDUMP(("Packet, len %d", sock->outbuf->len),
947 sock->outbuf->data, sock->outbuf->len);
949 /* Now actually send the packet */
950 silc_client_packet_send_real(client, sock, force_send);
953 /* Sends packet to a channel. Packet to channel is always encrypted
954 differently from "normal" packets. SILC header of the packet is
955 encrypted with the next receiver's key and the rest of the packet is
956 encrypted with the channel specific key. Padding and HMAC is computed
957 with the next receiver's key. */
959 void silc_client_packet_send_to_channel(SilcClient client,
960 SilcSocketConnection sock,
961 SilcChannelEntry channel,
963 unsigned int data_len,
967 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
969 SilcPacketContext packetdata;
972 unsigned char *id_string;
974 SILC_LOG_DEBUG(("Sending packet to channel"));
976 if (!channel || !channel->key) {
977 client->ops->say(client, conn,
978 "Cannot talk to channel: key does not exist");
984 for (i = 0; i < 16; i++) channel->iv[i] = silc_rng_get_byte(client->rng);
986 silc_hash_make(client->md5hash, channel->iv, 16, channel->iv);
988 /* Encode the channel payload */
989 payload = silc_channel_payload_encode(data_len, data, 16, channel->iv,
992 client->ops->say(client, conn,
993 "Error: Could not create packet to be sent to channel");
997 /* Get data used in packet header encryption, keys and stuff. Rest
998 of the packet (the payload) is, however, encrypted with the
999 specified channel key. */
1000 cipher = conn->send_key;
1002 id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1004 /* Set the packet context pointers. The destination ID is always
1005 the Channel ID of the channel. Server and router will handle the
1006 distribution of the packet. */
1007 packetdata.flags = 0;
1008 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
1009 packetdata.src_id = conn->local_id_data;
1010 packetdata.src_id_len = SILC_ID_CLIENT_LEN;
1011 packetdata.src_id_type = SILC_ID_CLIENT;
1012 packetdata.dst_id = id_string;
1013 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
1014 packetdata.dst_id_type = SILC_ID_CHANNEL;
1015 packetdata.truelen = payload->len + SILC_PACKET_HEADER_LEN +
1016 packetdata.src_id_len + packetdata.dst_id_len;
1017 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
1018 packetdata.src_id_len +
1019 packetdata.dst_id_len));
1021 /* Prepare outgoing data buffer for packet sending */
1022 silc_packet_send_prepare(sock,
1023 SILC_PACKET_HEADER_LEN +
1024 packetdata.src_id_len +
1025 packetdata.dst_id_len,
1029 packetdata.buffer = sock->outbuf;
1031 /* Encrypt payload of the packet. This is encrypted with the channel key. */
1032 channel->channel_key->cipher->encrypt(channel->channel_key->context,
1033 payload->data, payload->data,
1034 payload->len - 16, /* -IV_LEN */
1037 /* Put the actual encrypted payload data into the buffer. */
1038 silc_buffer_put(sock->outbuf, payload->data, payload->len);
1040 /* Create the outgoing packet */
1041 silc_packet_assemble(&packetdata);
1043 /* Encrypt the header and padding of the packet. This is encrypted
1044 with normal session key shared with our server. */
1045 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
1046 packetdata.src_id_len + packetdata.dst_id_len +
1049 SILC_LOG_HEXDUMP(("Packet to channel, len %d", sock->outbuf->len),
1050 sock->outbuf->data, sock->outbuf->len);
1052 /* Now actually send the packet */
1053 silc_client_packet_send_real(client, sock, force_send);
1054 silc_buffer_free(payload);
1055 silc_free(id_string);
1058 /* Sends private message to remote client. If private message key has
1059 not been set with this client then the message will be encrypted using
1060 normal session keys. Private messages are special packets in SILC
1061 network hence we need this own function for them. This is similiar
1062 to silc_client_packet_send_to_channel except that we send private
1065 void silc_client_packet_send_private_message(SilcClient client,
1066 SilcSocketConnection sock,
1067 SilcClientEntry client_entry,
1068 unsigned char *data,
1069 unsigned int data_len,
1072 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1074 SilcPacketContext packetdata;
1075 unsigned int nick_len;
1079 SILC_LOG_DEBUG(("Sending private message"));
1081 /* Create private message payload */
1082 nick_len = strlen(conn->nickname);
1083 buffer = silc_buffer_alloc(2 + nick_len + data_len);
1084 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
1085 silc_buffer_format(buffer,
1086 SILC_STR_UI_SHORT(nick_len),
1087 SILC_STR_UI_XNSTRING(conn->nickname,
1089 SILC_STR_UI_XNSTRING(data, data_len),
1092 /* If we don't have private message specific key then private messages
1093 are just as any normal packet thus call normal packet sending. If
1094 the key exist then the encryption process is a bit different and
1095 will be done in the rest of this function. */
1096 if (!client_entry->send_key) {
1097 silc_client_packet_send(client, sock, SILC_PACKET_PRIVATE_MESSAGE,
1098 client_entry->id, SILC_ID_CLIENT, NULL, NULL,
1099 buffer->data, buffer->len, force_send);
1103 /* We have private message specific key */
1105 /* Get data used in the encryption */
1106 cipher = client_entry->send_key;
1109 /* Set the packet context pointers. */
1110 packetdata.flags = 0;
1111 packetdata.type = SILC_PACKET_PRIVATE_MESSAGE;
1112 packetdata.src_id = conn->local_id_data;
1113 packetdata.src_id_len = SILC_ID_CLIENT_LEN;
1114 packetdata.src_id_type = SILC_ID_CLIENT;
1116 packetdata.dst_id = silc_id_id2str(client_entry->id, SILC_ID_CLIENT);
1118 packetdata.dst_id = conn->local_id_data;
1119 packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
1120 packetdata.dst_id_type = SILC_ID_CLIENT;
1121 packetdata.truelen = buffer->len + SILC_PACKET_HEADER_LEN +
1122 packetdata.src_id_len + packetdata.dst_id_len;
1123 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
1124 packetdata.src_id_len +
1125 packetdata.dst_id_len));
1127 /* Prepare outgoing data buffer for packet sending */
1128 silc_packet_send_prepare(sock,
1129 SILC_PACKET_HEADER_LEN +
1130 packetdata.src_id_len +
1131 packetdata.dst_id_len,
1135 packetdata.buffer = sock->outbuf;
1137 /* Encrypt payload of the packet. Encrypt with private message specific
1138 key if it exist, otherwise with session key. */
1139 cipher->cipher->encrypt(cipher->context, buffer->data, buffer->data,
1140 buffer->len, cipher->iv);
1142 /* Put the actual encrypted payload data into the buffer. */
1143 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
1145 /* Create the outgoing packet */
1146 silc_packet_assemble(&packetdata);
1148 /* Encrypt the header and padding of the packet. */
1149 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
1150 packetdata.src_id_len + packetdata.dst_id_len +
1153 SILC_LOG_HEXDUMP(("Private message packet, len %d", sock->outbuf->len),
1154 sock->outbuf->data, sock->outbuf->len);
1156 /* Now actually send the packet */
1157 silc_client_packet_send_real(client, sock, force_send);
1158 silc_free(packetdata.dst_id);
1164 /* Closes connection to remote end. Free's all allocated data except
1165 for some information such as nickname etc. that are valid at all time. */
1167 void silc_client_close_connection(SilcClient client,
1168 SilcSocketConnection sock)
1170 SilcClientConnection conn;
1172 /* We won't listen for this connection anymore */
1173 silc_schedule_unset_listen_fd(sock->sock);
1175 /* Unregister all tasks */
1176 silc_task_unregister_by_fd(client->io_queue, sock->sock);
1177 silc_task_unregister_by_fd(client->timeout_queue, sock->sock);
1179 /* Close the actual connection */
1180 silc_net_close_connection(sock->sock);
1182 client->ops->say(client, sock->user_data,
1183 "Closed connection to host %s", sock->hostname);
1185 /* Free everything */
1186 if (sock->user_data) {
1187 conn = (SilcClientConnection)sock->user_data;
1189 /* XXX Free all client entries and channel entries. */
1191 /* Clear ID caches */
1192 silc_idcache_del_all(conn->client_cache);
1193 silc_idcache_del_all(conn->channel_cache);
1196 if (conn->remote_host)
1197 silc_free(conn->remote_host);
1199 silc_free(conn->local_id);
1200 if (conn->local_id_data)
1201 silc_free(conn->local_id_data);
1203 silc_cipher_free(conn->send_key);
1204 if (conn->receive_key)
1205 silc_cipher_free(conn->receive_key);
1207 silc_hmac_free(conn->hmac);
1208 if (conn->hmac_key) {
1209 memset(conn->hmac_key, 0, conn->hmac_key_len);
1210 silc_free(conn->hmac_key);
1212 if (conn->pending_commands)
1213 silc_dlist_uninit(conn->pending_commands);
1216 conn->remote_port = 0;
1217 conn->remote_type = 0;
1218 conn->send_key = NULL;
1219 conn->receive_key = NULL;
1221 conn->hmac_key = NULL;
1222 conn->hmac_key_len = 0;
1223 conn->local_id = NULL;
1224 conn->local_id_data = NULL;
1225 conn->remote_host = NULL;
1226 conn->current_channel = NULL;
1227 conn->pending_commands = NULL;
1229 silc_client_del_connection(client, conn);
1232 if (sock->protocol) {
1233 silc_protocol_free(sock->protocol);
1234 sock->protocol = NULL;
1236 silc_socket_free(sock);
1239 /* Called when we receive disconnection packet from server. This
1240 closes our end properly and displays the reason of the disconnection
1243 void silc_client_disconnected_by_server(SilcClient client,
1244 SilcSocketConnection sock,
1249 SILC_LOG_DEBUG(("Server disconnected us, sock %d", sock->sock));
1251 msg = silc_calloc(message->len + 1, sizeof(char));
1252 memcpy(msg, message->data, message->len);
1253 client->ops->say(client, sock->user_data, msg);
1256 SILC_SET_DISCONNECTED(sock);
1257 silc_client_close_connection(client, sock);
1260 /* Received error message from server. Display it on the screen.
1261 We don't take any action what so ever of the error message. */
1263 void silc_client_error_by_server(SilcClient client,
1264 SilcSocketConnection sock,
1269 msg = silc_calloc(message->len + 1, sizeof(char));
1270 memcpy(msg, message->data, message->len);
1271 client->ops->say(client, sock->user_data, msg);
1275 /* Called when notify is received and some async operation (such as command)
1276 is required before processing the notify message. This calls again the
1277 silc_client_notify_by_server and reprocesses the original notify packet. */
1279 static void silc_client_notify_by_server_pending(void *context)
1281 SilcPacketContext *p = (SilcPacketContext *)context;
1282 silc_client_notify_by_server(p->context, p->sock, p);
1285 /* Destructor for the pending command callback */
1287 static void silc_client_notify_by_server_destructor(void *context)
1289 silc_packet_context_free((SilcPacketContext *)context);
1292 /* Resolve client information from server by Client ID. */
1294 static void silc_client_notify_by_server_resolve(SilcClient client,
1295 SilcClientConnection conn,
1296 SilcPacketContext *packet,
1297 SilcClientID *client_id)
1299 SilcPacketContext *p = silc_packet_context_dup(packet);
1300 SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1302 p->context = (void *)client;
1303 p->sock = conn->sock;
1305 silc_client_send_command(client, conn, SILC_COMMAND_WHOIS, ++conn->cmd_ident,
1306 1, 3, idp->data, idp->len);
1307 silc_client_command_pending(conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
1308 silc_client_notify_by_server_destructor,
1309 silc_client_notify_by_server_pending, p);
1310 silc_buffer_free(idp);
1313 /* Received notify message from server */
1315 void silc_client_notify_by_server(SilcClient client,
1316 SilcSocketConnection sock,
1317 SilcPacketContext *packet)
1319 SilcBuffer buffer = packet->buffer;
1320 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1321 SilcNotifyPayload payload;
1322 SilcNotifyType type;
1323 SilcArgumentPayload args;
1325 SilcClientID *client_id = NULL;
1326 SilcChannelID *channel_id = NULL;
1327 SilcClientEntry client_entry;
1328 SilcClientEntry client_entry2;
1329 SilcChannelEntry channel;
1330 SilcChannelUser chu;
1331 SilcIDCacheEntry id_cache = NULL;
1333 unsigned int tmp_len, mode;
1335 payload = silc_notify_payload_parse(buffer);
1339 type = silc_notify_get_type(payload);
1340 args = silc_notify_get_args(payload);
1345 case SILC_NOTIFY_TYPE_NONE:
1346 /* Notify application */
1347 client->ops->notify(client, conn, type,
1348 silc_argument_get_arg_type(args, 1, NULL));
1351 case SILC_NOTIFY_TYPE_INVITE:
1353 * Someone invited me to a channel. Find Client and Channel entries
1354 * for the application.
1358 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1362 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1366 /* Find Client entry and if not found query it */
1367 client_entry = silc_idlist_get_client_by_id(client, conn, client_id);
1368 if (!client_entry) {
1369 silc_client_notify_by_server_resolve(client, conn, packet, client_id);
1373 /* Get Channel ID */
1374 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1378 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1382 /* XXX Will ALWAYS fail because currently we don't have way to resolve
1383 channel information for channel that we're not joined to. */
1384 /* XXX ways to fix: use (extended) LIST command, or define the channel
1385 name to the notfy type when name resolving is not mandatory. */
1386 /* Find channel entry */
1387 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1388 SILC_ID_CHANNEL, &id_cache))
1391 channel = (SilcChannelEntry)id_cache->context;
1393 /* Notify application */
1394 client->ops->notify(client, conn, type, client_entry, channel);
1397 case SILC_NOTIFY_TYPE_JOIN:
1399 * Someone has joined to a channel. Get their ID and nickname and
1400 * cache them for later use.
1404 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1408 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1412 /* Find Client entry and if not found query it */
1413 client_entry = silc_idlist_get_client_by_id(client, conn, client_id);
1414 if (!client_entry) {
1415 silc_client_notify_by_server_resolve(client, conn, packet, client_id);
1419 /* If nickname or username hasn't been resolved, do so */
1420 if (!client_entry->nickname || !client_entry->username) {
1421 silc_client_notify_by_server_resolve(client, conn, packet, client_id);
1425 /* Get Channel ID */
1426 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1430 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1434 /* Get channel entry */
1435 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1436 SILC_ID_CHANNEL, &id_cache))
1439 channel = (SilcChannelEntry)id_cache->context;
1441 /* Add client to channel */
1442 chu = silc_calloc(1, sizeof(*chu));
1443 chu->client = client_entry;
1444 silc_list_add(channel->clients, chu);
1446 /* XXX add support for multiple same nicks on same channel. Check
1449 /* Notify application. The channel entry is sent last as this notify
1450 is for channel but application don't know it from the arguments
1452 client->ops->notify(client, conn, type, client_entry, channel);
1455 case SILC_NOTIFY_TYPE_LEAVE:
1457 * Someone has left a channel. We will remove it from the channel but
1458 * we'll keep it in the cache in case we'll need it later.
1462 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1466 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1470 /* Find Client entry */
1472 silc_idlist_get_client_by_id(client, conn, client_id);
1476 /* Get channel entry */
1477 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1481 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1482 SILC_ID_CHANNEL, &id_cache))
1485 channel = (SilcChannelEntry)id_cache->context;
1487 /* Remove client from channel */
1488 silc_list_start(channel->clients);
1489 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1490 if (chu->client == client_entry) {
1491 silc_list_del(channel->clients, chu);
1497 /* Notify application. The channel entry is sent last as this notify
1498 is for channel but application don't know it from the arguments
1500 client->ops->notify(client, conn, type, client_entry, channel);
1503 case SILC_NOTIFY_TYPE_SIGNOFF:
1505 * Someone left SILC. We'll remove it from all channels and from cache.
1509 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1513 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1517 /* Find Client entry */
1519 silc_idlist_get_client_by_id(client, conn, client_id);
1523 /* Remove from all channels */
1524 silc_client_remove_from_channels(client, conn, client_entry);
1526 /* Remove from cache */
1527 silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT,
1530 /* Get signoff message */
1531 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1535 /* Notify application */
1536 client->ops->notify(client, conn, type, client_entry, tmp);
1539 if (client_entry->nickname)
1540 silc_free(client_entry->nickname);
1541 if (client_entry->server)
1542 silc_free(client_entry->server);
1543 if (client_entry->id)
1544 silc_free(client_entry->id);
1545 if (client_entry->send_key)
1546 silc_cipher_free(client_entry->send_key);
1547 if (client_entry->receive_key)
1548 silc_cipher_free(client_entry->receive_key);
1551 case SILC_NOTIFY_TYPE_TOPIC_SET:
1553 * Someone set the topic on a channel.
1557 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1561 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1565 /* Find Client entry */
1567 silc_idlist_get_client_by_id(client, conn, client_id);
1572 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1576 /* Get channel entry */
1577 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1581 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1582 SILC_ID_CHANNEL, &id_cache))
1585 channel = (SilcChannelEntry)id_cache->context;
1587 /* Notify application. The channel entry is sent last as this notify
1588 is for channel but application don't know it from the arguments
1590 client->ops->notify(client, conn, type, client_entry, tmp, channel);
1593 case SILC_NOTIFY_TYPE_NICK_CHANGE:
1595 * Someone changed their nickname. If we don't have entry for the new
1596 * ID we will query it and return here after it's done. After we've
1597 * returned we fetch the old entry and free it and notify the
1601 /* Get new Client ID */
1602 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1606 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1611 if (!SILC_ID_CLIENT_COMPARE(client_id, conn->local_id))
1614 /* Find Client entry and if not found query it */
1616 silc_idlist_get_client_by_id(client, conn, client_id);
1617 if (!client_entry2) {
1618 silc_client_notify_by_server_resolve(client, conn, packet, client_id);
1621 silc_free(client_id);
1623 /* Get old Client ID */
1624 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1628 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1632 /* Find old Client entry */
1634 silc_idlist_get_client_by_id(client, conn, client_id);
1638 /* Remove the old from cache */
1639 silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT,
1642 /* Replace old ID entry with new one on all channels. */
1643 silc_client_replace_from_channels(client, conn, client_entry,
1646 /* Notify application */
1647 client->ops->notify(client, conn, type, client_entry, client_entry2);
1650 if (client_entry->nickname)
1651 silc_free(client_entry->nickname);
1652 if (client_entry->server)
1653 silc_free(client_entry->server);
1654 if (client_entry->id)
1655 silc_free(client_entry->id);
1656 if (client_entry->send_key)
1657 silc_cipher_free(client_entry->send_key);
1658 if (client_entry->receive_key)
1659 silc_cipher_free(client_entry->receive_key);
1662 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
1664 * Someone changed a channel mode
1668 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1672 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1676 /* Find Client entry */
1678 silc_idlist_get_client_by_id(client, conn, client_id);
1683 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1687 SILC_GET32_MSB(mode, tmp);
1689 /* Get channel entry */
1690 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1694 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1695 SILC_ID_CHANNEL, &id_cache))
1698 channel = (SilcChannelEntry)id_cache->context;
1700 /* Save the new mode */
1701 channel->mode = mode;
1703 /* Notify application. The channel entry is sent last as this notify
1704 is for channel but application don't know it from the arguments
1706 client->ops->notify(client, conn, type, client_entry, mode, channel);
1709 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
1711 * Someone changed user's mode on a channel
1715 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1719 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1723 /* Find Client entry */
1725 silc_idlist_get_client_by_id(client, conn, client_id);
1730 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1734 SILC_GET32_MSB(mode, tmp);
1736 /* Get target Client ID */
1737 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1741 silc_free(client_id);
1742 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1746 /* Find target Client entry */
1748 silc_idlist_get_client_by_id(client, conn, client_id);
1752 /* Get channel entry */
1753 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1757 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1758 SILC_ID_CHANNEL, &id_cache))
1761 channel = (SilcChannelEntry)id_cache->context;
1764 silc_list_start(channel->clients);
1765 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1766 if (chu->client == client_entry) {
1772 /* Notify application. The channel entry is sent last as this notify
1773 is for channel but application don't know it from the arguments
1775 client->ops->notify(client, conn, type, client_entry, mode,
1776 client_entry2, channel);
1779 case SILC_NOTIFY_TYPE_MOTD:
1781 * Received Message of the day
1785 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1789 /* Notify application */
1790 client->ops->notify(client, conn, type, tmp);
1793 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1795 * Router has enforced a new ID to a channel. Let's change the old
1796 * ID to the one provided here.
1799 /* Get the old ID */
1800 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1803 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1807 /* Get the channel entry */
1808 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1809 SILC_ID_CHANNEL, &id_cache))
1812 channel = (SilcChannelEntry)id_cache->context;
1814 /* Free the old ID */
1815 silc_free(channel_id);
1816 silc_free(channel->id);
1818 /* Get the new ID */
1819 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1822 channel->id = silc_id_payload_parse_id(tmp, tmp_len);
1826 id_cache->id = (void *)channel->id;
1828 /* Notify application */
1829 client->ops->notify(client, conn, type, channel, channel);
1832 case SILC_NOTIFY_TYPE_KICKED:
1834 * A client (maybe me) was kicked from a channel
1838 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1842 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1846 /* Find Client entry */
1848 silc_idlist_get_client_by_id(client, conn, client_id);
1852 /* Get channel entry */
1853 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1857 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1858 SILC_ID_CHANNEL, &id_cache))
1861 channel = (SilcChannelEntry)id_cache->context;
1864 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1866 /* Notify application. The channel entry is sent last as this notify
1867 is for channel but application don't know it from the arguments
1869 client->ops->notify(client, conn, type, client_entry, tmp, channel);
1871 /* If I was kicked from channel, remove the channel */
1872 if (client_entry == conn->local_entry) {
1873 if (conn->current_channel == channel)
1874 conn->current_channel = NULL;
1875 silc_idcache_del_by_id(conn->channel_cache,
1876 SILC_ID_CHANNEL, channel->id);
1877 silc_free(channel->channel_name);
1878 silc_free(channel->id);
1879 silc_free(channel->key);
1880 silc_cipher_free(channel->channel_key);
1890 silc_notify_payload_free(payload);
1892 silc_free(client_id);
1894 silc_free(channel_id);
1897 /* Processes the received new Client ID from server. Old Client ID is
1898 deleted from cache and new one is added. */
1900 void silc_client_receive_new_id(SilcClient client,
1901 SilcSocketConnection sock,
1904 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1905 int connecting = FALSE;
1907 if (!conn->local_entry)
1910 /* Delete old ID from ID cache */
1911 silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT, conn->local_id);
1913 /* Save the new ID */
1915 silc_free(conn->local_id);
1916 if (conn->local_id_data)
1917 silc_free(conn->local_id_data);
1919 conn->local_id = silc_id_payload_get_id(idp);
1920 conn->local_id_data = silc_id_payload_get_data(idp);
1921 conn->local_id_data_len = silc_id_payload_get_len(idp);;
1923 if (!conn->local_entry)
1924 conn->local_entry = silc_calloc(1, sizeof(*conn->local_entry));
1926 conn->local_entry->nickname = conn->nickname;
1927 if (!conn->local_entry->username) {
1928 conn->local_entry->username =
1929 silc_calloc(strlen(client->username) + strlen(client->hostname) + 1,
1930 sizeof(conn->local_entry->username));
1931 sprintf(conn->local_entry->username, "%s@%s", client->username,
1934 conn->local_entry->server = strdup(conn->remote_host);
1935 conn->local_entry->id = conn->local_id;
1937 /* Put it to the ID cache */
1938 silc_idcache_add(conn->client_cache, conn->nickname, SILC_ID_CLIENT,
1939 conn->local_id, (void *)conn->local_entry, TRUE);
1941 /* Notify application of successful connection. We do it here now that
1942 we've received the Client ID and are allowed to send traffic. */
1944 client->ops->connect(client, conn, TRUE);
1947 /* Processed received Channel ID for a channel. This is called when client
1948 joins to channel and server replies with channel ID. The ID is cached. */
1950 void silc_client_new_channel_id(SilcClient client,
1951 SilcSocketConnection sock,
1953 unsigned int mode, SilcIDPayload idp)
1955 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1956 SilcChannelEntry channel;
1958 SILC_LOG_DEBUG(("New channel ID"));
1960 channel = silc_calloc(1, sizeof(*channel));
1961 channel->channel_name = channel_name;
1962 channel->id = silc_id_payload_get_id(idp);
1963 channel->mode = mode;
1964 silc_list_init(channel->clients, struct SilcChannelUserStruct, next);
1966 conn->current_channel = channel;
1968 /* Put it to the ID cache */
1969 silc_idcache_add(conn->channel_cache, channel_name, SILC_ID_CHANNEL,
1970 (void *)channel->id, (void *)channel, TRUE);
1973 /* Saves channel key from encoded `key_payload'. This is used when we
1974 receive Channel Key Payload and when we are processing JOIN command
1977 void silc_client_save_channel_key(SilcClientConnection conn,
1978 SilcBuffer key_payload,
1979 SilcChannelEntry channel)
1981 unsigned char *id_string, *key, *cipher;
1982 unsigned int tmp_len;
1984 SilcIDCacheEntry id_cache = NULL;
1985 SilcChannelKeyPayload payload;
1987 payload = silc_channel_key_payload_parse(key_payload);
1991 id_string = silc_channel_key_get_id(payload, &tmp_len);
1993 silc_channel_key_payload_free(payload);
1997 id = silc_id_str2id(id_string, tmp_len, SILC_ID_CHANNEL);
1999 silc_channel_key_payload_free(payload);
2005 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
2006 SILC_ID_CHANNEL, &id_cache))
2009 /* Get channel entry */
2010 channel = (SilcChannelEntry)id_cache->context;
2014 key = silc_channel_key_get_key(payload, &tmp_len);
2015 cipher = silc_channel_key_get_cipher(payload, NULL);
2016 channel->key_len = tmp_len;
2017 channel->key = silc_calloc(tmp_len, sizeof(*channel->key));
2018 memcpy(channel->key, key, tmp_len);
2020 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
2021 conn->client->ops->say(conn->client, conn,
2022 "Cannot talk to channel: unsupported cipher %s", cipher);
2025 channel->channel_key->cipher->set_key(channel->channel_key->context,
2028 /* Client is now joined to the channel */
2029 channel->on_channel = TRUE;
2033 silc_channel_key_payload_free(payload);
2036 /* Processes received key for channel. The received key will be used
2037 to protect the traffic on the channel for now on. Client must receive
2038 the key to the channel before talking on the channel is possible.
2039 This is the key that server has generated, this is not the channel
2040 private key, it is entirely local setting. */
2042 void silc_client_receive_channel_key(SilcClient client,
2043 SilcSocketConnection sock,
2046 SILC_LOG_DEBUG(("Received key for channel"));
2049 silc_client_save_channel_key(sock->user_data, packet, NULL);
2052 /* Process received message to a channel (or from a channel, really). This
2053 decrypts the channel message with channel specific key and parses the
2054 channel payload. Finally it displays the message on the screen. */
2056 void silc_client_channel_message(SilcClient client,
2057 SilcSocketConnection sock,
2058 SilcPacketContext *packet)
2060 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
2061 SilcBuffer buffer = packet->buffer;
2062 SilcChannelPayload payload = NULL;
2063 SilcChannelID *id = NULL;
2064 SilcChannelEntry channel;
2065 SilcChannelUser chu;
2066 SilcIDCacheEntry id_cache = NULL;
2067 SilcClientID *client_id = NULL;
2071 if (packet->dst_id_type != SILC_ID_CHANNEL)
2074 client_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2078 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
2082 /* Find the channel entry from channels on this connection */
2083 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
2084 SILC_ID_CHANNEL, &id_cache))
2087 channel = (SilcChannelEntry)id_cache->context;
2089 /* Decrypt the channel message payload. Push the IV out of the way,
2090 since it is not encrypted (after pushing buffer->tail has the IV). */
2091 silc_buffer_push_tail(buffer, 16);
2092 channel->channel_key->cipher->decrypt(channel->channel_key->context,
2093 buffer->data, buffer->data,
2094 buffer->len, buffer->tail);
2095 silc_buffer_pull_tail(buffer, 16);
2097 /* Parse the channel message payload */
2098 payload = silc_channel_payload_parse(buffer);
2102 /* Find client entry */
2103 silc_list_start(channel->clients);
2104 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
2105 if (!SILC_ID_CLIENT_COMPARE(chu->client->id, client_id)) {
2111 /* Pass the message to application */
2112 client->ops->channel_message(client, conn, found ? chu->client : NULL,
2113 channel, silc_channel_get_data(payload, NULL));
2119 silc_free(client_id);
2121 silc_channel_payload_free(payload);
2124 /* Private message received. This processes the private message and
2125 finally displays it on the screen. */
2127 void silc_client_private_message(SilcClient client,
2128 SilcSocketConnection sock,
2129 SilcPacketContext *packet)
2131 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
2132 SilcBuffer buffer = packet->buffer;
2133 SilcIDCacheEntry id_cache;
2134 SilcClientID *remote_id = NULL;
2135 SilcClientEntry remote_client;
2136 unsigned short nick_len;
2137 unsigned char *nickname, *message = NULL;
2140 if (packet->src_id_type != SILC_ID_CLIENT)
2144 ret = silc_buffer_unformat(buffer,
2145 SILC_STR_UI16_NSTRING_ALLOC(&nickname, &nick_len),
2150 silc_buffer_pull(buffer, 2 + nick_len);
2152 message = silc_calloc(buffer->len + 1, sizeof(char));
2153 memcpy(message, buffer->data, buffer->len);
2155 remote_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2160 /* Check whether we know this client already */
2161 if (!silc_idcache_find_by_id_one(conn->client_cache, remote_id,
2162 SILC_ID_CLIENT, &id_cache))
2164 /* Allocate client entry */
2165 remote_client = silc_calloc(1, sizeof(*remote_client));
2166 remote_client->id = remote_id;
2167 silc_parse_nickname(nickname, &remote_client->nickname,
2168 &remote_client->server, &remote_client->num);
2170 /* Save the client to cache */
2171 silc_idcache_add(conn->client_cache, remote_client->nickname,
2172 SILC_ID_CLIENT, remote_client->id, remote_client,
2175 remote_client = (SilcClientEntry)id_cache->context;
2178 /* Pass the private message to application */
2179 client->ops->private_message(client, conn, remote_client, message);
2181 /* See if we are away (gone). If we are away we will reply to the
2182 sender with the set away message. */
2183 if (conn->away && conn->away->away) {
2184 /* If it's me, ignore */
2185 if (!SILC_ID_CLIENT_COMPARE(remote_id, conn->local_id))
2188 /* Send the away message */
2189 silc_client_packet_send_private_message(client, sock, remote_client,
2191 strlen(conn->away->away), TRUE);
2196 silc_free(remote_id);
2199 memset(message, 0, buffer->len);
2202 silc_free(nickname);
2205 /* Removes a client entry from all channel it has joined. This really is
2206 a performance killer (client_entry should have pointers to channel
2209 void silc_client_remove_from_channels(SilcClient client,
2210 SilcClientConnection conn,
2211 SilcClientEntry client_entry)
2213 SilcIDCacheEntry id_cache;
2214 SilcIDCacheList list;
2215 SilcChannelEntry channel;
2216 SilcChannelUser chu;
2218 if (!silc_idcache_find_by_id(conn->channel_cache, SILC_ID_CACHE_ANY,
2219 SILC_ID_CHANNEL, &list))
2222 silc_idcache_list_first(list, &id_cache);
2223 channel = (SilcChannelEntry)id_cache->context;
2227 /* Remove client from channel */
2228 silc_list_start(channel->clients);
2229 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
2230 if (chu->client == client_entry) {
2231 silc_list_del(channel->clients, chu);
2237 if (!silc_idcache_list_next(list, &id_cache))
2240 channel = (SilcChannelEntry)id_cache->context;
2243 silc_idcache_list_free(list);
2246 /* Replaces `old' client entries from all channels to `new' client entry.
2247 This can be called for example when nickname changes and old ID entry
2248 is replaced from ID cache with the new one. If the old ID entry is only
2249 updated, then this fucntion needs not to be called. */
2251 void silc_client_replace_from_channels(SilcClient client,
2252 SilcClientConnection conn,
2253 SilcClientEntry old,
2254 SilcClientEntry new)
2256 SilcIDCacheEntry id_cache;
2257 SilcIDCacheList list;
2258 SilcChannelEntry channel;
2259 SilcChannelUser chu;
2261 if (!silc_idcache_find_by_id(conn->channel_cache, SILC_ID_CACHE_ANY,
2262 SILC_ID_CHANNEL, &list))
2265 silc_idcache_list_first(list, &id_cache);
2266 channel = (SilcChannelEntry)id_cache->context;
2270 /* Replace client entry */
2271 silc_list_start(channel->clients);
2272 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
2273 if (chu->client == old) {
2279 if (!silc_idcache_list_next(list, &id_cache))
2282 channel = (SilcChannelEntry)id_cache->context;
2285 silc_idcache_list_free(list);
2288 /* Parses mode mask and returns the mode as string. */
2290 char *silc_client_chmode(unsigned int mode)
2297 memset(string, 0, sizeof(string));
2299 if (mode & SILC_CHANNEL_MODE_PRIVATE)
2300 strncat(string, "p", 1);
2302 if (mode & SILC_CHANNEL_MODE_SECRET)
2303 strncat(string, "s", 1);
2305 if (mode & SILC_CHANNEL_MODE_PRIVKEY)
2306 strncat(string, "k", 1);
2308 if (mode & SILC_CHANNEL_MODE_INVITE)
2309 strncat(string, "i", 1);
2311 if (mode & SILC_CHANNEL_MODE_TOPIC)
2312 strncat(string, "t", 1);
2314 if (mode & SILC_CHANNEL_MODE_ULIMIT)
2315 strncat(string, "l", 1);
2317 if (mode & SILC_CHANNEL_MODE_PASSPHRASE)
2318 strncat(string, "a", 1);
2320 /* Rest of mode is ignored */
2322 return strdup(string);
2325 /* Parses channel user mode mask and returns te mode as string */
2327 char *silc_client_chumode(unsigned int mode)
2334 memset(string, 0, sizeof(string));
2336 if (mode & SILC_CHANNEL_UMODE_CHANFO)
2337 strncat(string, "f", 1);
2339 if (mode & SILC_CHANNEL_UMODE_CHANOP)
2340 strncat(string, "o", 1);
2342 return strdup(string);
2345 /* Parses channel user mode and returns it as special mode character. */
2347 char *silc_client_chumode_char(unsigned int mode)
2354 memset(string, 0, sizeof(string));
2356 if (mode & SILC_CHANNEL_UMODE_CHANFO)
2357 strncat(string, "*", 1);
2359 if (mode & SILC_CHANNEL_UMODE_CHANOP)
2360 strncat(string, "@", 1);
2362 return strdup(string);