5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "silcincludes.h"
22 #include "silcclient.h"
23 #include "client_internal.h"
25 /* Static task callback prototypes */
26 SILC_TASK_CALLBACK(silc_client_connect_to_server_start);
27 SILC_TASK_CALLBACK(silc_client_connect_to_server_second);
28 SILC_TASK_CALLBACK(silc_client_connect_to_server_final);
29 SILC_TASK_CALLBACK(silc_client_rekey_callback);
30 SILC_TASK_CALLBACK(silc_client_rekey_final);
32 static bool silc_client_packet_parse(SilcPacketParserContext *parser_context,
34 static void silc_client_packet_parse_type(SilcClient client,
35 SilcSocketConnection sock,
36 SilcPacketContext *packet);
37 void silc_client_resolve_auth_method(bool success,
38 SilcProtocolAuthMeth auth_meth,
39 const unsigned char *auth_data,
40 SilcUInt32 auth_data_len, void *context);
42 /* Allocates new client object. This has to be done before client may
43 work. After calling this one must call silc_client_init to initialize
44 the client. The `application' is application specific user data pointer
45 and caller must free it. */
47 SilcClient silc_client_alloc(SilcClientOperations *ops,
48 SilcClientParams *params,
50 const char *silc_version)
52 SilcClient new_client;
54 new_client = silc_calloc(1, sizeof(*new_client));
55 new_client->application = application;
57 new_client->internal = silc_calloc(1, sizeof(*new_client->internal));
58 new_client->internal->ops = ops;
59 new_client->internal->params =
60 silc_calloc(1, sizeof(*new_client->internal->params));
61 new_client->internal->silc_client_version = strdup(silc_version);
64 memcpy(new_client->internal->params, params, sizeof(*params));
66 if (!new_client->internal->params->task_max)
67 new_client->internal->params->task_max = 200;
69 if (!new_client->internal->params->rekey_secs)
70 new_client->internal->params->rekey_secs = 3600;
72 if (!new_client->internal->params->connauth_request_secs)
73 new_client->internal->params->connauth_request_secs = 2;
75 new_client->internal->params->
76 nickname_format[sizeof(new_client->internal->
77 params->nickname_format) - 1] = 0;
82 /* Frees client object and its internals. */
84 void silc_client_free(SilcClient client)
88 silc_rng_free(client->rng);
90 silc_free(client->internal->params);
91 silc_free(client->internal->silc_client_version);
92 silc_free(client->internal);
97 /* Initializes the client. This makes all the necessary steps to make
98 the client ready to be run. One must call silc_client_run to run the
99 client. Returns FALSE if error occured, TRUE otherwise. */
101 int silc_client_init(SilcClient client)
103 SILC_LOG_DEBUG(("Initializing client"));
105 /* Initialize hash functions for client to use */
106 silc_hash_alloc("md5", &client->internal->md5hash);
107 silc_hash_alloc("sha1", &client->internal->sha1hash);
109 /* Initialize none cipher */
110 silc_cipher_alloc("none", &client->internal->none_cipher);
112 /* Initialize random number generator */
113 client->rng = silc_rng_alloc();
114 silc_rng_init(client->rng);
115 silc_rng_global_init(client->rng);
117 /* Register protocols */
118 silc_client_protocols_register();
120 /* Initialize the scheduler */
122 silc_schedule_init(client->internal->params->task_max ?
123 client->internal->params->task_max : 200, client);
124 if (!client->schedule)
127 /* Register commands */
128 silc_client_commands_register(client);
133 /* Stops the client. This is called to stop the client and thus to stop
136 void silc_client_stop(SilcClient client)
138 SILC_LOG_DEBUG(("Stopping client"));
140 silc_schedule_stop(client->schedule);
141 silc_schedule_uninit(client->schedule);
143 silc_client_protocols_unregister();
144 silc_client_commands_unregister(client);
146 SILC_LOG_DEBUG(("Client stopped"));
149 /* Runs the client. This starts the scheduler from the utility library.
150 When this functions returns the execution of the appliation is over. */
152 void silc_client_run(SilcClient client)
154 SILC_LOG_DEBUG(("Running client"));
156 /* Start the scheduler, the heart of the SILC client. When this returns
157 the program will be terminated. */
158 silc_schedule(client->schedule);
161 /* Runs the client and returns immeadiately. This function is used when
162 the SILC Client object indicated by the `client' is run under some
163 other scheduler, or event loop or main loop. On GUI applications,
164 for example this may be desired to use to run the client under the
165 GUI application's main loop. Typically the GUI application would
166 register an idle task that calls this function multiple times in
167 a second to quickly process the SILC specific data. */
169 void silc_client_run_one(SilcClient client)
171 /* Run the scheduler once. */
172 silc_schedule_one(client->schedule, 0);
175 static void silc_client_entry_destructor(SilcIDCache cache,
176 SilcIDCacheEntry entry)
178 silc_free(entry->name);
181 /* Allocates and adds new connection to the client. This adds the allocated
182 connection to the connection table and returns a pointer to it. A client
183 can have multiple connections to multiple servers. Every connection must
184 be added to the client using this function. User data `context' may
185 be sent as argument. This function is normally used only if the
186 application performed the connecting outside the library. The library
187 however may use this internally. */
190 silc_client_add_connection(SilcClient client,
191 SilcClientConnectionParams *params,
192 char *hostname, int port, void *context)
194 SilcClientConnection conn;
197 SILC_LOG_DEBUG(("Adding new connection to %s:%d", hostname, port));
199 conn = silc_calloc(1, sizeof(*conn));
201 /* Initialize ID caches */
202 conn->client_cache = silc_idcache_alloc(0, SILC_ID_CLIENT,
203 silc_client_entry_destructor);
204 conn->channel_cache = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
205 conn->server_cache = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
206 conn->client = client;
207 conn->remote_host = strdup(hostname);
208 conn->remote_port = port;
209 conn->context = context;
210 conn->pending_commands = silc_dlist_init();
211 conn->ftp_sessions = silc_dlist_init();
214 if (params->detach_data)
215 conn->params.detach_data = silc_memdup(params->detach_data,
216 params->detach_data_len);
217 conn->params.detach_data_len = params->detach_data_len;
220 /* Add the connection to connections table */
221 for (i = 0; i < client->internal->conns_count; i++)
222 if (client->internal->conns && !client->internal->conns[i]) {
223 client->internal->conns[i] = conn;
227 client->internal->conns =
228 silc_realloc(client->internal->conns, sizeof(*client->internal->conns)
229 * (client->internal->conns_count + 1));
230 client->internal->conns[client->internal->conns_count] = conn;
231 client->internal->conns_count++;
236 /* Removes connection from client. Frees all memory. */
238 void silc_client_del_connection(SilcClient client, SilcClientConnection conn)
242 for (i = 0; i < client->internal->conns_count; i++)
243 if (client->internal->conns[i] == conn) {
245 silc_idcache_free(conn->client_cache);
246 silc_idcache_free(conn->channel_cache);
247 silc_idcache_free(conn->server_cache);
248 if (conn->pending_commands)
249 silc_dlist_uninit(conn->pending_commands);
250 silc_free(conn->remote_host);
251 if (conn->ftp_sessions)
252 silc_dlist_uninit(conn->ftp_sessions);
255 client->internal->conns[i] = NULL;
259 /* Adds listener socket to the listener sockets table. This function is
260 used to add socket objects that are listeners to the client. This should
261 not be used to add other connection objects. */
263 void silc_client_add_socket(SilcClient client, SilcSocketConnection sock)
267 if (!client->internal->sockets) {
268 client->internal->sockets =
269 silc_calloc(1, sizeof(*client->internal->sockets));
270 client->internal->sockets[0] = silc_socket_dup(sock);
271 client->internal->sockets_count = 1;
275 for (i = 0; i < client->internal->sockets_count; i++) {
276 if (client->internal->sockets[i] == NULL) {
277 client->internal->sockets[i] = silc_socket_dup(sock);
282 client->internal->sockets =
283 silc_realloc(client->internal->sockets,
284 sizeof(*client->internal->sockets) *
285 (client->internal->sockets_count + 1));
286 client->internal->sockets[client->internal->sockets_count] =
287 silc_socket_dup(sock);
288 client->internal->sockets_count++;
291 /* Deletes listener socket from the listener sockets table. */
293 void silc_client_del_socket(SilcClient client, SilcSocketConnection sock)
297 if (!client->internal->sockets)
300 for (i = 0; i < client->internal->sockets_count; i++) {
301 if (client->internal->sockets[i] == sock) {
302 silc_socket_free(sock);
303 client->internal->sockets[i] = NULL;
310 silc_client_connect_to_server_internal(SilcClientInternalConnectContext *ctx)
314 /* XXX In the future we should give up this non-blocking connect all
315 together and use threads instead. */
316 /* Create connection to server asynchronously */
317 sock = silc_net_create_connection_async(NULL, ctx->port, ctx->host);
321 /* Register task that will receive the async connect and will
323 ctx->task = silc_schedule_task_add(ctx->client->schedule, sock,
324 silc_client_connect_to_server_start,
327 SILC_TASK_PRI_NORMAL);
328 silc_schedule_set_listen_fd(ctx->client->schedule, sock, SILC_TASK_WRITE);
335 /* Connects to remote server. This is the main routine used to connect
336 to SILC server. Returns -1 on error and the created socket otherwise.
337 The `context' is user context that is saved into the SilcClientConnection
338 that is created after the connection is created. Note that application
339 may handle the connecting process outside the library. If this is the
340 case then this function is not used at all. When the connecting is
341 done the `connect' client operation is called. */
343 int silc_client_connect_to_server(SilcClient client,
344 SilcClientConnectionParams *params,
345 int port, char *host, void *context)
347 SilcClientInternalConnectContext *ctx;
348 SilcClientConnection conn;
351 SILC_LOG_DEBUG(("Connecting to port %d of server %s",
354 conn = silc_client_add_connection(client, params, host, port, context);
356 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
357 "Connecting to port %d of server %s", port, host);
359 /* Allocate internal context for connection process. This is
360 needed as we are doing async connecting. */
361 ctx = silc_calloc(1, sizeof(*ctx));
362 ctx->client = client;
364 ctx->host = strdup(host);
368 /* Do the actual connecting process */
369 sock = silc_client_connect_to_server_internal(ctx);
371 silc_client_del_connection(client, conn);
375 /* Socket hostname and IP lookup callback that is called before actually
376 starting the key exchange. The lookup is called from the function
377 silc_client_start_key_exchange. */
379 static void silc_client_start_key_exchange_cb(SilcSocketConnection sock,
382 SilcClientConnection conn = (SilcClientConnection)context;
383 SilcClient client = conn->client;
384 SilcProtocol protocol;
385 SilcClientKEInternalContext *proto_ctx;
387 SILC_LOG_DEBUG(("Start"));
389 /* XXX We should most likely use the resolved host name instead of the
390 one user provided for us. */
391 silc_free(conn->sock->hostname);
392 conn->sock->hostname = strdup(conn->remote_host);
394 conn->sock->ip = strdup(conn->remote_host);
395 conn->sock->port = conn->remote_port;
397 /* Allocate internal Key Exchange context. This is sent to the
398 protocol as context. */
399 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
400 proto_ctx->client = (void *)client;
401 proto_ctx->sock = silc_socket_dup(conn->sock);
402 proto_ctx->rng = client->rng;
403 proto_ctx->responder = FALSE;
404 proto_ctx->send_packet = silc_client_protocol_ke_send_packet;
405 proto_ctx->verify = silc_client_protocol_ke_verify_key;
407 /* Perform key exchange protocol. silc_client_connect_to_server_final
408 will be called after the protocol is finished. */
409 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
410 &protocol, (void *)proto_ctx,
411 silc_client_connect_to_server_second);
413 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
414 "Error: Could not start key exchange protocol");
415 silc_net_close_connection(conn->sock->sock);
416 client->internal->ops->connect(client, conn, SILC_CLIENT_CONN_ERROR);
419 conn->sock->protocol = protocol;
421 /* Register the connection for network input and output. This sets
422 that scheduler will listen for incoming packets for this connection
423 and sets that outgoing packets may be sent to this connection as well.
424 However, this doesn't set the scheduler for outgoing traffic, it will
425 be set separately by calling SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT,
426 later when outgoing data is available. */
427 context = (void *)client;
428 SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(conn->sock->sock);
430 /* Execute the protocol */
431 silc_protocol_execute(protocol, client->schedule, 0, 0);
434 /* Start SILC Key Exchange (SKE) protocol to negotiate shared secret
435 key material between client and server. This function can be called
436 directly if application is performing its own connecting and does not
437 use the connecting provided by this library. This function is normally
438 used only if the application performed the connecting outside the library.
439 The library however may use this internally. */
441 void silc_client_start_key_exchange(SilcClient client,
442 SilcClientConnection conn,
445 /* Allocate new socket connection object */
446 silc_socket_alloc(fd, SILC_SOCKET_TYPE_SERVER, (void *)conn, &conn->sock);
448 /* Sometimes when doing quick reconnects the new socket may be same as
449 the old one and there might be pending stuff for the old socket.
450 If new one is same then those pending sutff might cause problems.
451 Make sure they do not do that. */
452 silc_schedule_task_del_by_fd(client->schedule, fd);
454 conn->nickname = (client->nickname ? strdup(client->nickname) :
455 strdup(client->username));
457 /* Resolve the remote hostname and IP address for our socket connection */
458 silc_socket_host_lookup(conn->sock, FALSE, silc_client_start_key_exchange_cb,
459 conn, client->schedule);
462 /* Callback called when error has occurred during connecting to the server.
463 The `connect' client operation will be called. */
465 SILC_TASK_CALLBACK(silc_client_connect_failure)
467 SilcClientKEInternalContext *ctx =
468 (SilcClientKEInternalContext *)context;
469 SilcClient client = (SilcClient)ctx->client;
471 client->internal->ops->connect(client, ctx->sock->user_data,
472 SILC_CLIENT_CONN_ERROR);
474 silc_packet_context_free(ctx->packet);
478 /* Start of the connection to the remote server. This is called after
479 succesful TCP/IP connection has been established to the remote host. */
481 SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
483 SilcClientInternalConnectContext *ctx =
484 (SilcClientInternalConnectContext *)context;
485 SilcClient client = ctx->client;
486 SilcClientConnection conn = ctx->conn;
487 int opt, opt_len = sizeof(opt);
489 SILC_LOG_DEBUG(("Start"));
491 /* Check the socket status as it might be in error */
492 silc_net_get_socket_opt(fd, SOL_SOCKET, SO_ERROR, &opt, &opt_len);
494 if (ctx->tries < 2) {
495 /* Connection failed but lets try again */
496 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
497 "Could not connect to server %s: %s",
498 ctx->host, strerror(opt));
499 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
500 "Connecting to port %d of server %s resumed",
501 ctx->port, ctx->host);
503 /* Unregister old connection try */
504 silc_schedule_unset_listen_fd(client->schedule, fd);
505 silc_net_close_connection(fd);
506 silc_schedule_task_del(client->schedule, ctx->task);
509 silc_client_connect_to_server_internal(ctx);
512 /* Connection failed and we won't try anymore */
513 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
514 "Could not connect to server %s: %s",
515 ctx->host, strerror(opt));
516 silc_schedule_unset_listen_fd(client->schedule, fd);
517 silc_net_close_connection(fd);
518 silc_schedule_task_del(client->schedule, ctx->task);
521 /* Notify application of failure */
522 client->internal->ops->connect(client, conn, SILC_CLIENT_CONN_ERROR);
523 silc_client_del_connection(client, conn);
528 silc_schedule_unset_listen_fd(client->schedule, fd);
529 silc_schedule_task_del(client->schedule, ctx->task);
532 silc_client_start_key_exchange(client, conn, fd);
535 /* Second part of the connecting to the server. This executed
536 authentication protocol. */
538 SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
540 SilcProtocol protocol = (SilcProtocol)context;
541 SilcClientKEInternalContext *ctx =
542 (SilcClientKEInternalContext *)protocol->context;
543 SilcClient client = (SilcClient)ctx->client;
544 SilcSocketConnection sock = NULL;
545 SilcClientConnAuthInternalContext *proto_ctx;
547 SILC_LOG_DEBUG(("Start"));
549 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
550 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
551 /* Error occured during protocol */
552 SILC_LOG_DEBUG(("Error during KE protocol"));
553 silc_protocol_free(protocol);
554 silc_ske_free_key_material(ctx->keymat);
556 silc_ske_free(ctx->ske);
558 silc_free(ctx->dest_id);
559 ctx->sock->protocol = NULL;
560 silc_socket_free(ctx->sock);
562 /* Notify application of failure */
563 silc_schedule_task_add(client->schedule, ctx->sock->sock,
564 silc_client_connect_failure, ctx,
565 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
569 /* We now have the key material as the result of the key exchange
570 protocol. Take the key material into use. Free the raw key material
571 as soon as we've set them into use. */
572 silc_client_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
573 ctx->ske->prop->cipher,
574 ctx->ske->prop->pkcs,
575 ctx->ske->prop->hash,
576 ctx->ske->prop->hmac,
577 ctx->ske->prop->group,
579 silc_ske_free_key_material(ctx->keymat);
581 /* Allocate internal context for the authentication protocol. This
582 is sent as context for the protocol. */
583 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
584 proto_ctx->client = (void *)client;
585 proto_ctx->sock = sock = ctx->sock;
586 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
587 proto_ctx->dest_id_type = ctx->dest_id_type;
588 proto_ctx->dest_id = ctx->dest_id;
590 /* Free old protocol as it is finished now */
591 silc_protocol_free(protocol);
593 silc_packet_context_free(ctx->packet);
595 sock->protocol = NULL;
597 /* Resolve the authentication method to be used in this connection. The
598 completion callback is called after the application has resolved
599 the authentication method. */
600 client->internal->ops->get_auth_method(client, sock->user_data,
603 silc_client_resolve_auth_method,
607 /* Authentication method resolving callback. Application calls this function
608 after we've called the client->internal->ops->get_auth_method
609 client operation to resolve the authentication method. We will continue
610 the executiong of the protocol in this function. */
612 void silc_client_resolve_auth_method(bool success,
613 SilcProtocolAuthMeth auth_meth,
614 const unsigned char *auth_data,
615 SilcUInt32 auth_data_len, void *context)
617 SilcClientConnAuthInternalContext *proto_ctx =
618 (SilcClientConnAuthInternalContext *)context;
619 SilcClient client = (SilcClient)proto_ctx->client;
622 auth_meth = SILC_AUTH_NONE;
624 proto_ctx->auth_meth = auth_meth;
626 if (auth_data && auth_data_len) {
627 proto_ctx->auth_data = silc_memdup(auth_data, auth_data_len);
628 proto_ctx->auth_data_len = auth_data_len;
631 /* Allocate the authenteication protocol and execute it. */
632 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
633 &proto_ctx->sock->protocol, (void *)proto_ctx,
634 silc_client_connect_to_server_final);
636 /* Execute the protocol */
637 silc_protocol_execute(proto_ctx->sock->protocol, client->schedule, 0, 0);
640 /* Finalizes the connection to the remote SILC server. This is called
641 after authentication protocol has been completed. This send our
642 user information to the server to receive our client ID from
645 SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
647 SilcProtocol protocol = (SilcProtocol)context;
648 SilcClientConnAuthInternalContext *ctx =
649 (SilcClientConnAuthInternalContext *)protocol->context;
650 SilcClient client = (SilcClient)ctx->client;
651 SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
654 SILC_LOG_DEBUG(("Start"));
656 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
657 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
658 /* Error occured during protocol */
659 SILC_LOG_DEBUG(("Error during authentication protocol"));
663 if (conn->params.detach_data) {
664 /* Send RESUME_CLIENT packet to the server, which is used to resume
665 old detached session back. */
667 SilcClientID *old_client_id;
668 unsigned char *old_id;
669 SilcUInt16 old_id_len;
671 if (!silc_client_process_detach_data(client, conn, &old_id, &old_id_len))
674 old_client_id = silc_id_str2id(old_id, old_id_len, SILC_ID_CLIENT);
675 if (!old_client_id) {
680 /* Generate authentication data that server will verify */
681 auth = silc_auth_public_key_auth_generate(client->public_key,
683 client->rng, conn->hash,
684 old_client_id, SILC_ID_CLIENT);
686 silc_free(old_client_id);
691 packet = silc_buffer_alloc_size(2 + old_id_len + auth->len);
692 silc_buffer_format(packet,
693 SILC_STR_UI_SHORT(old_id_len),
694 SILC_STR_UI_XNSTRING(old_id, old_id_len),
695 SILC_STR_UI_XNSTRING(auth->data, auth->len),
698 /* Send the packet */
699 silc_client_packet_send(client, ctx->sock, SILC_PACKET_RESUME_CLIENT,
701 packet->data, packet->len, TRUE);
702 silc_buffer_free(packet);
703 silc_buffer_free(auth);
704 silc_free(old_client_id);
707 /* Send NEW_CLIENT packet to the server. We will become registered
708 to the SILC network after sending this packet and we will receive
709 client ID from the server. */
710 packet = silc_buffer_alloc(2 + 2 + strlen(client->username) +
711 strlen(client->realname));
712 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
713 silc_buffer_format(packet,
714 SILC_STR_UI_SHORT(strlen(client->username)),
715 SILC_STR_UI_XNSTRING(client->username,
716 strlen(client->username)),
717 SILC_STR_UI_SHORT(strlen(client->realname)),
718 SILC_STR_UI_XNSTRING(client->realname,
719 strlen(client->realname)),
722 /* Send the packet */
723 silc_client_packet_send(client, ctx->sock, SILC_PACKET_NEW_CLIENT,
725 packet->data, packet->len, TRUE);
726 silc_buffer_free(packet);
729 /* Save remote ID. */
730 conn->remote_id = ctx->dest_id;
731 conn->remote_id_data = silc_id_id2str(ctx->dest_id, SILC_ID_SERVER);
732 conn->remote_id_data_len = silc_id_get_len(ctx->dest_id, SILC_ID_SERVER);
734 /* Register re-key timeout */
735 conn->rekey->timeout = client->internal->params->rekey_secs;
736 conn->rekey->context = (void *)client;
737 silc_schedule_task_add(client->schedule, conn->sock->sock,
738 silc_client_rekey_callback,
739 (void *)conn->sock, conn->rekey->timeout, 0,
740 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
742 silc_protocol_free(protocol);
743 silc_free(ctx->auth_data);
745 silc_ske_free(ctx->ske);
746 silc_socket_free(ctx->sock);
748 conn->sock->protocol = NULL;
752 silc_protocol_free(protocol);
753 silc_free(ctx->auth_data);
754 silc_free(ctx->dest_id);
756 silc_ske_free(ctx->ske);
757 conn->sock->protocol = NULL;
758 silc_socket_free(ctx->sock);
760 /* Notify application of failure */
761 silc_schedule_task_add(client->schedule, ctx->sock->sock,
762 silc_client_connect_failure, ctx,
763 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
766 /* Internal routine that sends packet or marks packet to be sent. This
767 is used directly only in special cases. Normal cases should use
768 silc_server_packet_send. Returns < 0 on error. */
770 int silc_client_packet_send_real(SilcClient client,
771 SilcSocketConnection sock,
776 /* If rekey protocol is active we must assure that all packets are
777 sent through packet queue. */
778 if (SILC_CLIENT_IS_REKEY(sock))
781 /* If outbound data is already pending do not force send */
782 if (SILC_IS_OUTBUF_PENDING(sock))
785 /* Send the packet */
786 ret = silc_packet_send(sock, force_send);
790 /* Mark that there is some outgoing data available for this connection.
791 This call sets the connection both for input and output (the input
792 is set always and this call keeps the input setting, actually).
793 Actual data sending is performed by silc_client_packet_process. */
794 SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(client->schedule, sock->sock);
796 /* Mark to socket that data is pending in outgoing buffer. This flag
797 is needed if new data is added to the buffer before the earlier
798 put data is sent to the network. */
799 SILC_SET_OUTBUF_PENDING(sock);
804 /* Packet processing callback. This is used to send and receive packets
805 from network. This is generic task. */
807 SILC_TASK_CALLBACK_GLOBAL(silc_client_packet_process)
809 SilcClient client = (SilcClient)context;
810 SilcSocketConnection sock = NULL;
811 SilcClientConnection conn;
814 SILC_LOG_DEBUG(("Processing packet"));
816 SILC_CLIENT_GET_SOCK(client, fd, sock);
820 conn = (SilcClientConnection)sock->user_data;
823 if (type == SILC_TASK_WRITE) {
824 /* Do not send data to disconnected connection */
825 if (SILC_IS_DISCONNECTED(sock))
828 ret = silc_packet_send(sock, TRUE);
830 /* If returned -2 could not write to connection now, will do
839 /* The packet has been sent and now it is time to set the connection
840 back to only for input. When there is again some outgoing data
841 available for this connection it will be set for output as well.
842 This call clears the output setting and sets it only for input. */
843 SILC_CLIENT_SET_CONNECTION_FOR_INPUT(client->schedule, fd);
844 SILC_UNSET_OUTBUF_PENDING(sock);
846 silc_buffer_clear(sock->outbuf);
850 /* Packet receiving */
851 if (type == SILC_TASK_READ) {
852 /* Read data from network */
853 ret = silc_packet_receive(sock);
859 SILC_LOG_DEBUG(("Read EOF"));
861 /* If connection is disconnecting already we will finally
862 close the connection */
863 if (SILC_IS_DISCONNECTING(sock)) {
864 if (sock == conn->sock && sock->type != SILC_SOCKET_TYPE_CLIENT)
865 client->internal->ops->disconnect(client, conn, 0, NULL);
866 silc_client_close_connection_real(client, sock, conn);
870 SILC_LOG_DEBUG(("EOF from connection %d", sock->sock));
871 if (sock == conn->sock && sock->type != SILC_SOCKET_TYPE_CLIENT)
872 client->internal->ops->disconnect(client, conn, 0, NULL);
873 silc_client_close_connection_real(client, sock, conn);
877 /* Process the packet. This will call the parser that will then
878 decrypt and parse the packet. */
879 if (sock->type != SILC_SOCKET_TYPE_UNKNOWN)
880 silc_packet_receive_process(sock, FALSE, conn->receive_key,
881 conn->hmac_receive, conn->psn_receive,
882 silc_client_packet_parse, client);
884 silc_packet_receive_process(sock, FALSE, NULL, NULL, 0,
885 silc_client_packet_parse, client);
889 /* Parser callback called by silc_packet_receive_process. Thie merely
890 registers timeout that will handle the actual parsing when appropriate. */
892 static bool silc_client_packet_parse(SilcPacketParserContext *parser_context,
895 SilcClient client = (SilcClient)context;
896 SilcSocketConnection sock = parser_context->sock;
897 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
898 SilcPacketContext *packet = parser_context->packet;
901 if (conn && conn->hmac_receive && conn->sock == sock)
902 conn->psn_receive = parser_context->packet->sequence + 1;
904 /* Parse the packet immediately */
905 if (parser_context->normal)
906 ret = silc_packet_parse(packet, conn->receive_key);
908 ret = silc_packet_parse_special(packet, conn->receive_key);
910 if (ret == SILC_PACKET_NONE) {
911 silc_packet_context_free(packet);
912 silc_free(parser_context);
916 /* If protocol for this connection is key exchange or rekey then we'll
917 process all packets synchronously, since there might be packets in
918 queue that we are not able to decrypt without first processing the
919 packets before them. */
920 if ((ret == SILC_PACKET_REKEY || ret == SILC_PACKET_REKEY_DONE) ||
921 (sock->protocol && sock->protocol->protocol &&
922 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
923 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY))) {
925 /* Parse the incoming packet type */
926 silc_client_packet_parse_type(client, sock, packet);
927 silc_packet_context_free(packet);
928 silc_free(parser_context);
930 /* Reprocess the buffer since we'll return FALSE. This is because
931 the `conn->receive_key' might have become valid by processing
932 the previous packet */
933 if (sock->type != SILC_SOCKET_TYPE_UNKNOWN)
934 silc_packet_receive_process(sock, FALSE, conn->receive_key,
935 conn->hmac_receive, conn->psn_receive,
936 silc_client_packet_parse, client);
938 silc_packet_receive_process(sock, FALSE, NULL, NULL, 0,
939 silc_client_packet_parse, client);
944 /* Parse the incoming packet type */
945 silc_client_packet_parse_type(client, sock, packet);
946 silc_packet_context_free(packet);
947 silc_free(parser_context);
951 /* Parses the packet type and calls what ever routines the packet type
952 requires. This is done for all incoming packets. */
954 void silc_client_packet_parse_type(SilcClient client,
955 SilcSocketConnection sock,
956 SilcPacketContext *packet)
958 SilcBuffer buffer = packet->buffer;
959 SilcPacketType type = packet->type;
961 SILC_LOG_DEBUG(("Parsing packet type %d", type));
963 /* Parse the packet type */
965 case SILC_PACKET_DISCONNECT:
966 silc_client_disconnected_by_server(client, sock, buffer);
968 case SILC_PACKET_SUCCESS:
970 * Success received for something. For now we can have only
971 * one protocol for connection executing at once hence this
972 * success message is for whatever protocol is executing currently.
975 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
977 case SILC_PACKET_FAILURE:
979 * Failure received for some protocol. Set the protocol state to
980 * error and call the protocol callback. This fill cause error on
981 * protocol and it will call the final callback.
983 silc_client_process_failure(client, sock, packet);
985 case SILC_PACKET_REJECT:
988 case SILC_PACKET_NOTIFY:
990 * Received notify message
992 silc_client_notify_by_server(client, sock, packet);
995 case SILC_PACKET_ERROR:
997 * Received error message
999 silc_client_error_by_server(client, sock, buffer);
1002 case SILC_PACKET_CHANNEL_MESSAGE:
1004 * Received message to (from, actually) a channel
1006 silc_client_channel_message(client, sock, packet);
1008 case SILC_PACKET_CHANNEL_KEY:
1010 * Received key for a channel. By receiving this key the client will be
1011 * able to talk to the channel it has just joined. This can also be
1012 * a new key for existing channel as keys expire peridiocally.
1014 silc_client_receive_channel_key(client, sock, buffer);
1017 case SILC_PACKET_PRIVATE_MESSAGE:
1019 * Received private message
1021 silc_client_private_message(client, sock, packet);
1023 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1025 * Received private message key
1029 case SILC_PACKET_COMMAND_REPLY:
1031 * Recived reply for a command
1033 silc_client_command_reply_process(client, sock, packet);
1036 case SILC_PACKET_KEY_EXCHANGE:
1037 if (sock->protocol && sock->protocol->protocol &&
1038 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE) {
1039 SilcClientKEInternalContext *proto_ctx =
1040 (SilcClientKEInternalContext *)sock->protocol->context;
1042 proto_ctx->packet = silc_packet_context_dup(packet);
1043 proto_ctx->dest_id_type = packet->src_id_type;
1044 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1045 packet->src_id_type);
1046 if (!proto_ctx->dest_id)
1049 /* Let the protocol handle the packet */
1050 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1052 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1053 "protocol active, packet dropped."));
1057 case SILC_PACKET_KEY_EXCHANGE_1:
1058 if (sock->protocol && sock->protocol->protocol &&
1059 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
1060 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)) {
1062 if (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
1063 SilcClientRekeyInternalContext *proto_ctx =
1064 (SilcClientRekeyInternalContext *)sock->protocol->context;
1066 if (proto_ctx->packet)
1067 silc_packet_context_free(proto_ctx->packet);
1069 proto_ctx->packet = silc_packet_context_dup(packet);
1071 /* Let the protocol handle the packet */
1072 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1074 SilcClientKEInternalContext *proto_ctx =
1075 (SilcClientKEInternalContext *)sock->protocol->context;
1077 if (proto_ctx->packet)
1078 silc_packet_context_free(proto_ctx->packet);
1080 proto_ctx->packet = silc_packet_context_dup(packet);
1081 proto_ctx->dest_id_type = packet->src_id_type;
1082 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1083 packet->src_id_type);
1084 if (!proto_ctx->dest_id)
1087 /* Let the protocol handle the packet */
1088 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1091 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1092 "protocol active, packet dropped."));
1095 case SILC_PACKET_KEY_EXCHANGE_2:
1096 if (sock->protocol && sock->protocol->protocol &&
1097 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
1098 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)) {
1100 if (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
1101 SilcClientRekeyInternalContext *proto_ctx =
1102 (SilcClientRekeyInternalContext *)sock->protocol->context;
1104 if (proto_ctx->packet)
1105 silc_packet_context_free(proto_ctx->packet);
1107 proto_ctx->packet = silc_packet_context_dup(packet);
1109 /* Let the protocol handle the packet */
1110 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1112 SilcClientKEInternalContext *proto_ctx =
1113 (SilcClientKEInternalContext *)sock->protocol->context;
1115 if (proto_ctx->packet)
1116 silc_packet_context_free(proto_ctx->packet);
1117 if (proto_ctx->dest_id)
1118 silc_free(proto_ctx->dest_id);
1119 proto_ctx->packet = silc_packet_context_dup(packet);
1120 proto_ctx->dest_id_type = packet->src_id_type;
1121 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1122 packet->src_id_type);
1123 if (!proto_ctx->dest_id)
1126 /* Let the protocol handle the packet */
1127 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1130 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1131 "protocol active, packet dropped."));
1135 case SILC_PACKET_NEW_ID:
1138 * Received new ID from server. This packet is received at
1139 * the connection to the server. New ID is also received when
1140 * user changes nickname but in that case the new ID is received
1141 * as command reply and not as this packet type.
1145 idp = silc_id_payload_parse(buffer->data, buffer->len);
1148 if (silc_id_payload_get_type(idp) != SILC_ID_CLIENT)
1151 silc_client_receive_new_id(client, sock, idp);
1152 silc_id_payload_free(idp);
1156 case SILC_PACKET_HEARTBEAT:
1158 * Received heartbeat packet
1160 SILC_LOG_DEBUG(("Heartbeat packet"));
1163 case SILC_PACKET_KEY_AGREEMENT:
1165 * Received key agreement packet
1167 SILC_LOG_DEBUG(("Key agreement packet"));
1168 silc_client_key_agreement(client, sock, packet);
1171 case SILC_PACKET_REKEY:
1172 SILC_LOG_DEBUG(("Re-key packet"));
1173 /* We ignore this for now */
1176 case SILC_PACKET_REKEY_DONE:
1177 SILC_LOG_DEBUG(("Re-key done packet"));
1179 if (sock->protocol && sock->protocol->protocol &&
1180 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
1182 SilcClientRekeyInternalContext *proto_ctx =
1183 (SilcClientRekeyInternalContext *)sock->protocol->context;
1185 if (proto_ctx->packet)
1186 silc_packet_context_free(proto_ctx->packet);
1188 proto_ctx->packet = silc_packet_context_dup(packet);
1190 /* Let the protocol handle the packet */
1191 if (proto_ctx->responder == FALSE)
1192 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1194 /* Let the protocol handle the packet */
1195 silc_protocol_execute(sock->protocol, client->schedule,
1198 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
1199 "protocol active, packet dropped."));
1203 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1205 * Reveived reply to our connection authentication method request
1206 * packet. This is used to resolve the authentication method for the
1207 * current session from the server if the client does not know it.
1209 silc_client_connection_auth_request(client, sock, packet);
1212 case SILC_PACKET_FTP:
1213 /* Received file transfer packet. */
1214 silc_client_ftp(client, sock, packet);
1218 SILC_LOG_DEBUG(("Incorrect packet type %d, packet dropped", type));
1223 /* Sends packet. This doesn't actually send the packet instead it assembles
1224 it and marks it to be sent. However, if force_send is TRUE the packet
1225 is sent immediately. if dst_id, cipher and hmac are NULL those parameters
1226 will be derived from sock argument. Otherwise the valid arguments sent
1229 void silc_client_packet_send(SilcClient client,
1230 SilcSocketConnection sock,
1231 SilcPacketType type,
1233 SilcIdType dst_id_type,
1236 unsigned char *data,
1237 SilcUInt32 data_len,
1240 SilcPacketContext packetdata;
1241 const SilcBufferStruct packet;
1243 SilcUInt32 sequence = 0;
1248 SILC_LOG_DEBUG(("Sending packet, type %d", type));
1250 /* Get data used in the packet sending, keys and stuff */
1251 if ((!cipher || !hmac || !dst_id) && sock->user_data) {
1252 if (!cipher && ((SilcClientConnection)sock->user_data)->send_key)
1253 cipher = ((SilcClientConnection)sock->user_data)->send_key;
1255 if (!hmac && ((SilcClientConnection)sock->user_data)->hmac_send)
1256 hmac = ((SilcClientConnection)sock->user_data)->hmac_send;
1258 if (!dst_id && ((SilcClientConnection)sock->user_data)->remote_id) {
1259 dst_id = ((SilcClientConnection)sock->user_data)->remote_id;
1260 dst_id_type = SILC_ID_SERVER;
1264 sequence = ((SilcClientConnection)sock->user_data)->psn_send++;
1267 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
1269 /* Set the packet context pointers */
1270 packetdata.flags = 0;
1271 packetdata.type = type;
1272 if (sock->user_data &&
1273 ((SilcClientConnection)sock->user_data)->local_id_data) {
1274 packetdata.src_id = ((SilcClientConnection)sock->user_data)->local_id_data;
1275 packetdata.src_id_len =
1276 silc_id_get_len(((SilcClientConnection)sock->user_data)->local_id,
1279 packetdata.src_id = silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char));
1280 packetdata.src_id_len = SILC_ID_CLIENT_LEN;
1282 packetdata.src_id_type = SILC_ID_CLIENT;
1284 packetdata.dst_id = silc_id_id2str(dst_id, dst_id_type);
1285 packetdata.dst_id_len = silc_id_get_len(dst_id, dst_id_type);
1286 packetdata.dst_id_type = dst_id_type;
1288 packetdata.dst_id = NULL;
1289 packetdata.dst_id_len = 0;
1290 packetdata.dst_id_type = SILC_ID_NONE;
1292 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
1293 packetdata.src_id_len +
1294 packetdata.dst_id_len));
1295 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1296 packetdata.src_id_len + packetdata.dst_id_len;
1297 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
1299 /* Create the outgoing packet */
1300 if (!silc_packet_assemble(&packetdata, client->rng, cipher, hmac, sock,
1301 data, data_len, (const SilcBuffer)&packet)) {
1302 SILC_LOG_ERROR(("Error assembling packet"));
1306 /* Encrypt the packet */
1308 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet,
1311 SILC_LOG_HEXDUMP(("Packet (%d), len %d", sequence, packet.len),
1312 packet.data, packet.len);
1314 /* Now actually send the packet */
1315 silc_client_packet_send_real(client, sock, force_send);
1318 void silc_client_packet_queue_purge(SilcClient client,
1319 SilcSocketConnection sock)
1321 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1322 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1323 silc_packet_send(sock, TRUE);
1324 SILC_CLIENT_SET_CONNECTION_FOR_INPUT(client->schedule, sock->sock);
1325 SILC_UNSET_OUTBUF_PENDING(sock);
1326 silc_buffer_clear(sock->outbuf);
1330 /* Closes connection to remote end. Free's all allocated data except
1331 for some information such as nickname etc. that are valid at all time.
1332 If the `sock' is NULL then the conn->sock will be used. If `sock' is
1333 provided it will be checked whether the sock and `conn->sock' are the
1334 same (they can be different, ie. a socket can use `conn' as its
1335 connection but `conn->sock' might be actually a different connection
1336 than the `sock'). */
1338 void silc_client_close_connection_real(SilcClient client,
1339 SilcSocketConnection sock,
1340 SilcClientConnection conn)
1344 SILC_LOG_DEBUG(("Start"));
1349 if (!sock || (sock && conn->sock == sock))
1354 /* We won't listen for this connection anymore */
1355 silc_schedule_unset_listen_fd(client->schedule, sock->sock);
1357 /* Unregister all tasks */
1358 silc_schedule_task_del_by_fd(client->schedule, sock->sock);
1360 /* Close the actual connection */
1361 silc_net_close_connection(sock->sock);
1363 /* Cancel any active protocol */
1364 if (sock->protocol) {
1365 if (sock->protocol->protocol->type ==
1366 SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
1367 sock->protocol->protocol->type ==
1368 SILC_PROTOCOL_CLIENT_CONNECTION_AUTH) {
1369 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
1370 silc_protocol_execute_final(sock->protocol, client->schedule);
1371 /* The application will recall this function with these protocols
1372 (the ops->connect client operation). */
1375 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
1376 silc_protocol_execute_final(sock->protocol, client->schedule);
1377 sock->protocol = NULL;
1381 /* Free everything */
1382 if (del && sock->user_data) {
1383 /* Free all cache entries */
1384 SilcIDCacheList list;
1385 SilcIDCacheEntry entry;
1386 SilcClientCommandPending *r;
1389 if (silc_idcache_get_all(conn->client_cache, &list)) {
1390 ret = silc_idcache_list_first(list, &entry);
1392 silc_client_del_client(client, conn, entry->context);
1393 ret = silc_idcache_list_next(list, &entry);
1395 silc_idcache_list_free(list);
1398 if (silc_idcache_get_all(conn->channel_cache, &list)) {
1399 ret = silc_idcache_list_first(list, &entry);
1401 silc_client_del_channel(client, conn, entry->context);
1402 ret = silc_idcache_list_next(list, &entry);
1404 silc_idcache_list_free(list);
1407 if (silc_idcache_get_all(conn->server_cache, &list)) {
1408 ret = silc_idcache_list_first(list, &entry);
1410 silc_client_del_server(client, conn, entry->context);
1411 ret = silc_idcache_list_next(list, &entry);
1413 silc_idcache_list_free(list);
1416 /* Clear ID caches */
1417 if (conn->client_cache)
1418 silc_idcache_free(conn->client_cache);
1419 if (conn->channel_cache)
1420 silc_idcache_free(conn->channel_cache);
1421 if (conn->server_cache)
1422 silc_idcache_free(conn->server_cache);
1424 /* Free data (my ID is freed in above silc_client_del_client).
1425 conn->nickname is freed when freeing the local_entry->nickname. */
1426 if (conn->remote_host)
1427 silc_free(conn->remote_host);
1428 if (conn->local_id_data)
1429 silc_free(conn->local_id_data);
1431 silc_cipher_free(conn->send_key);
1432 if (conn->receive_key)
1433 silc_cipher_free(conn->receive_key);
1434 if (conn->hmac_send)
1435 silc_hmac_free(conn->hmac_send);
1436 if (conn->hmac_receive)
1437 silc_hmac_free(conn->hmac_receive);
1439 silc_free(conn->rekey);
1441 if (conn->active_session) {
1442 sock->user_data = NULL;
1443 silc_client_ftp_session_free(conn->active_session);
1444 conn->active_session = NULL;
1447 silc_client_ftp_free_sessions(client, conn);
1449 silc_dlist_start(conn->pending_commands);
1450 while ((r = silc_dlist_get(conn->pending_commands)) != SILC_LIST_END)
1451 silc_dlist_del(conn->pending_commands, r);
1452 if (conn->pending_commands)
1453 silc_dlist_uninit(conn->pending_commands);
1455 memset(conn, 0, sizeof(*conn));
1456 silc_client_del_connection(client, conn);
1459 silc_socket_free(sock);
1462 /* Closes the connection to the remote end */
1464 void silc_client_close_connection(SilcClient client,
1465 SilcClientConnection conn)
1467 silc_client_close_connection_real(client, NULL, conn);
1470 /* Called when we receive disconnection packet from server. This
1471 closes our end properly and displays the reason of the disconnection
1474 SILC_TASK_CALLBACK(silc_client_disconnected_by_server_later)
1476 SilcClient client = (SilcClient)context;
1477 SilcSocketConnection sock;
1479 SILC_CLIENT_GET_SOCK(client, fd, sock);
1483 silc_client_close_connection_real(client, sock, sock->user_data);
1486 /* Called when we receive disconnection packet from server. This
1487 closes our end properly and displays the reason of the disconnection
1490 void silc_client_disconnected_by_server(SilcClient client,
1491 SilcSocketConnection sock,
1494 SilcClientConnection conn;
1496 char *message = NULL;
1498 SILC_LOG_DEBUG(("Server disconnected us, sock %d", sock->sock));
1500 if (packet->len < 1)
1503 status = (SilcStatus)packet->data[0];
1505 if (packet->len > 1 &&
1506 silc_utf8_valid(packet->data + 1, packet->len - 1))
1507 message = silc_memdup(packet->data + 1, packet->len - 1);
1509 conn = (SilcClientConnection)sock->user_data;
1510 if (sock == conn->sock && sock->type != SILC_SOCKET_TYPE_CLIENT)
1511 client->internal->ops->disconnect(client, conn, status, message);
1515 SILC_SET_DISCONNECTED(sock);
1517 /* Close connection through scheduler. */
1518 silc_schedule_task_add(client->schedule, sock->sock,
1519 silc_client_disconnected_by_server_later,
1520 client, 0, 1, SILC_TASK_TIMEOUT,
1521 SILC_TASK_PRI_NORMAL);
1524 /* Received error message from server. Display it on the screen.
1525 We don't take any action what so ever of the error message. */
1527 void silc_client_error_by_server(SilcClient client,
1528 SilcSocketConnection sock,
1533 msg = silc_memdup(message->data, message->len);
1534 client->internal->ops->say(client, sock->user_data,
1535 SILC_CLIENT_MESSAGE_AUDIT, msg);
1539 /* Auto-nicking callback to send NICK command to server. */
1541 SILC_TASK_CALLBACK(silc_client_send_auto_nick)
1543 SilcClientConnection conn = (SilcClientConnection)context;
1544 SilcClient client = conn->client;
1546 silc_client_command_send(client, conn, SILC_COMMAND_NICK,
1547 ++conn->cmd_ident, 1, 1,
1548 client->nickname, strlen(client->nickname));
1551 /* Client session resuming callback. If the session was resumed
1552 this callback is called after the resuming is completed. This
1553 will call the `connect' client operation to the application
1554 since it has not been called yet. */
1556 static void silc_client_resume_session_cb(SilcClient client,
1557 SilcClientConnection conn,
1563 /* Notify application that connection is created to server */
1564 client->internal->ops->connect(client, conn, success ?
1565 SILC_CLIENT_CONN_SUCCESS_RESUME :
1566 SILC_CLIENT_CONN_ERROR);
1569 /* Issue INFO command to fetch the real server name and server
1570 information and other stuff. */
1571 silc_client_command_register(client, SILC_COMMAND_INFO, NULL, NULL,
1572 silc_client_command_reply_info_i, 0,
1574 sidp = silc_id_payload_encode(conn->remote_id, SILC_ID_SERVER);
1575 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
1576 conn->cmd_ident, 1, 2, sidp->data, sidp->len);
1577 silc_buffer_free(sidp);
1581 /* Processes the received new Client ID from server. Old Client ID is
1582 deleted from cache and new one is added. */
1584 void silc_client_receive_new_id(SilcClient client,
1585 SilcSocketConnection sock,
1588 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1589 int connecting = FALSE;
1590 SilcClientID *client_id = silc_id_payload_get_id(idp);
1592 if (!conn->local_entry)
1595 /* Delete old ID from ID cache */
1596 if (conn->local_id) {
1597 /* Check whether they are different */
1598 if (SILC_ID_CLIENT_COMPARE(conn->local_id, client_id)) {
1599 silc_free(client_id);
1603 silc_idcache_del_by_context(conn->client_cache, conn->local_entry);
1604 silc_free(conn->local_id);
1607 /* Save the new ID */
1609 if (conn->local_id_data)
1610 silc_free(conn->local_id_data);
1612 conn->local_id = client_id;
1613 conn->local_id_data = silc_id_payload_get_data(idp);
1614 conn->local_id_data_len = silc_id_payload_get_len(idp);;
1616 if (!conn->local_entry)
1617 conn->local_entry = silc_calloc(1, sizeof(*conn->local_entry));
1619 conn->local_entry->nickname = conn->nickname;
1620 if (!conn->local_entry->username)
1621 conn->local_entry->username = strdup(client->username);
1622 if (!conn->local_entry->server)
1623 conn->local_entry->server = strdup(conn->remote_host);
1624 conn->local_entry->id = conn->local_id;
1625 conn->local_entry->valid = TRUE;
1626 if (!conn->local_entry->channels)
1627 conn->local_entry->channels = silc_hash_table_alloc(1, silc_hash_ptr,
1632 /* Put it to the ID cache */
1633 silc_idcache_add(conn->client_cache, strdup(conn->nickname), conn->local_id,
1634 (void *)conn->local_entry, 0, NULL);
1639 /* Issue IDENTIFY command for itself to get resolved hostname
1640 correctly from server. */
1641 silc_client_command_register(client, SILC_COMMAND_IDENTIFY, NULL, NULL,
1642 silc_client_command_reply_identify_i, 0,
1644 sidp = silc_id_payload_encode(conn->local_entry->id, SILC_ID_CLIENT);
1645 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
1646 conn->cmd_ident, 1, 5, sidp->data, sidp->len);
1647 silc_buffer_free(sidp);
1649 if (!conn->params.detach_data) {
1650 /* Send NICK command if the nickname was set by the application (and is
1651 not same as the username). Send this with little timeout. */
1652 if (client->nickname && strcmp(client->nickname, client->username))
1653 silc_schedule_task_add(client->schedule, 0,
1654 silc_client_send_auto_nick, conn,
1655 1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1657 /* Notify application of successful connection. We do it here now that
1658 we've received the Client ID and are allowed to send traffic. */
1659 client->internal->ops->connect(client, conn, SILC_CLIENT_CONN_SUCCESS);
1661 /* Issue INFO command to fetch the real server name and server
1662 information and other stuff. */
1663 silc_client_command_register(client, SILC_COMMAND_INFO, NULL, NULL,
1664 silc_client_command_reply_info_i, 0,
1666 sidp = silc_id_payload_encode(conn->remote_id, SILC_ID_SERVER);
1667 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
1668 conn->cmd_ident, 1, 2, sidp->data, sidp->len);
1669 silc_buffer_free(sidp);
1671 /* We are resuming session. Start resolving informations from the
1672 server we need to set the client libary in the state before
1673 detaching the session. The connect client operation is called
1674 after this is successfully completed */
1675 silc_client_resume_session(client, conn, silc_client_resume_session_cb,
1681 /* Removes a client entry from all channels it has joined. */
1683 void silc_client_remove_from_channels(SilcClient client,
1684 SilcClientConnection conn,
1685 SilcClientEntry client_entry)
1687 SilcHashTableList htl;
1688 SilcChannelUser chu;
1690 silc_hash_table_list(client_entry->channels, &htl);
1691 while (silc_hash_table_get(&htl, NULL, (void **)&chu)) {
1692 silc_hash_table_del(chu->client->channels, chu->channel);
1693 silc_hash_table_del(chu->channel->user_list, chu->client);
1697 silc_hash_table_list_reset(&htl);
1700 /* Replaces `old' client entries from all channels to `new' client entry.
1701 This can be called for example when nickname changes and old ID entry
1702 is replaced from ID cache with the new one. If the old ID entry is only
1703 updated, then this fucntion needs not to be called. */
1705 void silc_client_replace_from_channels(SilcClient client,
1706 SilcClientConnection conn,
1707 SilcClientEntry old,
1708 SilcClientEntry new)
1710 SilcHashTableList htl;
1711 SilcChannelUser chu;
1713 silc_hash_table_list(old->channels, &htl);
1714 while (silc_hash_table_get(&htl, NULL, (void **)&chu)) {
1715 /* Replace client entry */
1716 silc_hash_table_del(chu->client->channels, chu->channel);
1717 silc_hash_table_del(chu->channel->user_list, chu->client);
1720 silc_hash_table_add(chu->channel->user_list, chu->client, chu);
1721 silc_hash_table_add(chu->client->channels, chu->channel, chu);
1723 silc_hash_table_list_reset(&htl);
1726 /* Registers failure timeout to process the received failure packet
1729 void silc_client_process_failure(SilcClient client,
1730 SilcSocketConnection sock,
1731 SilcPacketContext *packet)
1733 SilcUInt32 failure = 0;
1735 if (sock->protocol) {
1736 if (packet->buffer->len >= 4)
1737 SILC_GET32_MSB(failure, packet->buffer->data);
1739 /* Notify application */
1740 client->internal->ops->failure(client, sock->user_data, sock->protocol,
1745 /* A timeout callback for the re-key. We will be the initiator of the
1748 SILC_TASK_CALLBACK(silc_client_rekey_callback)
1750 SilcSocketConnection sock = (SilcSocketConnection)context;
1751 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1752 SilcClient client = (SilcClient)conn->rekey->context;
1753 SilcProtocol protocol;
1754 SilcClientRekeyInternalContext *proto_ctx;
1756 SILC_LOG_DEBUG(("Start"));
1758 /* Allocate internal protocol context. This is sent as context
1760 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1761 proto_ctx->client = (void *)client;
1762 proto_ctx->sock = silc_socket_dup(sock);
1763 proto_ctx->responder = FALSE;
1764 proto_ctx->pfs = conn->rekey->pfs;
1766 /* Perform rekey protocol. Will call the final callback after the
1767 protocol is over. */
1768 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_REKEY,
1769 &protocol, proto_ctx, silc_client_rekey_final);
1770 sock->protocol = protocol;
1772 /* Run the protocol */
1773 silc_protocol_execute(protocol, client->schedule, 0, 0);
1775 /* Re-register re-key timeout */
1776 silc_schedule_task_add(client->schedule, sock->sock,
1777 silc_client_rekey_callback,
1778 context, conn->rekey->timeout, 0,
1779 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1782 /* The final callback for the REKEY protocol. This will actually take the
1783 new key material into use. */
1785 SILC_TASK_CALLBACK(silc_client_rekey_final)
1787 SilcProtocol protocol = (SilcProtocol)context;
1788 SilcClientRekeyInternalContext *ctx =
1789 (SilcClientRekeyInternalContext *)protocol->context;
1790 SilcClient client = (SilcClient)ctx->client;
1791 SilcSocketConnection sock = ctx->sock;
1793 SILC_LOG_DEBUG(("Start"));
1795 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1796 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1797 /* Error occured during protocol */
1798 silc_protocol_cancel(protocol, client->schedule);
1799 silc_protocol_free(protocol);
1800 sock->protocol = NULL;
1802 silc_packet_context_free(ctx->packet);
1804 silc_ske_free(ctx->ske);
1805 silc_socket_free(ctx->sock);
1810 /* Purge the outgoing data queue to assure that all rekey packets really
1811 go to the network before we quit the protocol. */
1812 silc_client_packet_queue_purge(client, sock);
1815 silc_protocol_free(protocol);
1816 sock->protocol = NULL;
1818 silc_packet_context_free(ctx->packet);
1820 silc_ske_free(ctx->ske);
1821 silc_socket_free(ctx->sock);
1825 /* Processes incoming connection authentication method request packet.
1826 It is a reply to our previously sent request. The packet can be used
1827 to resolve the authentication method for the current session if the
1828 client does not know it beforehand. */
1830 void silc_client_connection_auth_request(SilcClient client,
1831 SilcSocketConnection sock,
1832 SilcPacketContext *packet)
1834 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1835 SilcUInt16 conn_type, auth_meth;
1838 /* If we haven't send our request then ignore this one. */
1839 if (!conn->connauth)
1842 /* Parse the payload */
1843 ret = silc_buffer_unformat(packet->buffer,
1844 SILC_STR_UI_SHORT(&conn_type),
1845 SILC_STR_UI_SHORT(&auth_meth),
1848 auth_meth = SILC_AUTH_NONE;
1850 /* Call the request callback to notify application for received
1851 authentication method information. */
1852 if (conn->connauth->callback)
1853 (*conn->connauth->callback)(client, conn, auth_meth,
1854 conn->connauth->context);
1856 silc_schedule_task_del(client->schedule, conn->connauth->timeout);
1858 silc_free(conn->connauth);
1859 conn->connauth = NULL;
1862 /* Timeout task callback called if the server does not reply to our
1863 connection authentication method request in the specified time interval. */
1865 SILC_TASK_CALLBACK(silc_client_request_authentication_method_timeout)
1867 SilcClientConnection conn = (SilcClientConnection)context;
1868 SilcClient client = conn->client;
1870 if (!conn->connauth)
1873 /* Call the request callback to notify application */
1874 if (conn->connauth->callback)
1875 (*conn->connauth->callback)(client, conn, SILC_AUTH_NONE,
1876 conn->connauth->context);
1878 silc_free(conn->connauth);
1879 conn->connauth = NULL;
1882 /* This function can be used to request the current authentication method
1883 from the server. This may be called when connecting to the server
1884 and the client library requests the authentication data from the
1885 application. If the application does not know the current authentication
1886 method it can request it from the server using this function.
1887 The `callback' with `context' will be called after the server has
1888 replied back with the current authentication method. */
1891 silc_client_request_authentication_method(SilcClient client,
1892 SilcClientConnection conn,
1893 SilcConnectionAuthRequest callback,
1896 SilcClientConnAuthRequest connauth;
1899 connauth = silc_calloc(1, sizeof(*connauth));
1900 connauth->callback = callback;
1901 connauth->context = context;
1904 silc_free(conn->connauth);
1906 conn->connauth = connauth;
1908 /* Assemble the request packet and send it to the server */
1909 packet = silc_buffer_alloc(4);
1910 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1911 silc_buffer_format(packet,
1912 SILC_STR_UI_SHORT(SILC_SOCKET_TYPE_CLIENT),
1913 SILC_STR_UI_SHORT(SILC_AUTH_NONE),
1915 silc_client_packet_send(client, conn->sock,
1916 SILC_PACKET_CONNECTION_AUTH_REQUEST,
1917 NULL, 0, NULL, NULL,
1918 packet->data, packet->len, FALSE);
1919 silc_buffer_free(packet);
1921 /* Register a timeout in case server does not reply anything back. */
1923 silc_schedule_task_add(client->schedule, conn->sock->sock,
1924 silc_client_request_authentication_method_timeout,
1926 client->internal->params->connauth_request_secs, 0,
1927 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);