5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "clientlibincludes.h"
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_packet_parse_real);
30 SILC_TASK_CALLBACK(silc_client_rekey_callback);
31 SILC_TASK_CALLBACK(silc_client_rekey_final);
33 static void 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 uint32 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;
56 new_client->ops = ops;
57 new_client->silc_client_version = strdup(silc_version);
58 new_client->params = silc_calloc(1, sizeof(*new_client->params));
61 memcpy(new_client->params, params, sizeof(*params));
63 if (!new_client->params->rekey_secs)
64 new_client->params->rekey_secs = 3600;
66 if (!new_client->params->connauth_request_secs)
67 new_client->params->connauth_request_secs = 2;
72 /* Frees client object and its internals. */
74 void silc_client_free(SilcClient client)
78 silc_rng_free(client->rng);
80 silc_free(client->silc_client_version);
81 silc_free(client->params);
86 /* Initializes the client. This makes all the necessary steps to make
87 the client ready to be run. One must call silc_client_run to run the
88 client. Returns FALSE if error occured, TRUE otherwise. */
90 int silc_client_init(SilcClient client)
92 SILC_LOG_DEBUG(("Initializing client"));
94 /* Initialize hash functions for client to use */
95 silc_hash_alloc("md5", &client->md5hash);
96 silc_hash_alloc("sha1", &client->sha1hash);
98 /* Initialize none cipher */
99 silc_cipher_alloc("none", &client->none_cipher);
101 /* Initialize random number generator */
102 client->rng = silc_rng_alloc();
103 silc_rng_init(client->rng);
104 silc_rng_global_init(client->rng);
106 /* Register protocols */
107 silc_client_protocols_register();
109 /* Initialize the scheduler */
110 client->schedule = silc_schedule_init(client->params->task_max ?
111 client->params->task_max : 200);
112 if (!client->schedule)
118 /* Stops the client. This is called to stop the client and thus to stop
121 void silc_client_stop(SilcClient client)
123 SILC_LOG_DEBUG(("Stopping client"));
125 silc_schedule_stop(client->schedule);
126 silc_schedule_uninit(client->schedule);
128 silc_client_protocols_unregister();
130 SILC_LOG_DEBUG(("Client stopped"));
133 /* Runs the client. This starts the scheduler from the utility library.
134 When this functions returns the execution of the appliation is over. */
136 void silc_client_run(SilcClient client)
138 SILC_LOG_DEBUG(("Running client"));
140 /* Start the scheduler, the heart of the SILC client. When this returns
141 the program will be terminated. */
142 silc_schedule(client->schedule);
145 /* Allocates and adds new connection to the client. This adds the allocated
146 connection to the connection table and returns a pointer to it. A client
147 can have multiple connections to multiple servers. Every connection must
148 be added to the client using this function. User data `context' may
149 be sent as argument. This function is normally used only if the
150 application performed the connecting outside the library. The library
151 however may use this internally. */
153 SilcClientConnection silc_client_add_connection(SilcClient client,
158 SilcClientConnection conn;
161 conn = silc_calloc(1, sizeof(*conn));
163 /* Initialize ID caches */
164 conn->client_cache = silc_idcache_alloc(0, SILC_ID_CLIENT, NULL);
165 conn->channel_cache = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
166 conn->server_cache = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
167 conn->client = client;
168 conn->remote_host = strdup(hostname);
169 conn->remote_port = port;
170 conn->context = context;
171 conn->pending_commands = silc_dlist_init();
173 /* Add the connection to connections table */
174 for (i = 0; i < client->conns_count; i++)
175 if (client->conns && !client->conns[i]) {
176 client->conns[i] = conn;
180 client->conns = silc_realloc(client->conns, sizeof(*client->conns)
181 * (client->conns_count + 1));
182 client->conns[client->conns_count] = conn;
183 client->conns_count++;
188 /* Removes connection from client. Frees all memory. */
190 void silc_client_del_connection(SilcClient client, SilcClientConnection conn)
194 for (i = 0; i < client->conns_count; i++)
195 if (client->conns[i] == conn) {
196 if (conn->pending_commands)
197 silc_dlist_uninit(conn->pending_commands);
199 client->conns[i] = NULL;
203 /* Adds listener socket to the listener sockets table. This function is
204 used to add socket objects that are listeners to the client. This should
205 not be used to add other connection objects. */
207 void silc_client_add_socket(SilcClient client, SilcSocketConnection sock)
211 if (!client->sockets) {
212 client->sockets = silc_calloc(1, sizeof(*client->sockets));
213 client->sockets[0] = silc_socket_dup(sock);
214 client->sockets_count = 1;
218 for (i = 0; i < client->sockets_count; i++) {
219 if (client->sockets[i] == NULL) {
220 client->sockets[i] = silc_socket_dup(sock);
225 client->sockets = silc_realloc(client->sockets, sizeof(*client->sockets) *
226 (client->sockets_count + 1));
227 client->sockets[client->sockets_count] = silc_socket_dup(sock);
228 client->sockets_count++;
231 /* Deletes listener socket from the listener sockets table. */
233 void silc_client_del_socket(SilcClient client, SilcSocketConnection sock)
237 if (!client->sockets)
240 for (i = 0; i < client->sockets_count; i++) {
241 if (client->sockets[i] == sock) {
242 silc_socket_free(sock);
243 client->sockets[i] = NULL;
250 silc_client_connect_to_server_internal(SilcClientInternalConnectContext *ctx)
254 /* XXX In the future we should give up this non-blocking connect all
255 together and use threads instead. */
256 /* Create connection to server asynchronously */
257 sock = silc_net_create_connection_async(NULL, ctx->port, ctx->host);
261 /* Register task that will receive the async connect and will
263 ctx->task = silc_schedule_task_add(ctx->client->schedule, sock,
264 silc_client_connect_to_server_start,
267 SILC_TASK_PRI_NORMAL);
268 silc_schedule_set_listen_fd(ctx->client->schedule, sock, SILC_TASK_WRITE);
275 /* Connects to remote server. This is the main routine used to connect
276 to SILC server. Returns -1 on error and the created socket otherwise.
277 The `context' is user context that is saved into the SilcClientConnection
278 that is created after the connection is created. Note that application
279 may handle the connecting process outside the library. If this is the
280 case then this function is not used at all. When the connecting is
281 done the `connect' client operation is called. */
283 int silc_client_connect_to_server(SilcClient client, int port,
284 char *host, void *context)
286 SilcClientInternalConnectContext *ctx;
287 SilcClientConnection conn;
290 SILC_LOG_DEBUG(("Connecting to port %d of server %s",
293 conn = silc_client_add_connection(client, host, port, context);
295 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
296 "Connecting to port %d of server %s", port, host);
298 /* Allocate internal context for connection process. This is
299 needed as we are doing async connecting. */
300 ctx = silc_calloc(1, sizeof(*ctx));
301 ctx->client = client;
303 ctx->host = strdup(host);
307 /* Do the actual connecting process */
308 sock = silc_client_connect_to_server_internal(ctx);
310 silc_client_del_connection(client, conn);
314 /* Start SILC Key Exchange (SKE) protocol to negotiate shared secret
315 key material between client and server. This function can be called
316 directly if application is performing its own connecting and does not
317 use the connecting provided by this library. This function is normally
318 used only if the application performed the connecting outside the library.
319 The library however may use this internally. */
321 int silc_client_start_key_exchange(SilcClient client,
322 SilcClientConnection conn,
325 SilcProtocol protocol;
326 SilcClientKEInternalContext *proto_ctx;
329 /* Allocate new socket connection object */
330 silc_socket_alloc(fd, SILC_SOCKET_TYPE_SERVER, (void *)conn, &conn->sock);
332 /* Sometimes when doing quick reconnects the new socket may be same as
333 the old one and there might be pending stuff for the old socket.
334 If new one is same then those pending sutff might cause problems.
335 Make sure they do not do that. */
336 silc_schedule_task_del_by_fd(client->schedule, fd);
338 conn->nickname = strdup(client->username);
339 conn->sock->hostname = conn->remote_host;
340 conn->sock->ip = strdup(conn->remote_host);
341 conn->sock->port = conn->remote_port;
343 /* Allocate internal Key Exchange context. This is sent to the
344 protocol as context. */
345 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
346 proto_ctx->client = (void *)client;
347 proto_ctx->sock = silc_socket_dup(conn->sock);
348 proto_ctx->rng = client->rng;
349 proto_ctx->responder = FALSE;
350 proto_ctx->send_packet = silc_client_protocol_ke_send_packet;
351 proto_ctx->verify = silc_client_protocol_ke_verify_key;
353 /* Perform key exchange protocol. silc_client_connect_to_server_final
354 will be called after the protocol is finished. */
355 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
356 &protocol, (void *)proto_ctx,
357 silc_client_connect_to_server_second);
359 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
360 "Error: Could not start authentication protocol");
363 conn->sock->protocol = protocol;
365 /* Register the connection for network input and output. This sets
366 that scheduler will listen for incoming packets for this connection
367 and sets that outgoing packets may be sent to this connection as well.
368 However, this doesn't set the scheduler for outgoing traffic, it will
369 be set separately by calling SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT,
370 later when outgoing data is available. */
371 context = (void *)client;
372 SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd);
374 /* Execute the protocol */
375 silc_protocol_execute(protocol, client->schedule, 0, 0);
379 /* Start of the connection to the remote server. This is called after
380 succesful TCP/IP connection has been established to the remote host. */
382 SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
384 SilcClientInternalConnectContext *ctx =
385 (SilcClientInternalConnectContext *)context;
386 SilcClient client = ctx->client;
387 SilcClientConnection conn = ctx->conn;
388 int opt, opt_len = sizeof(opt);
390 SILC_LOG_DEBUG(("Start"));
392 /* Check the socket status as it might be in error */
393 silc_net_get_socket_opt(fd, SOL_SOCKET, SO_ERROR, &opt, &opt_len);
395 if (ctx->tries < 2) {
396 /* Connection failed but lets try again */
397 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
398 "Could not connect to server %s: %s",
399 ctx->host, strerror(opt));
400 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
401 "Connecting to port %d of server %s resumed",
402 ctx->port, ctx->host);
404 /* Unregister old connection try */
405 silc_schedule_unset_listen_fd(client->schedule, fd);
406 silc_net_close_connection(fd);
407 silc_schedule_task_del(client->schedule, ctx->task);
410 silc_client_connect_to_server_internal(ctx);
413 /* Connection failed and we won't try anymore */
414 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
415 "Could not connect to server %s: %s",
416 ctx->host, strerror(opt));
417 silc_schedule_unset_listen_fd(client->schedule, fd);
418 silc_net_close_connection(fd);
419 silc_schedule_task_del(client->schedule, ctx->task);
422 /* Notify application of failure */
423 client->ops->connect(client, conn, FALSE);
424 silc_client_del_connection(client, conn);
429 silc_schedule_unset_listen_fd(client->schedule, fd);
430 silc_schedule_task_del(client->schedule, ctx->task);
433 if (!silc_client_start_key_exchange(client, conn, fd)) {
434 silc_net_close_connection(fd);
435 client->ops->connect(client, conn, FALSE);
439 /* Second part of the connecting to the server. This executed
440 authentication protocol. */
442 SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
444 SilcProtocol protocol = (SilcProtocol)context;
445 SilcClientKEInternalContext *ctx =
446 (SilcClientKEInternalContext *)protocol->context;
447 SilcClient client = (SilcClient)ctx->client;
448 SilcSocketConnection sock = NULL;
449 SilcClientConnAuthInternalContext *proto_ctx;
451 SILC_LOG_DEBUG(("Start"));
453 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
454 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
455 /* Error occured during protocol */
456 SILC_LOG_DEBUG(("Error during KE protocol"));
457 silc_protocol_free(protocol);
458 silc_ske_free_key_material(ctx->keymat);
460 silc_ske_free(ctx->ske);
462 silc_free(ctx->dest_id);
463 ctx->sock->protocol = NULL;
464 silc_socket_free(ctx->sock);
466 /* Notify application of failure */
467 client->ops->connect(client, ctx->sock->user_data, FALSE);
472 /* We now have the key material as the result of the key exchange
473 protocol. Take the key material into use. Free the raw key material
474 as soon as we've set them into use. */
475 silc_client_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
476 ctx->ske->prop->cipher,
477 ctx->ske->prop->pkcs,
478 ctx->ske->prop->hash,
479 ctx->ske->prop->hmac,
480 ctx->ske->prop->group);
481 silc_ske_free_key_material(ctx->keymat);
483 /* Allocate internal context for the authentication protocol. This
484 is sent as context for the protocol. */
485 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
486 proto_ctx->client = (void *)client;
487 proto_ctx->sock = sock = ctx->sock;
488 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
489 proto_ctx->dest_id_type = ctx->dest_id_type;
490 proto_ctx->dest_id = ctx->dest_id;
492 /* Free old protocol as it is finished now */
493 silc_protocol_free(protocol);
495 silc_packet_context_free(ctx->packet);
497 sock->protocol = NULL;
499 /* Resolve the authentication method to be used in this connection. The
500 completion callback is called after the application has resolved
501 the authentication method. */
502 client->ops->get_auth_method(client, sock->user_data, sock->hostname,
503 sock->port, silc_client_resolve_auth_method,
507 /* Authentication method resolving callback. Application calls this function
508 after we've called the client->ops->get_auth_method client operation
509 to resolve the authentication method. We will continue the executiong
510 of the protocol in this function. */
512 void silc_client_resolve_auth_method(bool success,
513 SilcProtocolAuthMeth auth_meth,
514 const unsigned char *auth_data,
515 uint32 auth_data_len, void *context)
517 SilcClientConnAuthInternalContext *proto_ctx =
518 (SilcClientConnAuthInternalContext *)context;
519 SilcClient client = (SilcClient)proto_ctx->client;
522 auth_meth = SILC_AUTH_NONE;
524 proto_ctx->auth_meth = auth_meth;
526 if (auth_data && auth_data_len) {
527 proto_ctx->auth_data = silc_calloc(auth_data_len, sizeof(*auth_data));
528 memcpy(proto_ctx->auth_data, auth_data, auth_data_len);
529 proto_ctx->auth_data_len = auth_data_len;
532 /* Allocate the authenteication protocol and execute it. */
533 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
534 &proto_ctx->sock->protocol, (void *)proto_ctx,
535 silc_client_connect_to_server_final);
537 /* Execute the protocol */
538 silc_protocol_execute(proto_ctx->sock->protocol, client->schedule, 0, 0);
541 /* Finalizes the connection to the remote SILC server. This is called
542 after authentication protocol has been completed. This send our
543 user information to the server to receive our client ID from
546 SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
548 SilcProtocol protocol = (SilcProtocol)context;
549 SilcClientConnAuthInternalContext *ctx =
550 (SilcClientConnAuthInternalContext *)protocol->context;
551 SilcClient client = (SilcClient)ctx->client;
552 SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
555 SILC_LOG_DEBUG(("Start"));
557 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
558 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
559 /* Error occured during protocol */
560 SILC_LOG_DEBUG(("Error during authentication protocol"));
561 silc_protocol_free(protocol);
563 silc_free(ctx->auth_data);
565 silc_ske_free(ctx->ske);
567 silc_free(ctx->dest_id);
568 conn->sock->protocol = NULL;
569 silc_socket_free(ctx->sock);
571 /* Notify application of failure */
572 client->ops->connect(client, ctx->sock->user_data, FALSE);
577 /* Send NEW_CLIENT packet to the server. We will become registered
578 to the SILC network after sending this packet and we will receive
579 client ID from the server. */
580 packet = silc_buffer_alloc(2 + 2 + strlen(client->username) +
581 strlen(client->realname));
582 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
583 silc_buffer_format(packet,
584 SILC_STR_UI_SHORT(strlen(client->username)),
585 SILC_STR_UI_XNSTRING(client->username,
586 strlen(client->username)),
587 SILC_STR_UI_SHORT(strlen(client->realname)),
588 SILC_STR_UI_XNSTRING(client->realname,
589 strlen(client->realname)),
592 /* Send the packet */
593 silc_client_packet_send(client, ctx->sock, SILC_PACKET_NEW_CLIENT,
595 packet->data, packet->len, TRUE);
596 silc_buffer_free(packet);
598 /* Save remote ID. */
599 conn->remote_id = ctx->dest_id;
600 conn->remote_id_data = silc_id_id2str(ctx->dest_id, SILC_ID_SERVER);
601 conn->remote_id_data_len = silc_id_get_len(ctx->dest_id, SILC_ID_SERVER);
603 /* Register re-key timeout */
604 conn->rekey->timeout = client->params->rekey_secs;
605 conn->rekey->context = (void *)client;
606 silc_schedule_task_add(client->schedule, conn->sock->sock,
607 silc_client_rekey_callback,
608 (void *)conn->sock, conn->rekey->timeout, 0,
609 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
611 silc_protocol_free(protocol);
613 silc_free(ctx->auth_data);
615 silc_ske_free(ctx->ske);
616 silc_socket_free(ctx->sock);
618 conn->sock->protocol = NULL;
621 /* Internal routine that sends packet or marks packet to be sent. This
622 is used directly only in special cases. Normal cases should use
623 silc_server_packet_send. Returns < 0 on error. */
625 int silc_client_packet_send_real(SilcClient client,
626 SilcSocketConnection sock,
632 /* If rekey protocol is active we must assure that all packets are
633 sent through packet queue. */
634 if (flush == FALSE && SILC_CLIENT_IS_REKEY(sock))
637 /* Send the packet */
638 ret = silc_packet_send(sock, force_send);
642 /* Mark that there is some outgoing data available for this connection.
643 This call sets the connection both for input and output (the input
644 is set always and this call keeps the input setting, actually).
645 Actual data sending is performed by silc_client_packet_process. */
646 SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(client->schedule, sock->sock);
648 /* Mark to socket that data is pending in outgoing buffer. This flag
649 is needed if new data is added to the buffer before the earlier
650 put data is sent to the network. */
651 SILC_SET_OUTBUF_PENDING(sock);
656 /* Packet processing callback. This is used to send and receive packets
657 from network. This is generic task. */
659 SILC_TASK_CALLBACK_GLOBAL(silc_client_packet_process)
661 SilcClient client = (SilcClient)context;
662 SilcSocketConnection sock = NULL;
663 SilcClientConnection conn;
666 SILC_LOG_DEBUG(("Processing packet"));
668 SILC_CLIENT_GET_SOCK(client, fd, sock);
672 conn = (SilcClientConnection)sock->user_data;
675 if (type == SILC_TASK_WRITE) {
676 SILC_LOG_DEBUG(("Writing data to connection"));
678 if (sock->outbuf->data - sock->outbuf->head)
679 silc_buffer_push(sock->outbuf,
680 sock->outbuf->data - sock->outbuf->head);
682 ret = silc_client_packet_send_real(client, sock, TRUE, TRUE);
684 /* If returned -2 could not write to connection now, will do
689 /* The packet has been sent and now it is time to set the connection
690 back to only for input. When there is again some outgoing data
691 available for this connection it will be set for output as well.
692 This call clears the output setting and sets it only for input. */
693 SILC_CLIENT_SET_CONNECTION_FOR_INPUT(client->schedule, fd);
694 SILC_UNSET_OUTBUF_PENDING(sock);
696 silc_buffer_clear(sock->outbuf);
700 /* Packet receiving */
701 if (type == SILC_TASK_READ) {
702 SILC_LOG_DEBUG(("Reading data from connection"));
704 /* Read data from network */
705 ret = silc_packet_receive(sock);
711 SILC_LOG_DEBUG(("Read EOF"));
713 /* If connection is disconnecting already we will finally
714 close the connection */
715 if (SILC_IS_DISCONNECTING(sock)) {
716 if (sock == conn->sock)
717 client->ops->disconnect(client, conn);
718 silc_client_close_connection(client, sock, conn);
722 SILC_LOG_DEBUG(("EOF from connection %d", sock->sock));
723 if (sock == conn->sock)
724 client->ops->disconnect(client, conn);
725 silc_client_close_connection(client, sock, conn);
729 /* Process the packet. This will call the parser that will then
730 decrypt and parse the packet. */
731 if (sock->type != SILC_SOCKET_TYPE_UNKNOWN)
732 silc_packet_receive_process(sock, conn->receive_key, conn->hmac_receive,
733 silc_client_packet_parse, client);
735 silc_packet_receive_process(sock, NULL, NULL,
736 silc_client_packet_parse, client);
740 /* Callback function that the silc_packet_decrypt will call to make the
741 decision whether the packet is normal or special packet. We will
742 return TRUE if it is normal and FALSE if it is special */
744 static int silc_client_packet_decrypt_check(SilcPacketType packet_type,
746 SilcPacketContext *packet,
750 /* Packet is normal packet, if:
752 1) packet is private message packet and does not have private key set
753 2) is other packet than channel message packet
755 all other packets are special packets
758 if (packet_type == SILC_PACKET_PRIVATE_MESSAGE &&
759 (buffer->data[2] & SILC_PACKET_FLAG_PRIVMSG_KEY))
762 if (packet_type != SILC_PACKET_CHANNEL_MESSAGE)
768 /* Parses whole packet, received earlier. */
770 SILC_TASK_CALLBACK(silc_client_packet_parse_real)
772 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
773 SilcClient client = (SilcClient)parse_ctx->context;
774 SilcPacketContext *packet = parse_ctx->packet;
775 SilcBuffer buffer = packet->buffer;
776 SilcSocketConnection sock = parse_ctx->sock;
777 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
780 SILC_LOG_DEBUG(("Start"));
782 /* Decrypt the received packet */
783 if (sock->type != SILC_SOCKET_TYPE_UNKNOWN)
784 ret = silc_packet_decrypt(conn->receive_key, conn->hmac_receive,
786 silc_client_packet_decrypt_check, parse_ctx);
788 ret = silc_packet_decrypt(NULL, NULL, buffer, packet,
789 silc_client_packet_decrypt_check, parse_ctx);
795 /* Parse the packet. Packet type is returned. */
796 ret = silc_packet_parse(packet);
798 /* Parse the packet header in special way as this is "special"
800 ret = silc_packet_parse_special(packet);
803 if (ret == SILC_PACKET_NONE)
806 /* Parse the incoming packet type */
807 silc_client_packet_parse_type(client, sock, packet);
810 /* silc_buffer_clear(sock->inbuf); */
811 silc_packet_context_free(packet);
812 silc_free(parse_ctx);
815 /* Parser callback called by silc_packet_receive_process. Thie merely
816 registers timeout that will handle the actual parsing when appropriate. */
818 void silc_client_packet_parse(SilcPacketParserContext *parser_context)
820 SilcClient client = (SilcClient)parser_context->context;
822 /* Parse the packet */
823 silc_schedule_task_add(client->schedule, parser_context->sock->sock,
824 silc_client_packet_parse_real,
825 (void *)parser_context, 0, 1,
827 SILC_TASK_PRI_NORMAL);
830 /* Parses the packet type and calls what ever routines the packet type
831 requires. This is done for all incoming packets. */
833 void silc_client_packet_parse_type(SilcClient client,
834 SilcSocketConnection sock,
835 SilcPacketContext *packet)
837 SilcBuffer buffer = packet->buffer;
838 SilcPacketType type = packet->type;
840 SILC_LOG_DEBUG(("Parsing packet type %d", type));
842 /* Parse the packet type */
844 case SILC_PACKET_DISCONNECT:
845 silc_client_disconnected_by_server(client, sock, buffer);
847 case SILC_PACKET_SUCCESS:
849 * Success received for something. For now we can have only
850 * one protocol for connection executing at once hence this
851 * success message is for whatever protocol is executing currently.
854 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
856 case SILC_PACKET_FAILURE:
858 * Failure received for some protocol. Set the protocol state to
859 * error and call the protocol callback. This fill cause error on
860 * protocol and it will call the final callback.
862 silc_client_process_failure(client, sock, packet);
864 case SILC_PACKET_REJECT:
867 case SILC_PACKET_NOTIFY:
869 * Received notify message
871 silc_client_notify_by_server(client, sock, packet);
874 case SILC_PACKET_ERROR:
876 * Received error message
878 silc_client_error_by_server(client, sock, buffer);
881 case SILC_PACKET_CHANNEL_MESSAGE:
883 * Received message to (from, actually) a channel
885 silc_client_channel_message(client, sock, packet);
887 case SILC_PACKET_CHANNEL_KEY:
889 * Received key for a channel. By receiving this key the client will be
890 * able to talk to the channel it has just joined. This can also be
891 * a new key for existing channel as keys expire peridiocally.
893 silc_client_receive_channel_key(client, sock, buffer);
896 case SILC_PACKET_PRIVATE_MESSAGE:
898 * Received private message
900 silc_client_private_message(client, sock, packet);
902 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
904 * Received private message key
908 case SILC_PACKET_COMMAND_REPLY:
910 * Recived reply for a command
912 silc_client_command_reply_process(client, sock, packet);
915 case SILC_PACKET_KEY_EXCHANGE:
916 if (sock->protocol && sock->protocol->protocol &&
917 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE) {
918 SilcClientKEInternalContext *proto_ctx =
919 (SilcClientKEInternalContext *)sock->protocol->context;
921 proto_ctx->packet = silc_packet_context_dup(packet);
922 proto_ctx->dest_id_type = packet->src_id_type;
923 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
924 packet->src_id_type);
925 if (!proto_ctx->dest_id)
928 /* Let the protocol handle the packet */
929 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
931 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
932 "protocol active, packet dropped."));
936 case SILC_PACKET_KEY_EXCHANGE_1:
937 if (sock->protocol && sock->protocol->protocol &&
938 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
939 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)) {
941 if (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
942 SilcClientRekeyInternalContext *proto_ctx =
943 (SilcClientRekeyInternalContext *)sock->protocol->context;
945 if (proto_ctx->packet)
946 silc_packet_context_free(proto_ctx->packet);
948 proto_ctx->packet = silc_packet_context_dup(packet);
950 /* Let the protocol handle the packet */
951 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
953 SilcClientKEInternalContext *proto_ctx =
954 (SilcClientKEInternalContext *)sock->protocol->context;
956 if (proto_ctx->packet)
957 silc_packet_context_free(proto_ctx->packet);
959 proto_ctx->packet = silc_packet_context_dup(packet);
960 proto_ctx->dest_id_type = packet->src_id_type;
961 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
962 packet->src_id_type);
963 if (!proto_ctx->dest_id)
966 /* Let the protocol handle the packet */
967 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
970 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
971 "protocol active, packet dropped."));
974 case SILC_PACKET_KEY_EXCHANGE_2:
975 if (sock->protocol && sock->protocol->protocol &&
976 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
977 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)) {
979 if (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
980 SilcClientRekeyInternalContext *proto_ctx =
981 (SilcClientRekeyInternalContext *)sock->protocol->context;
983 if (proto_ctx->packet)
984 silc_packet_context_free(proto_ctx->packet);
986 proto_ctx->packet = silc_packet_context_dup(packet);
988 /* Let the protocol handle the packet */
989 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
991 SilcClientKEInternalContext *proto_ctx =
992 (SilcClientKEInternalContext *)sock->protocol->context;
994 if (proto_ctx->packet)
995 silc_packet_context_free(proto_ctx->packet);
997 proto_ctx->packet = silc_packet_context_dup(packet);
998 proto_ctx->dest_id_type = packet->src_id_type;
999 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1000 packet->src_id_type);
1001 if (!proto_ctx->dest_id)
1004 /* Let the protocol handle the packet */
1005 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1008 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1009 "protocol active, packet dropped."));
1013 case SILC_PACKET_NEW_ID:
1016 * Received new ID from server. This packet is received at
1017 * the connection to the server. New ID is also received when
1018 * user changes nickname but in that case the new ID is received
1019 * as command reply and not as this packet type.
1023 idp = silc_id_payload_parse(buffer);
1026 if (silc_id_payload_get_type(idp) != SILC_ID_CLIENT)
1029 silc_client_receive_new_id(client, sock, idp);
1030 silc_id_payload_free(idp);
1034 case SILC_PACKET_HEARTBEAT:
1036 * Received heartbeat packet
1038 SILC_LOG_DEBUG(("Heartbeat packet"));
1041 case SILC_PACKET_KEY_AGREEMENT:
1043 * Received key agreement packet
1045 SILC_LOG_DEBUG(("Key agreement packet"));
1046 silc_client_key_agreement(client, sock, packet);
1049 case SILC_PACKET_REKEY:
1050 SILC_LOG_DEBUG(("Re-key packet"));
1051 /* We ignore this for now */
1054 case SILC_PACKET_REKEY_DONE:
1055 SILC_LOG_DEBUG(("Re-key done packet"));
1057 if (sock->protocol && sock->protocol->protocol &&
1058 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
1060 SilcClientRekeyInternalContext *proto_ctx =
1061 (SilcClientRekeyInternalContext *)sock->protocol->context;
1063 if (proto_ctx->packet)
1064 silc_packet_context_free(proto_ctx->packet);
1066 proto_ctx->packet = silc_packet_context_dup(packet);
1068 /* Let the protocol handle the packet */
1069 if (proto_ctx->responder == FALSE)
1070 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1072 /* Let the protocol handle the packet */
1073 silc_protocol_execute(sock->protocol, client->schedule,
1076 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
1077 "protocol active, packet dropped."));
1081 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1083 * Reveived reply to our connection authentication method request
1084 * packet. This is used to resolve the authentication method for the
1085 * current session from the server if the client does not know it.
1087 silc_client_connection_auth_request(client, sock, packet);
1091 SILC_LOG_DEBUG(("Incorrect packet type %d, packet dropped", type));
1096 /* Sends packet. This doesn't actually send the packet instead it assembles
1097 it and marks it to be sent. However, if force_send is TRUE the packet
1098 is sent immediately. if dst_id, cipher and hmac are NULL those parameters
1099 will be derived from sock argument. Otherwise the valid arguments sent
1102 void silc_client_packet_send(SilcClient client,
1103 SilcSocketConnection sock,
1104 SilcPacketType type,
1106 SilcIdType dst_id_type,
1109 unsigned char *data,
1113 SilcPacketContext packetdata;
1118 SILC_LOG_DEBUG(("Sending packet, type %d", type));
1120 /* Get data used in the packet sending, keys and stuff */
1121 if ((!cipher || !hmac || !dst_id) && sock->user_data) {
1122 if (!cipher && ((SilcClientConnection)sock->user_data)->send_key)
1123 cipher = ((SilcClientConnection)sock->user_data)->send_key;
1125 if (!hmac && ((SilcClientConnection)sock->user_data)->hmac_send)
1126 hmac = ((SilcClientConnection)sock->user_data)->hmac_send;
1128 if (!dst_id && ((SilcClientConnection)sock->user_data)->remote_id) {
1129 dst_id = ((SilcClientConnection)sock->user_data)->remote_id;
1130 dst_id_type = SILC_ID_SERVER;
1134 /* Set the packet context pointers */
1135 packetdata.flags = 0;
1136 packetdata.type = type;
1137 if (sock->user_data &&
1138 ((SilcClientConnection)sock->user_data)->local_id_data) {
1139 packetdata.src_id = ((SilcClientConnection)sock->user_data)->local_id_data;
1140 packetdata.src_id_len =
1141 silc_id_get_len(((SilcClientConnection)sock->user_data)->local_id,
1144 packetdata.src_id = silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char));
1145 packetdata.src_id_len = SILC_ID_CLIENT_LEN;
1147 packetdata.src_id_type = SILC_ID_CLIENT;
1149 packetdata.dst_id = silc_id_id2str(dst_id, dst_id_type);
1150 packetdata.dst_id_len = silc_id_get_len(dst_id, dst_id_type);
1151 packetdata.dst_id_type = dst_id_type;
1153 packetdata.dst_id = NULL;
1154 packetdata.dst_id_len = 0;
1155 packetdata.dst_id_type = SILC_ID_NONE;
1157 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1158 packetdata.src_id_len + packetdata.dst_id_len;
1159 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1161 /* Prepare outgoing data buffer for packet sending */
1162 silc_packet_send_prepare(sock,
1163 SILC_PACKET_HEADER_LEN +
1164 packetdata.src_id_len +
1165 packetdata.dst_id_len,
1169 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
1171 packetdata.buffer = sock->outbuf;
1173 /* Put the data to the buffer */
1174 if (data && data_len)
1175 silc_buffer_put(sock->outbuf, data, data_len);
1177 /* Create the outgoing packet */
1178 silc_packet_assemble(&packetdata);
1180 /* Encrypt the packet */
1182 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
1184 SILC_LOG_HEXDUMP(("Packet, len %d", sock->outbuf->len),
1185 sock->outbuf->data, sock->outbuf->len);
1187 /* Now actually send the packet */
1188 silc_client_packet_send_real(client, sock, force_send, FALSE);
1191 /* Closes connection to remote end. Free's all allocated data except
1192 for some information such as nickname etc. that are valid at all time.
1193 If the `sock' is NULL then the conn->sock will be used. If `sock' is
1194 provided it will be checked whether the sock and `conn->sock' are the
1195 same (they can be different, ie. a socket can use `conn' as its
1196 connection but `conn->sock' might be actually a different connection
1197 than the `sock'). */
1199 void silc_client_close_connection(SilcClient client,
1200 SilcSocketConnection sock,
1201 SilcClientConnection conn)
1205 if (!sock || (sock && conn->sock == sock))
1210 /* We won't listen for this connection anymore */
1211 silc_schedule_unset_listen_fd(client->schedule, sock->sock);
1213 /* Unregister all tasks */
1214 silc_schedule_task_del_by_fd(client->schedule, sock->sock);
1215 silc_schedule_task_del_by_fd(client->schedule, sock->sock);
1217 /* Close the actual connection */
1218 silc_net_close_connection(sock->sock);
1220 /* Cancel any active protocol */
1221 if (sock->protocol) {
1222 if (sock->protocol->protocol->type ==
1223 SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
1224 sock->protocol->protocol->type ==
1225 SILC_PROTOCOL_CLIENT_CONNECTION_AUTH) {
1226 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
1227 silc_protocol_execute_final(sock->protocol, client->schedule);
1228 /* The application will recall this function with these protocols
1229 (the ops->connect client operation). */
1232 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
1233 silc_protocol_execute_final(sock->protocol, client->schedule);
1234 sock->protocol = NULL;
1238 /* Free everything */
1239 if (del && sock->user_data) {
1240 /* Free all cache entries */
1241 SilcIDCacheList list;
1242 SilcIDCacheEntry entry;
1245 if (silc_idcache_get_all(conn->client_cache, &list)) {
1246 ret = silc_idcache_list_first(list, &entry);
1248 silc_client_del_client(client, conn, entry->context);
1249 ret = silc_idcache_list_next(list, &entry);
1251 silc_idcache_list_free(list);
1254 if (silc_idcache_get_all(conn->channel_cache, &list)) {
1255 ret = silc_idcache_list_first(list, &entry);
1257 silc_client_del_channel(client, conn, entry->context);
1258 ret = silc_idcache_list_next(list, &entry);
1260 silc_idcache_list_free(list);
1263 if (silc_idcache_get_all(conn->server_cache, &list)) {
1264 ret = silc_idcache_list_first(list, &entry);
1266 silc_client_del_server(client, conn, entry->context);
1267 ret = silc_idcache_list_next(list, &entry);
1269 silc_idcache_list_free(list);
1272 /* Clear ID caches */
1273 if (conn->client_cache)
1274 silc_idcache_del_all(conn->client_cache);
1275 if (conn->channel_cache)
1276 silc_idcache_del_all(conn->channel_cache);
1277 if (conn->server_cache)
1278 silc_idcache_del_all(conn->server_cache);
1280 /* Free data (my ID is freed in above silc_client_del_client) */
1281 if (conn->remote_host)
1282 silc_free(conn->remote_host);
1283 if (conn->local_id_data)
1284 silc_free(conn->local_id_data);
1286 silc_cipher_free(conn->send_key);
1287 if (conn->receive_key)
1288 silc_cipher_free(conn->receive_key);
1289 if (conn->hmac_send) /* conn->hmac_receive is same */
1290 silc_hmac_free(conn->hmac_send);
1291 if (conn->pending_commands)
1292 silc_dlist_uninit(conn->pending_commands);
1294 silc_free(conn->rekey);
1296 memset(conn, 0, sizeof(*conn));
1297 silc_client_del_connection(client, conn);
1300 silc_socket_free(sock);
1303 /* Called when we receive disconnection packet from server. This
1304 closes our end properly and displays the reason of the disconnection
1307 void silc_client_disconnected_by_server(SilcClient client,
1308 SilcSocketConnection sock,
1313 SILC_LOG_DEBUG(("Server disconnected us, sock %d", sock->sock));
1315 msg = silc_calloc(message->len + 1, sizeof(char));
1316 memcpy(msg, message->data, message->len);
1317 client->ops->say(client, sock->user_data, SILC_CLIENT_MESSAGE_AUDIT, msg);
1320 SILC_SET_DISCONNECTED(sock);
1321 silc_client_close_connection(client, sock, sock->user_data);
1324 /* Received error message from server. Display it on the screen.
1325 We don't take any action what so ever of the error message. */
1327 void silc_client_error_by_server(SilcClient client,
1328 SilcSocketConnection sock,
1333 msg = silc_calloc(message->len + 1, sizeof(char));
1334 memcpy(msg, message->data, message->len);
1335 client->ops->say(client, sock->user_data, SILC_CLIENT_MESSAGE_AUDIT, msg);
1339 /* Processes the received new Client ID from server. Old Client ID is
1340 deleted from cache and new one is added. */
1342 void silc_client_receive_new_id(SilcClient client,
1343 SilcSocketConnection sock,
1346 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1347 int connecting = FALSE;
1350 if (!conn->local_entry)
1353 /* Delete old ID from ID cache */
1354 if (conn->local_id) {
1355 silc_idcache_del_by_context(conn->client_cache, conn->local_entry);
1356 silc_free(conn->local_id);
1359 /* Save the new ID */
1361 if (conn->local_id_data)
1362 silc_free(conn->local_id_data);
1364 conn->local_id = silc_id_payload_get_id(idp);
1365 conn->local_id_data = silc_id_payload_get_data(idp);
1366 conn->local_id_data_len = silc_id_payload_get_len(idp);;
1368 if (!conn->local_entry)
1369 conn->local_entry = silc_calloc(1, sizeof(*conn->local_entry));
1371 conn->local_entry->nickname = conn->nickname;
1372 if (!conn->local_entry->username) {
1373 conn->local_entry->username =
1374 silc_calloc(strlen(client->username) + strlen(client->hostname) + 1,
1375 sizeof(conn->local_entry->username));
1376 sprintf(conn->local_entry->username, "%s@%s", client->username,
1379 conn->local_entry->server = strdup(conn->remote_host);
1380 conn->local_entry->id = conn->local_id;
1382 /* Put it to the ID cache */
1383 silc_idcache_add(conn->client_cache, conn->nickname, conn->local_id,
1384 (void *)conn->local_entry, FALSE);
1386 /* Issue INFO command to fetch the real server name and server information
1388 sidp = silc_id_payload_encode(conn->remote_id, SILC_ID_SERVER);
1389 silc_client_send_command(client, conn, SILC_COMMAND_INFO,
1390 ++conn->cmd_ident, 1, 2, sidp->data, sidp->len);
1391 silc_buffer_free(sidp);
1393 /* Notify application of successful connection. We do it here now that
1394 we've received the Client ID and are allowed to send traffic. */
1396 client->ops->connect(client, conn, TRUE);
1399 /* Processed received Channel ID for a channel. This is called when client
1400 joins to channel and server replies with channel ID. The ID is cached.
1401 Returns the created channel entry. This is also called when received
1402 channel ID in for example USERS command reply that we do not have. */
1404 SilcChannelEntry silc_client_new_channel_id(SilcClient client,
1405 SilcSocketConnection sock,
1410 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1411 SilcChannelEntry channel;
1413 SILC_LOG_DEBUG(("New channel ID"));
1415 channel = silc_calloc(1, sizeof(*channel));
1416 channel->channel_name = channel_name;
1417 channel->id = silc_id_payload_get_id(idp);
1418 channel->mode = mode;
1419 silc_list_init(channel->clients, struct SilcChannelUserStruct, next);
1421 /* Put it to the ID cache */
1422 silc_idcache_add(conn->channel_cache, channel->channel_name,
1423 (void *)channel->id, (void *)channel, FALSE);
1428 /* Removes a client entry from all channel it has joined. This really is
1429 a performance killer (client_entry should have pointers to channel
1432 void silc_client_remove_from_channels(SilcClient client,
1433 SilcClientConnection conn,
1434 SilcClientEntry client_entry)
1436 SilcIDCacheEntry id_cache;
1437 SilcIDCacheList list;
1438 SilcChannelEntry channel;
1439 SilcChannelUser chu;
1441 if (!silc_idcache_get_all(conn->channel_cache, &list))
1444 silc_idcache_list_first(list, &id_cache);
1445 channel = (SilcChannelEntry)id_cache->context;
1449 /* Remove client from channel */
1450 silc_list_start(channel->clients);
1451 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1452 if (chu->client == client_entry) {
1453 silc_list_del(channel->clients, chu);
1459 if (!silc_idcache_list_next(list, &id_cache))
1462 channel = (SilcChannelEntry)id_cache->context;
1465 silc_idcache_list_free(list);
1468 /* Replaces `old' client entries from all channels to `new' client entry.
1469 This can be called for example when nickname changes and old ID entry
1470 is replaced from ID cache with the new one. If the old ID entry is only
1471 updated, then this fucntion needs not to be called. */
1473 void silc_client_replace_from_channels(SilcClient client,
1474 SilcClientConnection conn,
1475 SilcClientEntry old,
1476 SilcClientEntry new)
1478 SilcIDCacheEntry id_cache;
1479 SilcIDCacheList list;
1480 SilcChannelEntry channel;
1481 SilcChannelUser chu;
1483 if (!silc_idcache_get_all(conn->channel_cache, &list))
1486 silc_idcache_list_first(list, &id_cache);
1487 channel = (SilcChannelEntry)id_cache->context;
1491 /* Replace client entry */
1492 silc_list_start(channel->clients);
1493 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1494 if (chu->client == old) {
1500 if (!silc_idcache_list_next(list, &id_cache))
1503 channel = (SilcChannelEntry)id_cache->context;
1506 silc_idcache_list_free(list);
1509 /* Registers failure timeout to process the received failure packet
1512 void silc_client_process_failure(SilcClient client,
1513 SilcSocketConnection sock,
1514 SilcPacketContext *packet)
1518 if (sock->protocol) {
1519 if (packet->buffer->len >= 4)
1520 SILC_GET32_MSB(failure, packet->buffer->data);
1522 /* Notify application */
1523 client->ops->failure(client, sock->user_data, sock->protocol,
1528 /* A timeout callback for the re-key. We will be the initiator of the
1531 SILC_TASK_CALLBACK(silc_client_rekey_callback)
1533 SilcSocketConnection sock = (SilcSocketConnection)context;
1534 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1535 SilcClient client = (SilcClient)conn->rekey->context;
1536 SilcProtocol protocol;
1537 SilcClientRekeyInternalContext *proto_ctx;
1539 SILC_LOG_DEBUG(("Start"));
1541 /* Allocate internal protocol context. This is sent as context
1543 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1544 proto_ctx->client = (void *)client;
1545 proto_ctx->sock = silc_socket_dup(sock);
1546 proto_ctx->responder = FALSE;
1547 proto_ctx->pfs = conn->rekey->pfs;
1549 /* Perform rekey protocol. Will call the final callback after the
1550 protocol is over. */
1551 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_REKEY,
1552 &protocol, proto_ctx, silc_client_rekey_final);
1553 sock->protocol = protocol;
1555 /* Run the protocol */
1556 silc_protocol_execute(protocol, client->schedule, 0, 0);
1558 /* Re-register re-key timeout */
1559 silc_schedule_task_add(client->schedule, sock->sock,
1560 silc_client_rekey_callback,
1561 context, conn->rekey->timeout, 0,
1562 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1565 /* The final callback for the REKEY protocol. This will actually take the
1566 new key material into use. */
1568 SILC_TASK_CALLBACK(silc_client_rekey_final)
1570 SilcProtocol protocol = (SilcProtocol)context;
1571 SilcClientRekeyInternalContext *ctx =
1572 (SilcClientRekeyInternalContext *)protocol->context;
1573 SilcClient client = (SilcClient)ctx->client;
1574 SilcSocketConnection sock = ctx->sock;
1576 SILC_LOG_DEBUG(("Start"));
1578 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1579 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1580 /* Error occured during protocol */
1581 silc_protocol_cancel(protocol, client->schedule);
1582 silc_protocol_free(protocol);
1583 sock->protocol = NULL;
1585 silc_packet_context_free(ctx->packet);
1587 silc_ske_free(ctx->ske);
1588 silc_socket_free(ctx->sock);
1594 silc_protocol_free(protocol);
1595 sock->protocol = NULL;
1597 silc_packet_context_free(ctx->packet);
1599 silc_ske_free(ctx->ske);
1600 silc_socket_free(ctx->sock);
1604 /* Processes incoming connection authentication method request packet.
1605 It is a reply to our previously sent request. The packet can be used
1606 to resolve the authentication method for the current session if the
1607 client does not know it beforehand. */
1609 void silc_client_connection_auth_request(SilcClient client,
1610 SilcSocketConnection sock,
1611 SilcPacketContext *packet)
1613 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1614 uint16 conn_type, auth_meth;
1617 /* If we haven't send our request then ignore this one. */
1618 if (!conn->connauth)
1621 /* Parse the payload */
1622 ret = silc_buffer_unformat(packet->buffer,
1623 SILC_STR_UI_SHORT(&conn_type),
1624 SILC_STR_UI_SHORT(&auth_meth),
1627 auth_meth = SILC_AUTH_NONE;
1629 /* Call the request callback to notify application for received
1630 authentication method information. */
1631 if (conn->connauth->callback)
1632 (*conn->connauth->callback)(client, conn, auth_meth,
1633 conn->connauth->context);
1635 silc_schedule_task_del(client->schedule, conn->connauth->timeout);
1637 silc_free(conn->connauth);
1638 conn->connauth = NULL;
1641 /* Timeout task callback called if the server does not reply to our
1642 connection authentication method request in the specified time interval. */
1644 SILC_TASK_CALLBACK(silc_client_request_authentication_method_timeout)
1646 SilcClientConnection conn = (SilcClientConnection)context;
1647 SilcClient client = conn->client;
1649 if (!conn->connauth)
1652 /* Call the request callback to notify application */
1653 if (conn->connauth->callback)
1654 (*conn->connauth->callback)(client, conn, SILC_AUTH_NONE,
1655 conn->connauth->context);
1657 silc_free(conn->connauth);
1658 conn->connauth = NULL;
1661 /* This function can be used to request the current authentication method
1662 from the server. This may be called when connecting to the server
1663 and the client library requests the authentication data from the
1664 application. If the application does not know the current authentication
1665 method it can request it from the server using this function.
1666 The `callback' with `context' will be called after the server has
1667 replied back with the current authentication method. */
1670 silc_client_request_authentication_method(SilcClient client,
1671 SilcClientConnection conn,
1672 SilcConnectionAuthRequest callback,
1675 SilcClientConnAuthRequest connauth;
1678 connauth = silc_calloc(1, sizeof(*connauth));
1679 connauth->callback = callback;
1680 connauth->context = context;
1683 silc_free(conn->connauth);
1685 conn->connauth = connauth;
1687 /* Assemble the request packet and send it to the server */
1688 packet = silc_buffer_alloc(4);
1689 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1690 silc_buffer_format(packet,
1691 SILC_STR_UI_SHORT(SILC_SOCKET_TYPE_CLIENT),
1692 SILC_STR_UI_SHORT(SILC_AUTH_NONE),
1694 silc_client_packet_send(client, conn->sock,
1695 SILC_PACKET_CONNECTION_AUTH_REQUEST,
1696 NULL, 0, NULL, NULL,
1697 packet->data, packet->len, FALSE);
1698 silc_buffer_free(packet);
1700 /* Register a timeout in case server does not reply anything back. */
1702 silc_schedule_task_add(client->schedule, conn->sock->sock,
1703 silc_client_request_authentication_method_timeout,
1704 conn, client->params->connauth_request_secs, 0,
1705 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);