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);
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. */
189 SilcClientConnection silc_client_add_connection(SilcClient client,
194 SilcClientConnection conn;
197 conn = silc_calloc(1, sizeof(*conn));
199 /* Initialize ID caches */
200 conn->client_cache = silc_idcache_alloc(0, SILC_ID_CLIENT,
201 silc_client_entry_destructor);
202 conn->channel_cache = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
203 conn->server_cache = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
204 conn->client = client;
205 conn->remote_host = strdup(hostname);
206 conn->remote_port = port;
207 conn->context = context;
208 conn->pending_commands = silc_dlist_init();
209 conn->ftp_sessions = silc_dlist_init();
211 /* Add the connection to connections table */
212 for (i = 0; i < client->internal->conns_count; i++)
213 if (client->internal->conns && !client->internal->conns[i]) {
214 client->internal->conns[i] = conn;
218 client->internal->conns =
219 silc_realloc(client->internal->conns, sizeof(*client->internal->conns)
220 * (client->internal->conns_count + 1));
221 client->internal->conns[client->internal->conns_count] = conn;
222 client->internal->conns_count++;
227 /* Removes connection from client. Frees all memory. */
229 void silc_client_del_connection(SilcClient client, SilcClientConnection conn)
233 for (i = 0; i < client->internal->conns_count; i++)
234 if (client->internal->conns[i] == conn) {
236 silc_idcache_free(conn->client_cache);
237 silc_idcache_free(conn->channel_cache);
238 silc_idcache_free(conn->server_cache);
239 if (conn->pending_commands)
240 silc_dlist_uninit(conn->pending_commands);
241 silc_free(conn->remote_host);
242 silc_dlist_uninit(conn->ftp_sessions);
245 client->internal->conns[i] = NULL;
249 /* Adds listener socket to the listener sockets table. This function is
250 used to add socket objects that are listeners to the client. This should
251 not be used to add other connection objects. */
253 void silc_client_add_socket(SilcClient client, SilcSocketConnection sock)
257 if (!client->internal->sockets) {
258 client->internal->sockets =
259 silc_calloc(1, sizeof(*client->internal->sockets));
260 client->internal->sockets[0] = silc_socket_dup(sock);
261 client->internal->sockets_count = 1;
265 for (i = 0; i < client->internal->sockets_count; i++) {
266 if (client->internal->sockets[i] == NULL) {
267 client->internal->sockets[i] = silc_socket_dup(sock);
272 client->internal->sockets =
273 silc_realloc(client->internal->sockets,
274 sizeof(*client->internal->sockets) *
275 (client->internal->sockets_count + 1));
276 client->internal->sockets[client->internal->sockets_count] =
277 silc_socket_dup(sock);
278 client->internal->sockets_count++;
281 /* Deletes listener socket from the listener sockets table. */
283 void silc_client_del_socket(SilcClient client, SilcSocketConnection sock)
287 if (!client->internal->sockets)
290 for (i = 0; i < client->internal->sockets_count; i++) {
291 if (client->internal->sockets[i] == sock) {
292 silc_socket_free(sock);
293 client->internal->sockets[i] = NULL;
300 silc_client_connect_to_server_internal(SilcClientInternalConnectContext *ctx)
304 /* XXX In the future we should give up this non-blocking connect all
305 together and use threads instead. */
306 /* Create connection to server asynchronously */
307 sock = silc_net_create_connection_async(NULL, ctx->port, ctx->host);
311 /* Register task that will receive the async connect and will
313 ctx->task = silc_schedule_task_add(ctx->client->schedule, sock,
314 silc_client_connect_to_server_start,
317 SILC_TASK_PRI_NORMAL);
318 silc_schedule_set_listen_fd(ctx->client->schedule, sock, SILC_TASK_WRITE);
325 /* Connects to remote server. This is the main routine used to connect
326 to SILC server. Returns -1 on error and the created socket otherwise.
327 The `context' is user context that is saved into the SilcClientConnection
328 that is created after the connection is created. Note that application
329 may handle the connecting process outside the library. If this is the
330 case then this function is not used at all. When the connecting is
331 done the `connect' client operation is called. */
333 int silc_client_connect_to_server(SilcClient client, int port,
334 char *host, void *context)
336 SilcClientInternalConnectContext *ctx;
337 SilcClientConnection conn;
340 SILC_LOG_DEBUG(("Connecting to port %d of server %s",
343 conn = silc_client_add_connection(client, host, port, context);
345 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
346 "Connecting to port %d of server %s", port, host);
348 /* Allocate internal context for connection process. This is
349 needed as we are doing async connecting. */
350 ctx = silc_calloc(1, sizeof(*ctx));
351 ctx->client = client;
353 ctx->host = strdup(host);
357 /* Do the actual connecting process */
358 sock = silc_client_connect_to_server_internal(ctx);
360 silc_client_del_connection(client, conn);
364 /* Socket hostname and IP lookup callback that is called before actually
365 starting the key exchange. The lookup is called from the function
366 silc_client_start_key_exchange. */
368 static void silc_client_start_key_exchange_cb(SilcSocketConnection sock,
371 SilcClientConnection conn = (SilcClientConnection)context;
372 SilcClient client = conn->client;
373 SilcProtocol protocol;
374 SilcClientKEInternalContext *proto_ctx;
376 SILC_LOG_DEBUG(("Start"));
378 /* XXX We should most likely use the resolved host name instead of the
379 one user provided for us. */
380 silc_free(conn->sock->hostname);
381 conn->sock->hostname = strdup(conn->remote_host);
383 conn->sock->ip = strdup(conn->remote_host);
384 conn->sock->port = conn->remote_port;
386 /* Allocate internal Key Exchange context. This is sent to the
387 protocol as context. */
388 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
389 proto_ctx->client = (void *)client;
390 proto_ctx->sock = silc_socket_dup(conn->sock);
391 proto_ctx->rng = client->rng;
392 proto_ctx->responder = FALSE;
393 proto_ctx->send_packet = silc_client_protocol_ke_send_packet;
394 proto_ctx->verify = silc_client_protocol_ke_verify_key;
396 /* Perform key exchange protocol. silc_client_connect_to_server_final
397 will be called after the protocol is finished. */
398 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
399 &protocol, (void *)proto_ctx,
400 silc_client_connect_to_server_second);
402 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
403 "Error: Could not start key exchange protocol");
404 silc_net_close_connection(conn->sock->sock);
405 client->internal->ops->connect(client, conn, FALSE);
408 conn->sock->protocol = protocol;
410 /* Register the connection for network input and output. This sets
411 that scheduler will listen for incoming packets for this connection
412 and sets that outgoing packets may be sent to this connection as well.
413 However, this doesn't set the scheduler for outgoing traffic, it will
414 be set separately by calling SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT,
415 later when outgoing data is available. */
416 context = (void *)client;
417 SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(conn->sock->sock);
419 /* Execute the protocol */
420 silc_protocol_execute(protocol, client->schedule, 0, 0);
423 /* Start SILC Key Exchange (SKE) protocol to negotiate shared secret
424 key material between client and server. This function can be called
425 directly if application is performing its own connecting and does not
426 use the connecting provided by this library. This function is normally
427 used only if the application performed the connecting outside the library.
428 The library however may use this internally. */
430 void silc_client_start_key_exchange(SilcClient client,
431 SilcClientConnection conn,
434 /* Allocate new socket connection object */
435 silc_socket_alloc(fd, SILC_SOCKET_TYPE_SERVER, (void *)conn, &conn->sock);
437 /* Sometimes when doing quick reconnects the new socket may be same as
438 the old one and there might be pending stuff for the old socket.
439 If new one is same then those pending sutff might cause problems.
440 Make sure they do not do that. */
441 silc_schedule_task_del_by_fd(client->schedule, fd);
443 conn->nickname = (client->nickname ? strdup(client->nickname) :
444 strdup(client->username));
446 /* Resolve the remote hostname and IP address for our socket connection */
447 silc_socket_host_lookup(conn->sock, FALSE, silc_client_start_key_exchange_cb,
448 conn, client->schedule);
451 /* Callback called when error has occurred during connecting to the server.
452 The `connect' client operation will be called. */
454 SILC_TASK_CALLBACK(silc_client_connect_failure)
456 SilcClientKEInternalContext *ctx =
457 (SilcClientKEInternalContext *)context;
458 SilcClient client = (SilcClient)ctx->client;
460 client->internal->ops->connect(client, ctx->sock->user_data, FALSE);
462 silc_packet_context_free(ctx->packet);
466 /* Start of the connection to the remote server. This is called after
467 succesful TCP/IP connection has been established to the remote host. */
469 SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
471 SilcClientInternalConnectContext *ctx =
472 (SilcClientInternalConnectContext *)context;
473 SilcClient client = ctx->client;
474 SilcClientConnection conn = ctx->conn;
475 int opt, opt_len = sizeof(opt);
477 SILC_LOG_DEBUG(("Start"));
479 /* Check the socket status as it might be in error */
480 silc_net_get_socket_opt(fd, SOL_SOCKET, SO_ERROR, &opt, &opt_len);
482 if (ctx->tries < 2) {
483 /* Connection failed but lets try again */
484 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
485 "Could not connect to server %s: %s",
486 ctx->host, strerror(opt));
487 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
488 "Connecting to port %d of server %s resumed",
489 ctx->port, ctx->host);
491 /* Unregister old connection try */
492 silc_schedule_unset_listen_fd(client->schedule, fd);
493 silc_net_close_connection(fd);
494 silc_schedule_task_del(client->schedule, ctx->task);
497 silc_client_connect_to_server_internal(ctx);
500 /* Connection failed and we won't try anymore */
501 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
502 "Could not connect to server %s: %s",
503 ctx->host, strerror(opt));
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 /* Notify application of failure */
510 client->internal->ops->connect(client, conn, FALSE);
511 silc_client_del_connection(client, conn);
516 silc_schedule_unset_listen_fd(client->schedule, fd);
517 silc_schedule_task_del(client->schedule, ctx->task);
520 silc_client_start_key_exchange(client, conn, fd);
523 /* Second part of the connecting to the server. This executed
524 authentication protocol. */
526 SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
528 SilcProtocol protocol = (SilcProtocol)context;
529 SilcClientKEInternalContext *ctx =
530 (SilcClientKEInternalContext *)protocol->context;
531 SilcClient client = (SilcClient)ctx->client;
532 SilcSocketConnection sock = NULL;
533 SilcClientConnAuthInternalContext *proto_ctx;
535 SILC_LOG_DEBUG(("Start"));
537 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
538 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
539 /* Error occured during protocol */
540 SILC_LOG_DEBUG(("Error during KE protocol"));
541 silc_protocol_free(protocol);
542 silc_ske_free_key_material(ctx->keymat);
544 silc_ske_free(ctx->ske);
546 silc_free(ctx->dest_id);
547 ctx->sock->protocol = NULL;
548 silc_socket_free(ctx->sock);
550 /* Notify application of failure */
551 silc_schedule_task_add(client->schedule, ctx->sock->sock,
552 silc_client_connect_failure, ctx,
553 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
557 /* We now have the key material as the result of the key exchange
558 protocol. Take the key material into use. Free the raw key material
559 as soon as we've set them into use. */
560 silc_client_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
561 ctx->ske->prop->cipher,
562 ctx->ske->prop->pkcs,
563 ctx->ske->prop->hash,
564 ctx->ske->prop->hmac,
565 ctx->ske->prop->group,
567 silc_ske_free_key_material(ctx->keymat);
569 /* Allocate internal context for the authentication protocol. This
570 is sent as context for the protocol. */
571 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
572 proto_ctx->client = (void *)client;
573 proto_ctx->sock = sock = ctx->sock;
574 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
575 proto_ctx->dest_id_type = ctx->dest_id_type;
576 proto_ctx->dest_id = ctx->dest_id;
578 /* Free old protocol as it is finished now */
579 silc_protocol_free(protocol);
581 silc_packet_context_free(ctx->packet);
583 sock->protocol = NULL;
585 /* Resolve the authentication method to be used in this connection. The
586 completion callback is called after the application has resolved
587 the authentication method. */
588 client->internal->ops->get_auth_method(client, sock->user_data,
591 silc_client_resolve_auth_method,
595 /* Authentication method resolving callback. Application calls this function
596 after we've called the client->internal->ops->get_auth_method
597 client operation to resolve the authentication method. We will continue
598 the executiong of the protocol in this function. */
600 void silc_client_resolve_auth_method(bool success,
601 SilcProtocolAuthMeth auth_meth,
602 const unsigned char *auth_data,
603 SilcUInt32 auth_data_len, void *context)
605 SilcClientConnAuthInternalContext *proto_ctx =
606 (SilcClientConnAuthInternalContext *)context;
607 SilcClient client = (SilcClient)proto_ctx->client;
610 auth_meth = SILC_AUTH_NONE;
612 proto_ctx->auth_meth = auth_meth;
614 if (auth_data && auth_data_len) {
615 proto_ctx->auth_data = silc_memdup(auth_data, auth_data_len);
616 proto_ctx->auth_data_len = auth_data_len;
619 /* Allocate the authenteication protocol and execute it. */
620 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
621 &proto_ctx->sock->protocol, (void *)proto_ctx,
622 silc_client_connect_to_server_final);
624 /* Execute the protocol */
625 silc_protocol_execute(proto_ctx->sock->protocol, client->schedule, 0, 0);
628 /* Finalizes the connection to the remote SILC server. This is called
629 after authentication protocol has been completed. This send our
630 user information to the server to receive our client ID from
633 SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
635 SilcProtocol protocol = (SilcProtocol)context;
636 SilcClientConnAuthInternalContext *ctx =
637 (SilcClientConnAuthInternalContext *)protocol->context;
638 SilcClient client = (SilcClient)ctx->client;
639 SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
642 SILC_LOG_DEBUG(("Start"));
644 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
645 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
646 /* Error occured during protocol */
647 SILC_LOG_DEBUG(("Error during authentication protocol"));
648 silc_protocol_free(protocol);
650 silc_free(ctx->auth_data);
652 silc_ske_free(ctx->ske);
654 silc_free(ctx->dest_id);
655 conn->sock->protocol = NULL;
656 silc_socket_free(ctx->sock);
658 /* Notify application of failure */
659 silc_schedule_task_add(client->schedule, ctx->sock->sock,
660 silc_client_connect_failure, ctx,
661 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
665 /* Send NEW_CLIENT packet to the server. We will become registered
666 to the SILC network after sending this packet and we will receive
667 client ID from the server. */
668 packet = silc_buffer_alloc(2 + 2 + strlen(client->username) +
669 strlen(client->realname));
670 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
671 silc_buffer_format(packet,
672 SILC_STR_UI_SHORT(strlen(client->username)),
673 SILC_STR_UI_XNSTRING(client->username,
674 strlen(client->username)),
675 SILC_STR_UI_SHORT(strlen(client->realname)),
676 SILC_STR_UI_XNSTRING(client->realname,
677 strlen(client->realname)),
680 /* Send the packet */
681 silc_client_packet_send(client, ctx->sock, SILC_PACKET_NEW_CLIENT,
683 packet->data, packet->len, TRUE);
684 silc_buffer_free(packet);
686 /* Save remote ID. */
687 conn->remote_id = ctx->dest_id;
688 conn->remote_id_data = silc_id_id2str(ctx->dest_id, SILC_ID_SERVER);
689 conn->remote_id_data_len = silc_id_get_len(ctx->dest_id, SILC_ID_SERVER);
691 /* Register re-key timeout */
692 conn->rekey->timeout = client->internal->params->rekey_secs;
693 conn->rekey->context = (void *)client;
694 silc_schedule_task_add(client->schedule, conn->sock->sock,
695 silc_client_rekey_callback,
696 (void *)conn->sock, conn->rekey->timeout, 0,
697 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
699 silc_protocol_free(protocol);
701 silc_free(ctx->auth_data);
703 silc_ske_free(ctx->ske);
704 silc_socket_free(ctx->sock);
706 conn->sock->protocol = NULL;
709 /* Internal routine that sends packet or marks packet to be sent. This
710 is used directly only in special cases. Normal cases should use
711 silc_server_packet_send. Returns < 0 on error. */
713 int silc_client_packet_send_real(SilcClient client,
714 SilcSocketConnection sock,
719 /* If rekey protocol is active we must assure that all packets are
720 sent through packet queue. */
721 if (SILC_CLIENT_IS_REKEY(sock))
724 /* If outbound data is already pending do not force send */
725 if (SILC_IS_OUTBUF_PENDING(sock))
728 /* Send the packet */
729 ret = silc_packet_send(sock, force_send);
733 /* Mark that there is some outgoing data available for this connection.
734 This call sets the connection both for input and output (the input
735 is set always and this call keeps the input setting, actually).
736 Actual data sending is performed by silc_client_packet_process. */
737 SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(client->schedule, sock->sock);
739 /* Mark to socket that data is pending in outgoing buffer. This flag
740 is needed if new data is added to the buffer before the earlier
741 put data is sent to the network. */
742 SILC_SET_OUTBUF_PENDING(sock);
747 /* Packet processing callback. This is used to send and receive packets
748 from network. This is generic task. */
750 SILC_TASK_CALLBACK_GLOBAL(silc_client_packet_process)
752 SilcClient client = (SilcClient)context;
753 SilcSocketConnection sock = NULL;
754 SilcClientConnection conn;
757 SILC_LOG_DEBUG(("Processing packet"));
759 SILC_CLIENT_GET_SOCK(client, fd, sock);
763 conn = (SilcClientConnection)sock->user_data;
766 if (type == SILC_TASK_WRITE) {
767 /* Do not send data to disconnected connection */
768 if (SILC_IS_DISCONNECTED(sock))
771 if (sock->outbuf->data - sock->outbuf->head)
772 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
774 ret = silc_packet_send(sock, TRUE);
776 /* If returned -2 could not write to connection now, will do
785 /* The packet has been sent and now it is time to set the connection
786 back to only for input. When there is again some outgoing data
787 available for this connection it will be set for output as well.
788 This call clears the output setting and sets it only for input. */
789 SILC_CLIENT_SET_CONNECTION_FOR_INPUT(client->schedule, fd);
790 SILC_UNSET_OUTBUF_PENDING(sock);
792 silc_buffer_clear(sock->outbuf);
796 /* Packet receiving */
797 if (type == SILC_TASK_READ) {
798 /* Read data from network */
799 ret = silc_packet_receive(sock);
805 SILC_LOG_DEBUG(("Read EOF"));
807 /* If connection is disconnecting already we will finally
808 close the connection */
809 if (SILC_IS_DISCONNECTING(sock)) {
810 if (sock == conn->sock && sock->type != SILC_SOCKET_TYPE_CLIENT)
811 client->internal->ops->disconnect(client, conn);
812 silc_client_close_connection(client, sock, conn);
816 SILC_LOG_DEBUG(("EOF from connection %d", sock->sock));
817 if (sock == conn->sock && sock->type != SILC_SOCKET_TYPE_CLIENT)
818 client->internal->ops->disconnect(client, conn);
819 silc_client_close_connection(client, sock, conn);
823 /* Process the packet. This will call the parser that will then
824 decrypt and parse the packet. */
825 if (sock->type != SILC_SOCKET_TYPE_UNKNOWN)
826 silc_packet_receive_process(sock, FALSE, conn->receive_key,
827 conn->hmac_receive, conn->psn_receive,
828 silc_client_packet_parse, client);
830 silc_packet_receive_process(sock, FALSE, NULL, NULL, 0,
831 silc_client_packet_parse, client);
835 /* Parser callback called by silc_packet_receive_process. Thie merely
836 registers timeout that will handle the actual parsing when appropriate. */
838 static bool silc_client_packet_parse(SilcPacketParserContext *parser_context,
841 SilcClient client = (SilcClient)context;
842 SilcSocketConnection sock = parser_context->sock;
843 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
844 SilcPacketContext *packet = parser_context->packet;
847 if (conn && conn->hmac_receive && conn->sock == sock)
848 conn->psn_receive = parser_context->packet->sequence + 1;
850 /* Parse the packet immediately */
851 if (parser_context->normal)
852 ret = silc_packet_parse(packet, conn->receive_key);
854 ret = silc_packet_parse_special(packet, conn->receive_key);
856 if (ret == SILC_PACKET_NONE) {
857 silc_packet_context_free(packet);
858 silc_free(parser_context);
862 /* If protocol for this connection is key exchange or rekey then we'll
863 process all packets synchronously, since there might be packets in
864 queue that we are not able to decrypt without first processing the
865 packets before them. */
866 if ((ret == SILC_PACKET_REKEY || ret == SILC_PACKET_REKEY_DONE) ||
867 (sock->protocol && sock->protocol->protocol &&
868 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
869 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY))) {
871 /* Parse the incoming packet type */
872 silc_client_packet_parse_type(client, sock, packet);
873 silc_packet_context_free(packet);
874 silc_free(parser_context);
876 /* Reprocess the buffer since we'll return FALSE. This is because
877 the `conn->receive_key' might have become valid by processing
878 the previous 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);
890 /* Parse the incoming packet type */
891 silc_client_packet_parse_type(client, sock, packet);
892 silc_packet_context_free(packet);
893 silc_free(parser_context);
897 /* Parses the packet type and calls what ever routines the packet type
898 requires. This is done for all incoming packets. */
900 void silc_client_packet_parse_type(SilcClient client,
901 SilcSocketConnection sock,
902 SilcPacketContext *packet)
904 SilcBuffer buffer = packet->buffer;
905 SilcPacketType type = packet->type;
907 SILC_LOG_DEBUG(("Parsing packet type %d", type));
909 /* Parse the packet type */
911 case SILC_PACKET_DISCONNECT:
912 silc_client_disconnected_by_server(client, sock, buffer);
914 case SILC_PACKET_SUCCESS:
916 * Success received for something. For now we can have only
917 * one protocol for connection executing at once hence this
918 * success message is for whatever protocol is executing currently.
921 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
923 case SILC_PACKET_FAILURE:
925 * Failure received for some protocol. Set the protocol state to
926 * error and call the protocol callback. This fill cause error on
927 * protocol and it will call the final callback.
929 silc_client_process_failure(client, sock, packet);
931 case SILC_PACKET_REJECT:
934 case SILC_PACKET_NOTIFY:
936 * Received notify message
938 silc_client_notify_by_server(client, sock, packet);
941 case SILC_PACKET_ERROR:
943 * Received error message
945 silc_client_error_by_server(client, sock, buffer);
948 case SILC_PACKET_CHANNEL_MESSAGE:
950 * Received message to (from, actually) a channel
952 silc_client_channel_message(client, sock, packet);
954 case SILC_PACKET_CHANNEL_KEY:
956 * Received key for a channel. By receiving this key the client will be
957 * able to talk to the channel it has just joined. This can also be
958 * a new key for existing channel as keys expire peridiocally.
960 silc_client_receive_channel_key(client, sock, buffer);
963 case SILC_PACKET_PRIVATE_MESSAGE:
965 * Received private message
967 silc_client_private_message(client, sock, packet);
969 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
971 * Received private message key
975 case SILC_PACKET_COMMAND_REPLY:
977 * Recived reply for a command
979 silc_client_command_reply_process(client, sock, packet);
982 case SILC_PACKET_KEY_EXCHANGE:
983 if (sock->protocol && sock->protocol->protocol &&
984 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE) {
985 SilcClientKEInternalContext *proto_ctx =
986 (SilcClientKEInternalContext *)sock->protocol->context;
988 proto_ctx->packet = silc_packet_context_dup(packet);
989 proto_ctx->dest_id_type = packet->src_id_type;
990 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
991 packet->src_id_type);
992 if (!proto_ctx->dest_id)
995 /* Let the protocol handle the packet */
996 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
998 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
999 "protocol active, packet dropped."));
1003 case SILC_PACKET_KEY_EXCHANGE_1:
1004 if (sock->protocol && sock->protocol->protocol &&
1005 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
1006 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)) {
1008 if (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
1009 SilcClientRekeyInternalContext *proto_ctx =
1010 (SilcClientRekeyInternalContext *)sock->protocol->context;
1012 if (proto_ctx->packet)
1013 silc_packet_context_free(proto_ctx->packet);
1015 proto_ctx->packet = silc_packet_context_dup(packet);
1017 /* Let the protocol handle the packet */
1018 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1020 SilcClientKEInternalContext *proto_ctx =
1021 (SilcClientKEInternalContext *)sock->protocol->context;
1023 if (proto_ctx->packet)
1024 silc_packet_context_free(proto_ctx->packet);
1026 proto_ctx->packet = silc_packet_context_dup(packet);
1027 proto_ctx->dest_id_type = packet->src_id_type;
1028 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1029 packet->src_id_type);
1030 if (!proto_ctx->dest_id)
1033 /* Let the protocol handle the packet */
1034 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1037 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1038 "protocol active, packet dropped."));
1041 case SILC_PACKET_KEY_EXCHANGE_2:
1042 if (sock->protocol && sock->protocol->protocol &&
1043 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
1044 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)) {
1046 if (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
1047 SilcClientRekeyInternalContext *proto_ctx =
1048 (SilcClientRekeyInternalContext *)sock->protocol->context;
1050 if (proto_ctx->packet)
1051 silc_packet_context_free(proto_ctx->packet);
1053 proto_ctx->packet = silc_packet_context_dup(packet);
1055 /* Let the protocol handle the packet */
1056 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1058 SilcClientKEInternalContext *proto_ctx =
1059 (SilcClientKEInternalContext *)sock->protocol->context;
1061 if (proto_ctx->packet)
1062 silc_packet_context_free(proto_ctx->packet);
1063 if (proto_ctx->dest_id)
1064 silc_free(proto_ctx->dest_id);
1065 proto_ctx->packet = silc_packet_context_dup(packet);
1066 proto_ctx->dest_id_type = packet->src_id_type;
1067 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1068 packet->src_id_type);
1069 if (!proto_ctx->dest_id)
1072 /* Let the protocol handle the packet */
1073 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1076 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1077 "protocol active, packet dropped."));
1081 case SILC_PACKET_NEW_ID:
1084 * Received new ID from server. This packet is received at
1085 * the connection to the server. New ID is also received when
1086 * user changes nickname but in that case the new ID is received
1087 * as command reply and not as this packet type.
1091 idp = silc_id_payload_parse(buffer->data, buffer->len);
1094 if (silc_id_payload_get_type(idp) != SILC_ID_CLIENT)
1097 silc_client_receive_new_id(client, sock, idp);
1098 silc_id_payload_free(idp);
1102 case SILC_PACKET_HEARTBEAT:
1104 * Received heartbeat packet
1106 SILC_LOG_DEBUG(("Heartbeat packet"));
1109 case SILC_PACKET_KEY_AGREEMENT:
1111 * Received key agreement packet
1113 SILC_LOG_DEBUG(("Key agreement packet"));
1114 silc_client_key_agreement(client, sock, packet);
1117 case SILC_PACKET_REKEY:
1118 SILC_LOG_DEBUG(("Re-key packet"));
1119 /* We ignore this for now */
1122 case SILC_PACKET_REKEY_DONE:
1123 SILC_LOG_DEBUG(("Re-key done packet"));
1125 if (sock->protocol && sock->protocol->protocol &&
1126 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
1128 SilcClientRekeyInternalContext *proto_ctx =
1129 (SilcClientRekeyInternalContext *)sock->protocol->context;
1131 if (proto_ctx->packet)
1132 silc_packet_context_free(proto_ctx->packet);
1134 proto_ctx->packet = silc_packet_context_dup(packet);
1136 /* Let the protocol handle the packet */
1137 if (proto_ctx->responder == FALSE)
1138 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1140 /* Let the protocol handle the packet */
1141 silc_protocol_execute(sock->protocol, client->schedule,
1144 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
1145 "protocol active, packet dropped."));
1149 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1151 * Reveived reply to our connection authentication method request
1152 * packet. This is used to resolve the authentication method for the
1153 * current session from the server if the client does not know it.
1155 silc_client_connection_auth_request(client, sock, packet);
1158 case SILC_PACKET_FTP:
1159 /* Received file transfer packet. */
1160 silc_client_ftp(client, sock, packet);
1164 SILC_LOG_DEBUG(("Incorrect packet type %d, packet dropped", type));
1169 /* Sends packet. This doesn't actually send the packet instead it assembles
1170 it and marks it to be sent. However, if force_send is TRUE the packet
1171 is sent immediately. if dst_id, cipher and hmac are NULL those parameters
1172 will be derived from sock argument. Otherwise the valid arguments sent
1175 void silc_client_packet_send(SilcClient client,
1176 SilcSocketConnection sock,
1177 SilcPacketType type,
1179 SilcIdType dst_id_type,
1182 unsigned char *data,
1183 SilcUInt32 data_len,
1186 SilcPacketContext packetdata;
1188 SilcUInt32 sequence = 0;
1193 SILC_LOG_DEBUG(("Sending packet, type %d", type));
1195 /* Get data used in the packet sending, keys and stuff */
1196 if ((!cipher || !hmac || !dst_id) && sock->user_data) {
1197 if (!cipher && ((SilcClientConnection)sock->user_data)->send_key)
1198 cipher = ((SilcClientConnection)sock->user_data)->send_key;
1200 if (!hmac && ((SilcClientConnection)sock->user_data)->hmac_send)
1201 hmac = ((SilcClientConnection)sock->user_data)->hmac_send;
1203 if (!dst_id && ((SilcClientConnection)sock->user_data)->remote_id) {
1204 dst_id = ((SilcClientConnection)sock->user_data)->remote_id;
1205 dst_id_type = SILC_ID_SERVER;
1209 sequence = ((SilcClientConnection)sock->user_data)->psn_send++;
1212 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
1214 /* Set the packet context pointers */
1215 packetdata.flags = 0;
1216 packetdata.type = type;
1217 if (sock->user_data &&
1218 ((SilcClientConnection)sock->user_data)->local_id_data) {
1219 packetdata.src_id = ((SilcClientConnection)sock->user_data)->local_id_data;
1220 packetdata.src_id_len =
1221 silc_id_get_len(((SilcClientConnection)sock->user_data)->local_id,
1224 packetdata.src_id = silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char));
1225 packetdata.src_id_len = SILC_ID_CLIENT_LEN;
1227 packetdata.src_id_type = SILC_ID_CLIENT;
1229 packetdata.dst_id = silc_id_id2str(dst_id, dst_id_type);
1230 packetdata.dst_id_len = silc_id_get_len(dst_id, dst_id_type);
1231 packetdata.dst_id_type = dst_id_type;
1233 packetdata.dst_id = NULL;
1234 packetdata.dst_id_len = 0;
1235 packetdata.dst_id_type = SILC_ID_NONE;
1237 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
1238 packetdata.src_id_len +
1239 packetdata.dst_id_len));
1240 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1241 packetdata.src_id_len + packetdata.dst_id_len;
1242 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
1244 /* Prepare outgoing data buffer for packet sending */
1245 silc_packet_send_prepare(sock,
1246 SILC_PACKET_HEADER_LEN +
1247 packetdata.src_id_len +
1248 packetdata.dst_id_len,
1252 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
1254 packetdata.buffer = sock->outbuf;
1256 /* Put the data to the buffer */
1257 if (data && data_len)
1258 silc_buffer_put(sock->outbuf, data, data_len);
1260 /* Create the outgoing packet */
1261 silc_packet_assemble(&packetdata, cipher);
1263 /* Encrypt the packet */
1265 silc_packet_encrypt(cipher, hmac, sequence, sock->outbuf,
1268 SILC_LOG_HEXDUMP(("Packet (%d), len %d", sequence, sock->outbuf->len),
1269 sock->outbuf->data, sock->outbuf->len);
1271 /* Now actually send the packet */
1272 silc_client_packet_send_real(client, sock, force_send);
1275 void silc_client_packet_queue_purge(SilcClient client,
1276 SilcSocketConnection sock)
1278 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1279 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1280 if (sock->outbuf->data - sock->outbuf->head)
1281 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
1283 silc_packet_send(sock, TRUE);
1285 SILC_CLIENT_SET_CONNECTION_FOR_INPUT(client->schedule, sock->sock);
1286 SILC_UNSET_OUTBUF_PENDING(sock);
1287 silc_buffer_clear(sock->outbuf);
1291 /* Closes connection to remote end. Free's all allocated data except
1292 for some information such as nickname etc. that are valid at all time.
1293 If the `sock' is NULL then the conn->sock will be used. If `sock' is
1294 provided it will be checked whether the sock and `conn->sock' are the
1295 same (they can be different, ie. a socket can use `conn' as its
1296 connection but `conn->sock' might be actually a different connection
1297 than the `sock'). */
1299 void silc_client_close_connection(SilcClient client,
1300 SilcSocketConnection sock,
1301 SilcClientConnection conn)
1305 SILC_LOG_DEBUG(("Start"));
1307 if (!sock || (sock && conn->sock == sock))
1312 /* We won't listen for this connection anymore */
1313 silc_schedule_unset_listen_fd(client->schedule, sock->sock);
1315 /* Unregister all tasks */
1316 silc_schedule_task_del_by_fd(client->schedule, sock->sock);
1317 silc_schedule_task_del_by_fd(client->schedule, sock->sock);
1319 /* Close the actual connection */
1320 silc_net_close_connection(sock->sock);
1322 /* Cancel any active protocol */
1323 if (sock->protocol) {
1324 if (sock->protocol->protocol->type ==
1325 SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
1326 sock->protocol->protocol->type ==
1327 SILC_PROTOCOL_CLIENT_CONNECTION_AUTH) {
1328 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
1329 silc_protocol_execute_final(sock->protocol, client->schedule);
1330 /* The application will recall this function with these protocols
1331 (the ops->connect client operation). */
1334 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
1335 silc_protocol_execute_final(sock->protocol, client->schedule);
1336 sock->protocol = NULL;
1340 /* Free everything */
1341 if (del && sock->user_data) {
1342 /* Free all cache entries */
1343 SilcIDCacheList list;
1344 SilcIDCacheEntry entry;
1347 if (silc_idcache_get_all(conn->client_cache, &list)) {
1348 ret = silc_idcache_list_first(list, &entry);
1350 silc_client_del_client(client, conn, entry->context);
1351 ret = silc_idcache_list_next(list, &entry);
1353 silc_idcache_list_free(list);
1356 if (silc_idcache_get_all(conn->channel_cache, &list)) {
1357 ret = silc_idcache_list_first(list, &entry);
1359 silc_client_del_channel(client, conn, entry->context);
1360 ret = silc_idcache_list_next(list, &entry);
1362 silc_idcache_list_free(list);
1365 if (silc_idcache_get_all(conn->server_cache, &list)) {
1366 ret = silc_idcache_list_first(list, &entry);
1368 silc_client_del_server(client, conn, entry->context);
1369 ret = silc_idcache_list_next(list, &entry);
1371 silc_idcache_list_free(list);
1374 /* Clear ID caches */
1375 if (conn->client_cache)
1376 silc_idcache_free(conn->client_cache);
1377 if (conn->channel_cache)
1378 silc_idcache_free(conn->channel_cache);
1379 if (conn->server_cache)
1380 silc_idcache_free(conn->server_cache);
1382 /* Free data (my ID is freed in above silc_client_del_client).
1383 conn->nickname is freed when freeing the local_entry->nickname. */
1384 if (conn->remote_host)
1385 silc_free(conn->remote_host);
1386 if (conn->local_id_data)
1387 silc_free(conn->local_id_data);
1389 silc_cipher_free(conn->send_key);
1390 if (conn->receive_key)
1391 silc_cipher_free(conn->receive_key);
1392 if (conn->hmac_send)
1393 silc_hmac_free(conn->hmac_send);
1394 if (conn->hmac_receive)
1395 silc_hmac_free(conn->hmac_receive);
1396 if (conn->pending_commands)
1397 silc_dlist_uninit(conn->pending_commands);
1399 silc_free(conn->rekey);
1401 if (conn->active_session) {
1402 sock->user_data = NULL;
1403 silc_client_ftp_session_free(conn->active_session);
1404 conn->active_session = NULL;
1407 silc_client_ftp_free_sessions(client, conn);
1409 memset(conn, 0, sizeof(*conn));
1410 silc_client_del_connection(client, conn);
1413 silc_socket_free(sock);
1416 /* Called when we receive disconnection packet from server. This
1417 closes our end properly and displays the reason of the disconnection
1420 SILC_TASK_CALLBACK(silc_client_disconnected_by_server_later)
1422 SilcClient client = (SilcClient)context;
1423 SilcSocketConnection sock;
1425 SILC_CLIENT_GET_SOCK(client, fd, sock);
1429 silc_client_close_connection(client, sock, sock->user_data);
1432 /* Called when we receive disconnection packet from server. This
1433 closes our end properly and displays the reason of the disconnection
1436 void silc_client_disconnected_by_server(SilcClient client,
1437 SilcSocketConnection sock,
1442 SILC_LOG_DEBUG(("Server disconnected us, sock %d", sock->sock));
1444 msg = silc_memdup(message->data, message->len);
1445 client->internal->ops->say(client, sock->user_data,
1446 SILC_CLIENT_MESSAGE_AUDIT, msg);
1449 SILC_SET_DISCONNECTED(sock);
1451 /* Close connection through scheduler. */
1452 silc_schedule_task_add(client->schedule, sock->sock,
1453 silc_client_disconnected_by_server_later,
1454 client, 0, 1, SILC_TASK_TIMEOUT,
1455 SILC_TASK_PRI_NORMAL);
1458 /* Received error message from server. Display it on the screen.
1459 We don't take any action what so ever of the error message. */
1461 void silc_client_error_by_server(SilcClient client,
1462 SilcSocketConnection sock,
1467 msg = silc_memdup(message->data, message->len);
1468 client->internal->ops->say(client, sock->user_data,
1469 SILC_CLIENT_MESSAGE_AUDIT, msg);
1473 /* Auto-nicking callback to send NICK command to server. */
1475 SILC_TASK_CALLBACK(silc_client_send_auto_nick)
1477 SilcClientConnection conn = (SilcClientConnection)context;
1478 SilcClient client = conn->client;
1480 silc_client_command_send(client, conn, SILC_COMMAND_NICK,
1481 ++conn->cmd_ident, 1, 1,
1482 client->nickname, strlen(client->nickname));
1485 /* Processes the received new Client ID from server. Old Client ID is
1486 deleted from cache and new one is added. */
1488 void silc_client_receive_new_id(SilcClient client,
1489 SilcSocketConnection sock,
1492 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1493 int connecting = FALSE;
1494 SilcClientID *client_id = silc_id_payload_get_id(idp);
1497 if (!conn->local_entry)
1500 /* Delete old ID from ID cache */
1501 if (conn->local_id) {
1502 /* Check whether they are different */
1503 if (SILC_ID_CLIENT_COMPARE(conn->local_id, client_id)) {
1504 silc_free(client_id);
1508 silc_idcache_del_by_context(conn->client_cache, conn->local_entry);
1509 silc_free(conn->local_id);
1512 /* Save the new ID */
1514 if (conn->local_id_data)
1515 silc_free(conn->local_id_data);
1517 conn->local_id = client_id;
1518 conn->local_id_data = silc_id_payload_get_data(idp);
1519 conn->local_id_data_len = silc_id_payload_get_len(idp);;
1521 if (!conn->local_entry)
1522 conn->local_entry = silc_calloc(1, sizeof(*conn->local_entry));
1524 conn->local_entry->nickname = conn->nickname;
1525 if (!conn->local_entry->username)
1526 conn->local_entry->username = strdup(client->username);
1527 if (!conn->local_entry->hostname)
1528 conn->local_entry->hostname = strdup(client->hostname);
1529 if (!conn->local_entry->server)
1530 conn->local_entry->server = strdup(conn->remote_host);
1531 conn->local_entry->id = conn->local_id;
1532 conn->local_entry->valid = TRUE;
1533 if (!conn->local_entry->channels)
1534 conn->local_entry->channels = silc_hash_table_alloc(1, silc_hash_ptr,
1539 /* Put it to the ID cache */
1540 silc_idcache_add(conn->client_cache, strdup(conn->nickname), conn->local_id,
1541 (void *)conn->local_entry, 0, NULL);
1544 /* Send NICK command if the nickname was set by the application (and is
1545 not same as the username). Send this with little timeout. */
1546 if (client->nickname && strcmp(client->nickname, client->username))
1547 silc_schedule_task_add(client->schedule, 0,
1548 silc_client_send_auto_nick, conn,
1549 1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1551 /* Issue INFO command to fetch the real server name and server information
1553 silc_client_command_register(client, SILC_COMMAND_INFO, NULL, NULL,
1554 silc_client_command_reply_info_i, 0,
1556 sidp = silc_id_payload_encode(conn->remote_id, SILC_ID_SERVER);
1557 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
1558 conn->cmd_ident, 1, 2, sidp->data, sidp->len);
1559 silc_buffer_free(sidp);
1561 /* Notify application of successful connection. We do it here now that
1562 we've received the Client ID and are allowed to send traffic. */
1563 client->internal->ops->connect(client, conn, TRUE);
1567 /* Removes a client entry from all channels it has joined. */
1569 void silc_client_remove_from_channels(SilcClient client,
1570 SilcClientConnection conn,
1571 SilcClientEntry client_entry)
1573 SilcHashTableList htl;
1574 SilcChannelUser chu;
1576 silc_hash_table_list(client_entry->channels, &htl);
1577 while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
1578 silc_hash_table_del(chu->client->channels, chu->channel);
1579 silc_hash_table_del(chu->channel->user_list, chu->client);
1583 silc_hash_table_list_reset(&htl);
1586 /* Replaces `old' client entries from all channels to `new' client entry.
1587 This can be called for example when nickname changes and old ID entry
1588 is replaced from ID cache with the new one. If the old ID entry is only
1589 updated, then this fucntion needs not to be called. */
1591 void silc_client_replace_from_channels(SilcClient client,
1592 SilcClientConnection conn,
1593 SilcClientEntry old,
1594 SilcClientEntry new)
1596 SilcHashTableList htl;
1597 SilcChannelUser chu;
1599 silc_hash_table_list(old->channels, &htl);
1600 while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
1601 /* Replace client entry */
1602 silc_hash_table_del(chu->client->channels, chu->channel);
1603 silc_hash_table_del(chu->channel->user_list, chu->client);
1606 silc_hash_table_add(chu->channel->user_list, chu->client, chu);
1607 silc_hash_table_add(chu->client->channels, chu->channel, chu);
1609 silc_hash_table_list_reset(&htl);
1612 /* Registers failure timeout to process the received failure packet
1615 void silc_client_process_failure(SilcClient client,
1616 SilcSocketConnection sock,
1617 SilcPacketContext *packet)
1619 SilcUInt32 failure = 0;
1621 if (sock->protocol) {
1622 if (packet->buffer->len >= 4)
1623 SILC_GET32_MSB(failure, packet->buffer->data);
1625 /* Notify application */
1626 client->internal->ops->failure(client, sock->user_data, sock->protocol,
1631 /* A timeout callback for the re-key. We will be the initiator of the
1634 SILC_TASK_CALLBACK(silc_client_rekey_callback)
1636 SilcSocketConnection sock = (SilcSocketConnection)context;
1637 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1638 SilcClient client = (SilcClient)conn->rekey->context;
1639 SilcProtocol protocol;
1640 SilcClientRekeyInternalContext *proto_ctx;
1642 SILC_LOG_DEBUG(("Start"));
1644 /* Allocate internal protocol context. This is sent as context
1646 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1647 proto_ctx->client = (void *)client;
1648 proto_ctx->sock = silc_socket_dup(sock);
1649 proto_ctx->responder = FALSE;
1650 proto_ctx->pfs = conn->rekey->pfs;
1652 /* Perform rekey protocol. Will call the final callback after the
1653 protocol is over. */
1654 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_REKEY,
1655 &protocol, proto_ctx, silc_client_rekey_final);
1656 sock->protocol = protocol;
1658 /* Run the protocol */
1659 silc_protocol_execute(protocol, client->schedule, 0, 0);
1661 /* Re-register re-key timeout */
1662 silc_schedule_task_add(client->schedule, sock->sock,
1663 silc_client_rekey_callback,
1664 context, conn->rekey->timeout, 0,
1665 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1668 /* The final callback for the REKEY protocol. This will actually take the
1669 new key material into use. */
1671 SILC_TASK_CALLBACK(silc_client_rekey_final)
1673 SilcProtocol protocol = (SilcProtocol)context;
1674 SilcClientRekeyInternalContext *ctx =
1675 (SilcClientRekeyInternalContext *)protocol->context;
1676 SilcClient client = (SilcClient)ctx->client;
1677 SilcSocketConnection sock = ctx->sock;
1679 SILC_LOG_DEBUG(("Start"));
1681 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1682 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1683 /* Error occured during protocol */
1684 silc_protocol_cancel(protocol, client->schedule);
1685 silc_protocol_free(protocol);
1686 sock->protocol = NULL;
1688 silc_packet_context_free(ctx->packet);
1690 silc_ske_free(ctx->ske);
1691 silc_socket_free(ctx->sock);
1696 /* Purge the outgoing data queue to assure that all rekey packets really
1697 go to the network before we quit the protocol. */
1698 silc_client_packet_queue_purge(client, sock);
1701 silc_protocol_free(protocol);
1702 sock->protocol = NULL;
1704 silc_packet_context_free(ctx->packet);
1706 silc_ske_free(ctx->ske);
1707 silc_socket_free(ctx->sock);
1711 /* Processes incoming connection authentication method request packet.
1712 It is a reply to our previously sent request. The packet can be used
1713 to resolve the authentication method for the current session if the
1714 client does not know it beforehand. */
1716 void silc_client_connection_auth_request(SilcClient client,
1717 SilcSocketConnection sock,
1718 SilcPacketContext *packet)
1720 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1721 SilcUInt16 conn_type, auth_meth;
1724 /* If we haven't send our request then ignore this one. */
1725 if (!conn->connauth)
1728 /* Parse the payload */
1729 ret = silc_buffer_unformat(packet->buffer,
1730 SILC_STR_UI_SHORT(&conn_type),
1731 SILC_STR_UI_SHORT(&auth_meth),
1734 auth_meth = SILC_AUTH_NONE;
1736 /* Call the request callback to notify application for received
1737 authentication method information. */
1738 if (conn->connauth->callback)
1739 (*conn->connauth->callback)(client, conn, auth_meth,
1740 conn->connauth->context);
1742 silc_schedule_task_del(client->schedule, conn->connauth->timeout);
1744 silc_free(conn->connauth);
1745 conn->connauth = NULL;
1748 /* Timeout task callback called if the server does not reply to our
1749 connection authentication method request in the specified time interval. */
1751 SILC_TASK_CALLBACK(silc_client_request_authentication_method_timeout)
1753 SilcClientConnection conn = (SilcClientConnection)context;
1754 SilcClient client = conn->client;
1756 if (!conn->connauth)
1759 /* Call the request callback to notify application */
1760 if (conn->connauth->callback)
1761 (*conn->connauth->callback)(client, conn, SILC_AUTH_NONE,
1762 conn->connauth->context);
1764 silc_free(conn->connauth);
1765 conn->connauth = NULL;
1768 /* This function can be used to request the current authentication method
1769 from the server. This may be called when connecting to the server
1770 and the client library requests the authentication data from the
1771 application. If the application does not know the current authentication
1772 method it can request it from the server using this function.
1773 The `callback' with `context' will be called after the server has
1774 replied back with the current authentication method. */
1777 silc_client_request_authentication_method(SilcClient client,
1778 SilcClientConnection conn,
1779 SilcConnectionAuthRequest callback,
1782 SilcClientConnAuthRequest connauth;
1785 connauth = silc_calloc(1, sizeof(*connauth));
1786 connauth->callback = callback;
1787 connauth->context = context;
1790 silc_free(conn->connauth);
1792 conn->connauth = connauth;
1794 /* Assemble the request packet and send it to the server */
1795 packet = silc_buffer_alloc(4);
1796 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1797 silc_buffer_format(packet,
1798 SILC_STR_UI_SHORT(SILC_SOCKET_TYPE_CLIENT),
1799 SILC_STR_UI_SHORT(SILC_AUTH_NONE),
1801 silc_client_packet_send(client, conn->sock,
1802 SILC_PACKET_CONNECTION_AUTH_REQUEST,
1803 NULL, 0, NULL, NULL,
1804 packet->data, packet->len, FALSE);
1805 silc_buffer_free(packet);
1807 /* Register a timeout in case server does not reply anything back. */
1809 silc_schedule_task_add(client->schedule, conn->sock->sock,
1810 silc_client_request_authentication_method_timeout,
1812 client->internal->params->connauth_request_secs, 0,
1813 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);