5 Author: Pekka Riikonen <priikone@silcnet.org>
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,
35 static void silc_client_packet_parse_type(SilcClient client,
36 SilcSocketConnection sock,
37 SilcPacketContext *packet);
38 void silc_client_resolve_auth_method(bool success,
39 SilcProtocolAuthMeth auth_meth,
40 const unsigned char *auth_data,
41 uint32 auth_data_len, void *context);
43 /* Allocates new client object. This has to be done before client may
44 work. After calling this one must call silc_client_init to initialize
45 the client. The `application' is application specific user data pointer
46 and caller must free it. */
48 SilcClient silc_client_alloc(SilcClientOperations *ops,
49 SilcClientParams *params,
51 const char *silc_version)
53 SilcClient new_client;
55 new_client = silc_calloc(1, sizeof(*new_client));
56 new_client->application = application;
57 new_client->ops = ops;
58 new_client->silc_client_version = strdup(silc_version);
59 new_client->params = silc_calloc(1, sizeof(*new_client->params));
62 memcpy(new_client->params, params, sizeof(*params));
64 if (!new_client->params->task_max)
65 new_client->params->task_max = 200;
67 if (!new_client->params->rekey_secs)
68 new_client->params->rekey_secs = 3600;
70 if (!new_client->params->connauth_request_secs)
71 new_client->params->connauth_request_secs = 2;
74 nickname_format[sizeof(new_client->params->nickname_format) - 1] = 0;
79 /* Frees client object and its internals. */
81 void silc_client_free(SilcClient client)
85 silc_rng_free(client->rng);
87 silc_free(client->silc_client_version);
88 silc_free(client->params);
93 /* Initializes the client. This makes all the necessary steps to make
94 the client ready to be run. One must call silc_client_run to run the
95 client. Returns FALSE if error occured, TRUE otherwise. */
97 int silc_client_init(SilcClient client)
99 SILC_LOG_DEBUG(("Initializing client"));
101 /* Initialize hash functions for client to use */
102 silc_hash_alloc("md5", &client->md5hash);
103 silc_hash_alloc("sha1", &client->sha1hash);
105 /* Initialize none cipher */
106 silc_cipher_alloc("none", &client->none_cipher);
108 /* Initialize random number generator */
109 client->rng = silc_rng_alloc();
110 silc_rng_init(client->rng);
111 silc_rng_global_init(client->rng);
113 /* Register protocols */
114 silc_client_protocols_register();
116 /* Initialize the scheduler */
117 client->schedule = silc_schedule_init(client->params->task_max ?
118 client->params->task_max : 200);
119 if (!client->schedule)
125 /* Stops the client. This is called to stop the client and thus to stop
128 void silc_client_stop(SilcClient client)
130 SILC_LOG_DEBUG(("Stopping client"));
132 silc_schedule_stop(client->schedule);
133 silc_schedule_uninit(client->schedule);
135 silc_client_protocols_unregister();
137 SILC_LOG_DEBUG(("Client stopped"));
140 /* Runs the client. This starts the scheduler from the utility library.
141 When this functions returns the execution of the appliation is over. */
143 void silc_client_run(SilcClient client)
145 SILC_LOG_DEBUG(("Running client"));
147 /* Start the scheduler, the heart of the SILC client. When this returns
148 the program will be terminated. */
149 silc_schedule(client->schedule);
152 static void silc_client_entry_destructor(SilcIDCache cache,
153 SilcIDCacheEntry entry)
155 silc_free(entry->name);
158 /* Allocates and adds new connection to the client. This adds the allocated
159 connection to the connection table and returns a pointer to it. A client
160 can have multiple connections to multiple servers. Every connection must
161 be added to the client using this function. User data `context' may
162 be sent as argument. This function is normally used only if the
163 application performed the connecting outside the library. The library
164 however may use this internally. */
166 SilcClientConnection silc_client_add_connection(SilcClient client,
171 SilcClientConnection conn;
174 conn = silc_calloc(1, sizeof(*conn));
176 /* Initialize ID caches */
177 conn->client_cache = silc_idcache_alloc(0, SILC_ID_CLIENT,
178 silc_client_entry_destructor);
179 conn->channel_cache = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
180 conn->server_cache = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
181 conn->client = client;
182 conn->remote_host = strdup(hostname);
183 conn->remote_port = port;
184 conn->context = context;
185 conn->pending_commands = silc_dlist_init();
187 /* Add the connection to connections table */
188 for (i = 0; i < client->conns_count; i++)
189 if (client->conns && !client->conns[i]) {
190 client->conns[i] = conn;
194 client->conns = silc_realloc(client->conns, sizeof(*client->conns)
195 * (client->conns_count + 1));
196 client->conns[client->conns_count] = conn;
197 client->conns_count++;
202 /* Removes connection from client. Frees all memory. */
204 void silc_client_del_connection(SilcClient client, SilcClientConnection conn)
208 for (i = 0; i < client->conns_count; i++)
209 if (client->conns[i] == conn) {
210 if (conn->pending_commands)
211 silc_dlist_uninit(conn->pending_commands);
213 client->conns[i] = NULL;
217 /* Adds listener socket to the listener sockets table. This function is
218 used to add socket objects that are listeners to the client. This should
219 not be used to add other connection objects. */
221 void silc_client_add_socket(SilcClient client, SilcSocketConnection sock)
225 if (!client->sockets) {
226 client->sockets = silc_calloc(1, sizeof(*client->sockets));
227 client->sockets[0] = silc_socket_dup(sock);
228 client->sockets_count = 1;
232 for (i = 0; i < client->sockets_count; i++) {
233 if (client->sockets[i] == NULL) {
234 client->sockets[i] = silc_socket_dup(sock);
239 client->sockets = silc_realloc(client->sockets, sizeof(*client->sockets) *
240 (client->sockets_count + 1));
241 client->sockets[client->sockets_count] = silc_socket_dup(sock);
242 client->sockets_count++;
245 /* Deletes listener socket from the listener sockets table. */
247 void silc_client_del_socket(SilcClient client, SilcSocketConnection sock)
251 if (!client->sockets)
254 for (i = 0; i < client->sockets_count; i++) {
255 if (client->sockets[i] == sock) {
256 silc_socket_free(sock);
257 client->sockets[i] = NULL;
264 silc_client_connect_to_server_internal(SilcClientInternalConnectContext *ctx)
268 /* XXX In the future we should give up this non-blocking connect all
269 together and use threads instead. */
270 /* Create connection to server asynchronously */
271 sock = silc_net_create_connection_async(NULL, ctx->port, ctx->host);
275 /* Register task that will receive the async connect and will
277 ctx->task = silc_schedule_task_add(ctx->client->schedule, sock,
278 silc_client_connect_to_server_start,
281 SILC_TASK_PRI_NORMAL);
282 silc_schedule_set_listen_fd(ctx->client->schedule, sock, SILC_TASK_WRITE);
289 /* Connects to remote server. This is the main routine used to connect
290 to SILC server. Returns -1 on error and the created socket otherwise.
291 The `context' is user context that is saved into the SilcClientConnection
292 that is created after the connection is created. Note that application
293 may handle the connecting process outside the library. If this is the
294 case then this function is not used at all. When the connecting is
295 done the `connect' client operation is called. */
297 int silc_client_connect_to_server(SilcClient client, int port,
298 char *host, void *context)
300 SilcClientInternalConnectContext *ctx;
301 SilcClientConnection conn;
304 SILC_LOG_DEBUG(("Connecting to port %d of server %s",
307 conn = silc_client_add_connection(client, host, port, context);
309 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
310 "Connecting to port %d of server %s", port, host);
312 /* Allocate internal context for connection process. This is
313 needed as we are doing async connecting. */
314 ctx = silc_calloc(1, sizeof(*ctx));
315 ctx->client = client;
317 ctx->host = strdup(host);
321 /* Do the actual connecting process */
322 sock = silc_client_connect_to_server_internal(ctx);
324 silc_client_del_connection(client, conn);
328 /* Start SILC Key Exchange (SKE) protocol to negotiate shared secret
329 key material between client and server. This function can be called
330 directly if application is performing its own connecting and does not
331 use the connecting provided by this library. This function is normally
332 used only if the application performed the connecting outside the library.
333 The library however may use this internally. */
335 bool silc_client_start_key_exchange(SilcClient client,
336 SilcClientConnection conn,
339 SilcProtocol protocol;
340 SilcClientKEInternalContext *proto_ctx;
343 /* Allocate new socket connection object */
344 silc_socket_alloc(fd, SILC_SOCKET_TYPE_SERVER, (void *)conn, &conn->sock);
346 /* Sometimes when doing quick reconnects the new socket may be same as
347 the old one and there might be pending stuff for the old socket.
348 If new one is same then those pending sutff might cause problems.
349 Make sure they do not do that. */
350 silc_schedule_task_del_by_fd(client->schedule, fd);
352 conn->nickname = strdup(client->username);
353 conn->sock->hostname = conn->remote_host;
354 conn->sock->ip = strdup(conn->remote_host);
355 conn->sock->port = conn->remote_port;
357 /* Allocate internal Key Exchange context. This is sent to the
358 protocol as context. */
359 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
360 proto_ctx->client = (void *)client;
361 proto_ctx->sock = silc_socket_dup(conn->sock);
362 proto_ctx->rng = client->rng;
363 proto_ctx->responder = FALSE;
364 proto_ctx->send_packet = silc_client_protocol_ke_send_packet;
365 proto_ctx->verify = silc_client_protocol_ke_verify_key;
367 /* Perform key exchange protocol. silc_client_connect_to_server_final
368 will be called after the protocol is finished. */
369 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
370 &protocol, (void *)proto_ctx,
371 silc_client_connect_to_server_second);
373 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
374 "Error: Could not start key exchange protocol");
377 conn->sock->protocol = protocol;
379 /* Register the connection for network input and output. This sets
380 that scheduler will listen for incoming packets for this connection
381 and sets that outgoing packets may be sent to this connection as well.
382 However, this doesn't set the scheduler for outgoing traffic, it will
383 be set separately by calling SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT,
384 later when outgoing data is available. */
385 context = (void *)client;
386 SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd);
388 /* Execute the protocol */
389 silc_protocol_execute(protocol, client->schedule, 0, 0);
393 /* Start of the connection to the remote server. This is called after
394 succesful TCP/IP connection has been established to the remote host. */
396 SILC_TASK_CALLBACK(silc_client_connect_to_server_start)
398 SilcClientInternalConnectContext *ctx =
399 (SilcClientInternalConnectContext *)context;
400 SilcClient client = ctx->client;
401 SilcClientConnection conn = ctx->conn;
402 int opt, opt_len = sizeof(opt);
404 SILC_LOG_DEBUG(("Start"));
406 /* Check the socket status as it might be in error */
407 silc_net_get_socket_opt(fd, SOL_SOCKET, SO_ERROR, &opt, &opt_len);
409 if (ctx->tries < 2) {
410 /* Connection failed but lets try again */
411 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
412 "Could not connect to server %s: %s",
413 ctx->host, strerror(opt));
414 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
415 "Connecting to port %d of server %s resumed",
416 ctx->port, ctx->host);
418 /* Unregister old connection try */
419 silc_schedule_unset_listen_fd(client->schedule, fd);
420 silc_net_close_connection(fd);
421 silc_schedule_task_del(client->schedule, ctx->task);
424 silc_client_connect_to_server_internal(ctx);
427 /* Connection failed and we won't try anymore */
428 client->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
429 "Could not connect to server %s: %s",
430 ctx->host, strerror(opt));
431 silc_schedule_unset_listen_fd(client->schedule, fd);
432 silc_net_close_connection(fd);
433 silc_schedule_task_del(client->schedule, ctx->task);
436 /* Notify application of failure */
437 client->ops->connect(client, conn, FALSE);
438 silc_client_del_connection(client, conn);
443 silc_schedule_unset_listen_fd(client->schedule, fd);
444 silc_schedule_task_del(client->schedule, ctx->task);
447 if (!silc_client_start_key_exchange(client, conn, fd)) {
448 silc_net_close_connection(fd);
449 client->ops->connect(client, conn, FALSE);
453 /* Second part of the connecting to the server. This executed
454 authentication protocol. */
456 SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
458 SilcProtocol protocol = (SilcProtocol)context;
459 SilcClientKEInternalContext *ctx =
460 (SilcClientKEInternalContext *)protocol->context;
461 SilcClient client = (SilcClient)ctx->client;
462 SilcSocketConnection sock = NULL;
463 SilcClientConnAuthInternalContext *proto_ctx;
465 SILC_LOG_DEBUG(("Start"));
467 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
468 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
469 /* Error occured during protocol */
470 SILC_LOG_DEBUG(("Error during KE protocol"));
471 silc_protocol_free(protocol);
472 silc_ske_free_key_material(ctx->keymat);
474 silc_ske_free(ctx->ske);
476 silc_free(ctx->dest_id);
477 ctx->sock->protocol = NULL;
478 silc_socket_free(ctx->sock);
480 /* Notify application of failure */
481 client->ops->connect(client, ctx->sock->user_data, FALSE);
486 /* We now have the key material as the result of the key exchange
487 protocol. Take the key material into use. Free the raw key material
488 as soon as we've set them into use. */
489 silc_client_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
490 ctx->ske->prop->cipher,
491 ctx->ske->prop->pkcs,
492 ctx->ske->prop->hash,
493 ctx->ske->prop->hmac,
494 ctx->ske->prop->group);
495 silc_ske_free_key_material(ctx->keymat);
497 /* Allocate internal context for the authentication protocol. This
498 is sent as context for the protocol. */
499 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
500 proto_ctx->client = (void *)client;
501 proto_ctx->sock = sock = ctx->sock;
502 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
503 proto_ctx->dest_id_type = ctx->dest_id_type;
504 proto_ctx->dest_id = ctx->dest_id;
506 /* Free old protocol as it is finished now */
507 silc_protocol_free(protocol);
509 silc_packet_context_free(ctx->packet);
511 sock->protocol = NULL;
513 /* Resolve the authentication method to be used in this connection. The
514 completion callback is called after the application has resolved
515 the authentication method. */
516 client->ops->get_auth_method(client, sock->user_data, sock->hostname,
517 sock->port, silc_client_resolve_auth_method,
521 /* Authentication method resolving callback. Application calls this function
522 after we've called the client->ops->get_auth_method client operation
523 to resolve the authentication method. We will continue the executiong
524 of the protocol in this function. */
526 void silc_client_resolve_auth_method(bool success,
527 SilcProtocolAuthMeth auth_meth,
528 const unsigned char *auth_data,
529 uint32 auth_data_len, void *context)
531 SilcClientConnAuthInternalContext *proto_ctx =
532 (SilcClientConnAuthInternalContext *)context;
533 SilcClient client = (SilcClient)proto_ctx->client;
536 auth_meth = SILC_AUTH_NONE;
538 proto_ctx->auth_meth = auth_meth;
540 if (auth_data && auth_data_len) {
541 proto_ctx->auth_data = silc_calloc(auth_data_len, sizeof(*auth_data));
542 memcpy(proto_ctx->auth_data, auth_data, auth_data_len);
543 proto_ctx->auth_data_len = auth_data_len;
546 /* Allocate the authenteication protocol and execute it. */
547 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
548 &proto_ctx->sock->protocol, (void *)proto_ctx,
549 silc_client_connect_to_server_final);
551 /* Execute the protocol */
552 silc_protocol_execute(proto_ctx->sock->protocol, client->schedule, 0, 0);
555 /* Finalizes the connection to the remote SILC server. This is called
556 after authentication protocol has been completed. This send our
557 user information to the server to receive our client ID from
560 SILC_TASK_CALLBACK(silc_client_connect_to_server_final)
562 SilcProtocol protocol = (SilcProtocol)context;
563 SilcClientConnAuthInternalContext *ctx =
564 (SilcClientConnAuthInternalContext *)protocol->context;
565 SilcClient client = (SilcClient)ctx->client;
566 SilcClientConnection conn = (SilcClientConnection)ctx->sock->user_data;
569 SILC_LOG_DEBUG(("Start"));
571 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
572 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
573 /* Error occured during protocol */
574 SILC_LOG_DEBUG(("Error during authentication protocol"));
575 silc_protocol_free(protocol);
577 silc_free(ctx->auth_data);
579 silc_ske_free(ctx->ske);
581 silc_free(ctx->dest_id);
582 conn->sock->protocol = NULL;
583 silc_socket_free(ctx->sock);
585 /* Notify application of failure */
586 client->ops->connect(client, ctx->sock->user_data, FALSE);
591 /* Send NEW_CLIENT packet to the server. We will become registered
592 to the SILC network after sending this packet and we will receive
593 client ID from the server. */
594 packet = silc_buffer_alloc(2 + 2 + strlen(client->username) +
595 strlen(client->realname));
596 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
597 silc_buffer_format(packet,
598 SILC_STR_UI_SHORT(strlen(client->username)),
599 SILC_STR_UI_XNSTRING(client->username,
600 strlen(client->username)),
601 SILC_STR_UI_SHORT(strlen(client->realname)),
602 SILC_STR_UI_XNSTRING(client->realname,
603 strlen(client->realname)),
606 /* Send the packet */
607 silc_client_packet_send(client, ctx->sock, SILC_PACKET_NEW_CLIENT,
609 packet->data, packet->len, TRUE);
610 silc_buffer_free(packet);
612 /* Save remote ID. */
613 conn->remote_id = ctx->dest_id;
614 conn->remote_id_data = silc_id_id2str(ctx->dest_id, SILC_ID_SERVER);
615 conn->remote_id_data_len = silc_id_get_len(ctx->dest_id, SILC_ID_SERVER);
617 /* Register re-key timeout */
618 conn->rekey->timeout = client->params->rekey_secs;
619 conn->rekey->context = (void *)client;
620 silc_schedule_task_add(client->schedule, conn->sock->sock,
621 silc_client_rekey_callback,
622 (void *)conn->sock, conn->rekey->timeout, 0,
623 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
625 silc_protocol_free(protocol);
627 silc_free(ctx->auth_data);
629 silc_ske_free(ctx->ske);
630 silc_socket_free(ctx->sock);
632 conn->sock->protocol = NULL;
635 /* Internal routine that sends packet or marks packet to be sent. This
636 is used directly only in special cases. Normal cases should use
637 silc_server_packet_send. Returns < 0 on error. */
639 int silc_client_packet_send_real(SilcClient client,
640 SilcSocketConnection sock,
645 /* If rekey protocol is active we must assure that all packets are
646 sent through packet queue. */
647 if (SILC_CLIENT_IS_REKEY(sock))
650 /* If outbound data is already pending do not force send */
651 if (SILC_IS_OUTBUF_PENDING(sock))
654 /* Send the packet */
655 ret = silc_packet_send(sock, force_send);
659 /* Mark that there is some outgoing data available for this connection.
660 This call sets the connection both for input and output (the input
661 is set always and this call keeps the input setting, actually).
662 Actual data sending is performed by silc_client_packet_process. */
663 SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(client->schedule, sock->sock);
665 /* Mark to socket that data is pending in outgoing buffer. This flag
666 is needed if new data is added to the buffer before the earlier
667 put data is sent to the network. */
668 SILC_SET_OUTBUF_PENDING(sock);
673 /* Packet processing callback. This is used to send and receive packets
674 from network. This is generic task. */
676 SILC_TASK_CALLBACK_GLOBAL(silc_client_packet_process)
678 SilcClient client = (SilcClient)context;
679 SilcSocketConnection sock = NULL;
680 SilcClientConnection conn;
683 SILC_LOG_DEBUG(("Processing packet"));
685 SILC_CLIENT_GET_SOCK(client, fd, sock);
689 conn = (SilcClientConnection)sock->user_data;
692 if (type == SILC_TASK_WRITE) {
693 /* Do not send data to disconnected connection */
694 if (SILC_IS_DISCONNECTED(sock))
697 if (sock->outbuf->data - sock->outbuf->head)
698 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
700 ret = silc_packet_send(sock, TRUE);
702 /* If returned -2 could not write to connection now, will do
711 /* The packet has been sent and now it is time to set the connection
712 back to only for input. When there is again some outgoing data
713 available for this connection it will be set for output as well.
714 This call clears the output setting and sets it only for input. */
715 SILC_CLIENT_SET_CONNECTION_FOR_INPUT(client->schedule, fd);
716 SILC_UNSET_OUTBUF_PENDING(sock);
718 silc_buffer_clear(sock->outbuf);
722 /* Packet receiving */
723 if (type == SILC_TASK_READ) {
724 /* Read data from network */
725 ret = silc_packet_receive(sock);
731 SILC_LOG_DEBUG(("Read EOF"));
733 /* If connection is disconnecting already we will finally
734 close the connection */
735 if (SILC_IS_DISCONNECTING(sock)) {
736 if (sock == conn->sock)
737 client->ops->disconnect(client, conn);
738 silc_client_close_connection(client, sock, conn);
742 SILC_LOG_DEBUG(("EOF from connection %d", sock->sock));
743 if (sock == conn->sock)
744 client->ops->disconnect(client, conn);
745 silc_client_close_connection(client, sock, conn);
749 /* Process the packet. This will call the parser that will then
750 decrypt and parse the packet. */
751 if (sock->type != SILC_SOCKET_TYPE_UNKNOWN)
752 silc_packet_receive_process(sock, FALSE, conn->receive_key,
753 conn->hmac_receive, conn->psn_receive,
754 silc_client_packet_parse, client);
756 silc_packet_receive_process(sock, FALSE, NULL, NULL, 0,
757 silc_client_packet_parse, client);
761 /* Parses whole packet, received earlier. */
763 SILC_TASK_CALLBACK(silc_client_packet_parse_real)
765 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
766 SilcClient client = (SilcClient)parse_ctx->context;
767 SilcPacketContext *packet = parse_ctx->packet;
768 SilcSocketConnection sock = parse_ctx->sock;
769 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
772 SILC_LOG_DEBUG(("Start"));
774 /* Parse the packet */
775 if (parse_ctx->normal)
776 ret = silc_packet_parse(packet, conn->receive_key);
778 ret = silc_packet_parse_special(packet, conn->receive_key);
780 if (ret == SILC_PACKET_NONE)
783 /* Parse the incoming packet type */
784 silc_client_packet_parse_type(client, sock, packet);
787 /* silc_buffer_clear(sock->inbuf); */
788 silc_packet_context_free(packet);
789 silc_free(parse_ctx);
792 /* Parser callback called by silc_packet_receive_process. Thie merely
793 registers timeout that will handle the actual parsing when appropriate. */
795 void silc_client_packet_parse(SilcPacketParserContext *parser_context,
798 SilcClient client = (SilcClient)context;
799 SilcSocketConnection sock = parser_context->sock;
800 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
802 if (conn && conn->hmac_receive)
803 conn->psn_receive = parser_context->packet->sequence + 1;
805 /* Parse the packet */
806 silc_schedule_task_add(client->schedule, parser_context->sock->sock,
807 silc_client_packet_parse_real,
808 (void *)parser_context, 0, 1,
810 SILC_TASK_PRI_NORMAL);
813 /* Parses the packet type and calls what ever routines the packet type
814 requires. This is done for all incoming packets. */
816 void silc_client_packet_parse_type(SilcClient client,
817 SilcSocketConnection sock,
818 SilcPacketContext *packet)
820 SilcBuffer buffer = packet->buffer;
821 SilcPacketType type = packet->type;
823 SILC_LOG_DEBUG(("Parsing packet type %d", type));
825 /* Parse the packet type */
827 case SILC_PACKET_DISCONNECT:
828 silc_client_disconnected_by_server(client, sock, buffer);
830 case SILC_PACKET_SUCCESS:
832 * Success received for something. For now we can have only
833 * one protocol for connection executing at once hence this
834 * success message is for whatever protocol is executing currently.
837 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
839 case SILC_PACKET_FAILURE:
841 * Failure received for some protocol. Set the protocol state to
842 * error and call the protocol callback. This fill cause error on
843 * protocol and it will call the final callback.
845 silc_client_process_failure(client, sock, packet);
847 case SILC_PACKET_REJECT:
850 case SILC_PACKET_NOTIFY:
852 * Received notify message
854 silc_client_notify_by_server(client, sock, packet);
857 case SILC_PACKET_ERROR:
859 * Received error message
861 silc_client_error_by_server(client, sock, buffer);
864 case SILC_PACKET_CHANNEL_MESSAGE:
866 * Received message to (from, actually) a channel
868 silc_client_channel_message(client, sock, packet);
870 case SILC_PACKET_CHANNEL_KEY:
872 * Received key for a channel. By receiving this key the client will be
873 * able to talk to the channel it has just joined. This can also be
874 * a new key for existing channel as keys expire peridiocally.
876 silc_client_receive_channel_key(client, sock, buffer);
879 case SILC_PACKET_PRIVATE_MESSAGE:
881 * Received private message
883 silc_client_private_message(client, sock, packet);
885 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
887 * Received private message key
891 case SILC_PACKET_COMMAND_REPLY:
893 * Recived reply for a command
895 silc_client_command_reply_process(client, sock, packet);
898 case SILC_PACKET_KEY_EXCHANGE:
899 if (sock->protocol && sock->protocol->protocol &&
900 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE) {
901 SilcClientKEInternalContext *proto_ctx =
902 (SilcClientKEInternalContext *)sock->protocol->context;
904 proto_ctx->packet = silc_packet_context_dup(packet);
905 proto_ctx->dest_id_type = packet->src_id_type;
906 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
907 packet->src_id_type);
908 if (!proto_ctx->dest_id)
911 /* Let the protocol handle the packet */
912 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
914 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
915 "protocol active, packet dropped."));
919 case SILC_PACKET_KEY_EXCHANGE_1:
920 if (sock->protocol && sock->protocol->protocol &&
921 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
922 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)) {
924 if (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
925 SilcClientRekeyInternalContext *proto_ctx =
926 (SilcClientRekeyInternalContext *)sock->protocol->context;
928 if (proto_ctx->packet)
929 silc_packet_context_free(proto_ctx->packet);
931 proto_ctx->packet = silc_packet_context_dup(packet);
933 /* Let the protocol handle the packet */
934 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
936 SilcClientKEInternalContext *proto_ctx =
937 (SilcClientKEInternalContext *)sock->protocol->context;
939 if (proto_ctx->packet)
940 silc_packet_context_free(proto_ctx->packet);
942 proto_ctx->packet = silc_packet_context_dup(packet);
943 proto_ctx->dest_id_type = packet->src_id_type;
944 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
945 packet->src_id_type);
946 if (!proto_ctx->dest_id)
949 /* Let the protocol handle the packet */
950 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
953 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
954 "protocol active, packet dropped."));
957 case SILC_PACKET_KEY_EXCHANGE_2:
958 if (sock->protocol && sock->protocol->protocol &&
959 (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
960 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)) {
962 if (sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
963 SilcClientRekeyInternalContext *proto_ctx =
964 (SilcClientRekeyInternalContext *)sock->protocol->context;
966 if (proto_ctx->packet)
967 silc_packet_context_free(proto_ctx->packet);
969 proto_ctx->packet = silc_packet_context_dup(packet);
971 /* Let the protocol handle the packet */
972 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
974 SilcClientKEInternalContext *proto_ctx =
975 (SilcClientKEInternalContext *)sock->protocol->context;
977 if (proto_ctx->packet)
978 silc_packet_context_free(proto_ctx->packet);
980 proto_ctx->packet = silc_packet_context_dup(packet);
981 proto_ctx->dest_id_type = packet->src_id_type;
982 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
983 packet->src_id_type);
984 if (!proto_ctx->dest_id)
987 /* Let the protocol handle the packet */
988 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
991 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
992 "protocol active, packet dropped."));
996 case SILC_PACKET_NEW_ID:
999 * Received new ID from server. This packet is received at
1000 * the connection to the server. New ID is also received when
1001 * user changes nickname but in that case the new ID is received
1002 * as command reply and not as this packet type.
1006 idp = silc_id_payload_parse(buffer);
1009 if (silc_id_payload_get_type(idp) != SILC_ID_CLIENT)
1012 silc_client_receive_new_id(client, sock, idp);
1013 silc_id_payload_free(idp);
1017 case SILC_PACKET_HEARTBEAT:
1019 * Received heartbeat packet
1021 SILC_LOG_DEBUG(("Heartbeat packet"));
1024 case SILC_PACKET_KEY_AGREEMENT:
1026 * Received key agreement packet
1028 SILC_LOG_DEBUG(("Key agreement packet"));
1029 silc_client_key_agreement(client, sock, packet);
1032 case SILC_PACKET_REKEY:
1033 SILC_LOG_DEBUG(("Re-key packet"));
1034 /* We ignore this for now */
1037 case SILC_PACKET_REKEY_DONE:
1038 SILC_LOG_DEBUG(("Re-key done packet"));
1040 if (sock->protocol && sock->protocol->protocol &&
1041 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY) {
1043 SilcClientRekeyInternalContext *proto_ctx =
1044 (SilcClientRekeyInternalContext *)sock->protocol->context;
1046 if (proto_ctx->packet)
1047 silc_packet_context_free(proto_ctx->packet);
1049 proto_ctx->packet = silc_packet_context_dup(packet);
1051 /* Let the protocol handle the packet */
1052 if (proto_ctx->responder == FALSE)
1053 silc_protocol_execute(sock->protocol, client->schedule, 0, 0);
1055 /* Let the protocol handle the packet */
1056 silc_protocol_execute(sock->protocol, client->schedule,
1059 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
1060 "protocol active, packet dropped."));
1064 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1066 * Reveived reply to our connection authentication method request
1067 * packet. This is used to resolve the authentication method for the
1068 * current session from the server if the client does not know it.
1070 silc_client_connection_auth_request(client, sock, packet);
1073 case SILC_PACKET_FTP:
1074 /* Received file transfer packet. */
1075 silc_client_ftp(client, sock, packet);
1079 SILC_LOG_DEBUG(("Incorrect packet type %d, packet dropped", type));
1084 /* Sends packet. This doesn't actually send the packet instead it assembles
1085 it and marks it to be sent. However, if force_send is TRUE the packet
1086 is sent immediately. if dst_id, cipher and hmac are NULL those parameters
1087 will be derived from sock argument. Otherwise the valid arguments sent
1090 void silc_client_packet_send(SilcClient client,
1091 SilcSocketConnection sock,
1092 SilcPacketType type,
1094 SilcIdType dst_id_type,
1097 unsigned char *data,
1101 SilcPacketContext packetdata;
1103 uint32 sequence = 0;
1108 SILC_LOG_DEBUG(("Sending packet, type %d", type));
1110 /* Get data used in the packet sending, keys and stuff */
1111 if ((!cipher || !hmac || !dst_id) && sock->user_data) {
1112 if (!cipher && ((SilcClientConnection)sock->user_data)->send_key)
1113 cipher = ((SilcClientConnection)sock->user_data)->send_key;
1115 if (!hmac && ((SilcClientConnection)sock->user_data)->hmac_send)
1116 hmac = ((SilcClientConnection)sock->user_data)->hmac_send;
1118 if (!dst_id && ((SilcClientConnection)sock->user_data)->remote_id) {
1119 dst_id = ((SilcClientConnection)sock->user_data)->remote_id;
1120 dst_id_type = SILC_ID_SERVER;
1124 sequence = ((SilcClientConnection)sock->user_data)->psn_send++;
1127 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
1129 /* Set the packet context pointers */
1130 packetdata.flags = 0;
1131 packetdata.type = type;
1132 if (sock->user_data &&
1133 ((SilcClientConnection)sock->user_data)->local_id_data) {
1134 packetdata.src_id = ((SilcClientConnection)sock->user_data)->local_id_data;
1135 packetdata.src_id_len =
1136 silc_id_get_len(((SilcClientConnection)sock->user_data)->local_id,
1139 packetdata.src_id = silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char));
1140 packetdata.src_id_len = SILC_ID_CLIENT_LEN;
1142 packetdata.src_id_type = SILC_ID_CLIENT;
1144 packetdata.dst_id = silc_id_id2str(dst_id, dst_id_type);
1145 packetdata.dst_id_len = silc_id_get_len(dst_id, dst_id_type);
1146 packetdata.dst_id_type = dst_id_type;
1148 packetdata.dst_id = NULL;
1149 packetdata.dst_id_len = 0;
1150 packetdata.dst_id_type = SILC_ID_NONE;
1152 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1153 packetdata.src_id_len + packetdata.dst_id_len;
1154 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
1156 /* Prepare outgoing data buffer for packet sending */
1157 silc_packet_send_prepare(sock,
1158 SILC_PACKET_HEADER_LEN +
1159 packetdata.src_id_len +
1160 packetdata.dst_id_len,
1164 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
1166 packetdata.buffer = sock->outbuf;
1168 /* Put the data to the buffer */
1169 if (data && data_len)
1170 silc_buffer_put(sock->outbuf, data, data_len);
1172 /* Create the outgoing packet */
1173 silc_packet_assemble(&packetdata, cipher);
1175 /* Encrypt the packet */
1177 silc_packet_encrypt(cipher, hmac, sequence, sock->outbuf,
1180 SILC_LOG_HEXDUMP(("Packet, len %d", sock->outbuf->len),
1181 sock->outbuf->data, sock->outbuf->len);
1183 /* Now actually send the packet */
1184 silc_client_packet_send_real(client, sock, force_send);
1187 void silc_client_packet_queue_purge(SilcClient client,
1188 SilcSocketConnection sock)
1190 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1191 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1192 if (sock->outbuf->data - sock->outbuf->head)
1193 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
1195 silc_packet_send(sock, TRUE);
1197 SILC_CLIENT_SET_CONNECTION_FOR_INPUT(client->schedule, sock->sock);
1198 SILC_UNSET_OUTBUF_PENDING(sock);
1199 silc_buffer_clear(sock->outbuf);
1203 /* Closes connection to remote end. Free's all allocated data except
1204 for some information such as nickname etc. that are valid at all time.
1205 If the `sock' is NULL then the conn->sock will be used. If `sock' is
1206 provided it will be checked whether the sock and `conn->sock' are the
1207 same (they can be different, ie. a socket can use `conn' as its
1208 connection but `conn->sock' might be actually a different connection
1209 than the `sock'). */
1211 void silc_client_close_connection(SilcClient client,
1212 SilcSocketConnection sock,
1213 SilcClientConnection conn)
1217 if (!sock || (sock && conn->sock == sock))
1222 /* We won't listen for this connection anymore */
1223 silc_schedule_unset_listen_fd(client->schedule, sock->sock);
1225 /* Unregister all tasks */
1226 silc_schedule_task_del_by_fd(client->schedule, sock->sock);
1227 silc_schedule_task_del_by_fd(client->schedule, sock->sock);
1229 /* Close the actual connection */
1230 silc_net_close_connection(sock->sock);
1232 /* Cancel any active protocol */
1233 if (sock->protocol) {
1234 if (sock->protocol->protocol->type ==
1235 SILC_PROTOCOL_CLIENT_KEY_EXCHANGE ||
1236 sock->protocol->protocol->type ==
1237 SILC_PROTOCOL_CLIENT_CONNECTION_AUTH) {
1238 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
1239 silc_protocol_execute_final(sock->protocol, client->schedule);
1240 /* The application will recall this function with these protocols
1241 (the ops->connect client operation). */
1244 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
1245 silc_protocol_execute_final(sock->protocol, client->schedule);
1246 sock->protocol = NULL;
1250 /* Free everything */
1251 if (del && sock->user_data) {
1252 /* Free all cache entries */
1253 SilcIDCacheList list;
1254 SilcIDCacheEntry entry;
1257 if (silc_idcache_get_all(conn->client_cache, &list)) {
1258 ret = silc_idcache_list_first(list, &entry);
1260 silc_client_del_client(client, conn, entry->context);
1261 ret = silc_idcache_list_next(list, &entry);
1263 silc_idcache_list_free(list);
1266 if (silc_idcache_get_all(conn->channel_cache, &list)) {
1267 ret = silc_idcache_list_first(list, &entry);
1269 silc_client_del_channel(client, conn, entry->context);
1270 ret = silc_idcache_list_next(list, &entry);
1272 silc_idcache_list_free(list);
1275 if (silc_idcache_get_all(conn->server_cache, &list)) {
1276 ret = silc_idcache_list_first(list, &entry);
1278 silc_client_del_server(client, conn, entry->context);
1279 ret = silc_idcache_list_next(list, &entry);
1281 silc_idcache_list_free(list);
1284 /* Clear ID caches */
1285 if (conn->client_cache)
1286 silc_idcache_del_all(conn->client_cache);
1287 if (conn->channel_cache)
1288 silc_idcache_del_all(conn->channel_cache);
1289 if (conn->server_cache)
1290 silc_idcache_del_all(conn->server_cache);
1292 /* Free data (my ID is freed in above silc_client_del_client) */
1293 if (conn->remote_host)
1294 silc_free(conn->remote_host);
1295 if (conn->local_id_data)
1296 silc_free(conn->local_id_data);
1298 silc_cipher_free(conn->send_key);
1299 if (conn->receive_key)
1300 silc_cipher_free(conn->receive_key);
1301 if (conn->hmac_send)
1302 silc_hmac_free(conn->hmac_send);
1303 if (conn->hmac_receive)
1304 silc_hmac_free(conn->hmac_receive);
1305 if (conn->pending_commands)
1306 silc_dlist_uninit(conn->pending_commands);
1308 silc_free(conn->rekey);
1310 memset(conn, 0, sizeof(*conn));
1311 silc_client_del_connection(client, conn);
1314 silc_socket_free(sock);
1317 /* Called when we receive disconnection packet from server. This
1318 closes our end properly and displays the reason of the disconnection
1321 void silc_client_disconnected_by_server(SilcClient client,
1322 SilcSocketConnection sock,
1327 SILC_LOG_DEBUG(("Server disconnected us, sock %d", sock->sock));
1329 msg = silc_calloc(message->len + 1, sizeof(char));
1330 memcpy(msg, message->data, message->len);
1331 client->ops->say(client, sock->user_data, SILC_CLIENT_MESSAGE_AUDIT, msg);
1334 SILC_SET_DISCONNECTED(sock);
1335 silc_client_close_connection(client, sock, sock->user_data);
1338 /* Received error message from server. Display it on the screen.
1339 We don't take any action what so ever of the error message. */
1341 void silc_client_error_by_server(SilcClient client,
1342 SilcSocketConnection sock,
1347 msg = silc_calloc(message->len + 1, sizeof(char));
1348 memcpy(msg, message->data, message->len);
1349 client->ops->say(client, sock->user_data, SILC_CLIENT_MESSAGE_AUDIT, msg);
1353 /* Processes the received new Client ID from server. Old Client ID is
1354 deleted from cache and new one is added. */
1356 void silc_client_receive_new_id(SilcClient client,
1357 SilcSocketConnection sock,
1360 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1361 int connecting = FALSE;
1364 if (!conn->local_entry)
1367 /* Delete old ID from ID cache */
1368 if (conn->local_id) {
1369 silc_idcache_del_by_context(conn->client_cache, conn->local_entry);
1370 silc_free(conn->local_id);
1373 /* Save the new ID */
1375 if (conn->local_id_data)
1376 silc_free(conn->local_id_data);
1378 conn->local_id = silc_id_payload_get_id(idp);
1379 conn->local_id_data = silc_id_payload_get_data(idp);
1380 conn->local_id_data_len = silc_id_payload_get_len(idp);;
1382 if (!conn->local_entry)
1383 conn->local_entry = silc_calloc(1, sizeof(*conn->local_entry));
1385 conn->local_entry->nickname = conn->nickname;
1386 if (!conn->local_entry->username)
1387 conn->local_entry->username = strdup(client->username);
1388 if (!conn->local_entry->hostname)
1389 conn->local_entry->hostname = strdup(client->hostname);
1390 conn->local_entry->server = strdup(conn->remote_host);
1391 conn->local_entry->id = conn->local_id;
1392 conn->local_entry->valid = TRUE;
1394 /* Put it to the ID cache */
1395 silc_idcache_add(conn->client_cache, strdup(conn->nickname), conn->local_id,
1396 (void *)conn->local_entry, FALSE);
1398 /* Issue INFO command to fetch the real server name and server information
1400 sidp = silc_id_payload_encode(conn->remote_id, SILC_ID_SERVER);
1401 silc_client_send_command(client, conn, SILC_COMMAND_INFO,
1402 ++conn->cmd_ident, 1, 2, sidp->data, sidp->len);
1403 silc_buffer_free(sidp);
1405 /* Notify application of successful connection. We do it here now that
1406 we've received the Client ID and are allowed to send traffic. */
1408 client->ops->connect(client, conn, TRUE);
1411 /* Processed received Channel ID for a channel. This is called when client
1412 joins to channel and server replies with channel ID. The ID is cached.
1413 Returns the created channel entry. This is also called when received
1414 channel ID in for example USERS command reply that we do not have. */
1416 SilcChannelEntry silc_client_new_channel_id(SilcClient client,
1417 SilcSocketConnection sock,
1422 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1423 SilcChannelEntry channel;
1425 SILC_LOG_DEBUG(("New channel ID"));
1427 channel = silc_calloc(1, sizeof(*channel));
1428 channel->channel_name = channel_name;
1429 channel->id = silc_id_payload_get_id(idp);
1430 channel->mode = mode;
1431 silc_list_init(channel->clients, struct SilcChannelUserStruct, next);
1433 /* Put it to the ID cache */
1434 silc_idcache_add(conn->channel_cache, channel->channel_name,
1435 (void *)channel->id, (void *)channel, FALSE);
1440 /* Removes a client entry from all channel it has joined. This really is
1441 a performance killer (client_entry should have pointers to channel
1444 void silc_client_remove_from_channels(SilcClient client,
1445 SilcClientConnection conn,
1446 SilcClientEntry client_entry)
1448 SilcIDCacheEntry id_cache;
1449 SilcIDCacheList list;
1450 SilcChannelEntry channel;
1451 SilcChannelUser chu;
1453 if (!silc_idcache_get_all(conn->channel_cache, &list))
1456 silc_idcache_list_first(list, &id_cache);
1457 channel = (SilcChannelEntry)id_cache->context;
1461 /* Remove client from channel */
1462 silc_list_start(channel->clients);
1463 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1464 if (chu->client == client_entry) {
1465 silc_list_del(channel->clients, chu);
1471 if (!silc_idcache_list_next(list, &id_cache))
1474 channel = (SilcChannelEntry)id_cache->context;
1477 silc_idcache_list_free(list);
1480 /* Replaces `old' client entries from all channels to `new' client entry.
1481 This can be called for example when nickname changes and old ID entry
1482 is replaced from ID cache with the new one. If the old ID entry is only
1483 updated, then this fucntion needs not to be called. */
1485 void silc_client_replace_from_channels(SilcClient client,
1486 SilcClientConnection conn,
1487 SilcClientEntry old,
1488 SilcClientEntry new)
1490 SilcIDCacheEntry id_cache;
1491 SilcIDCacheList list;
1492 SilcChannelEntry channel;
1493 SilcChannelUser chu;
1495 if (!silc_idcache_get_all(conn->channel_cache, &list))
1498 silc_idcache_list_first(list, &id_cache);
1499 channel = (SilcChannelEntry)id_cache->context;
1503 /* Replace client entry */
1504 silc_list_start(channel->clients);
1505 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1506 if (chu->client == old) {
1512 if (!silc_idcache_list_next(list, &id_cache))
1515 channel = (SilcChannelEntry)id_cache->context;
1518 silc_idcache_list_free(list);
1521 /* Registers failure timeout to process the received failure packet
1524 void silc_client_process_failure(SilcClient client,
1525 SilcSocketConnection sock,
1526 SilcPacketContext *packet)
1530 if (sock->protocol) {
1531 if (packet->buffer->len >= 4)
1532 SILC_GET32_MSB(failure, packet->buffer->data);
1534 /* Notify application */
1535 client->ops->failure(client, sock->user_data, sock->protocol,
1540 /* A timeout callback for the re-key. We will be the initiator of the
1543 SILC_TASK_CALLBACK(silc_client_rekey_callback)
1545 SilcSocketConnection sock = (SilcSocketConnection)context;
1546 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1547 SilcClient client = (SilcClient)conn->rekey->context;
1548 SilcProtocol protocol;
1549 SilcClientRekeyInternalContext *proto_ctx;
1551 SILC_LOG_DEBUG(("Start"));
1553 /* Allocate internal protocol context. This is sent as context
1555 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1556 proto_ctx->client = (void *)client;
1557 proto_ctx->sock = silc_socket_dup(sock);
1558 proto_ctx->responder = FALSE;
1559 proto_ctx->pfs = conn->rekey->pfs;
1561 /* Perform rekey protocol. Will call the final callback after the
1562 protocol is over. */
1563 silc_protocol_alloc(SILC_PROTOCOL_CLIENT_REKEY,
1564 &protocol, proto_ctx, silc_client_rekey_final);
1565 sock->protocol = protocol;
1567 /* Run the protocol */
1568 silc_protocol_execute(protocol, client->schedule, 0, 0);
1570 /* Re-register re-key timeout */
1571 silc_schedule_task_add(client->schedule, sock->sock,
1572 silc_client_rekey_callback,
1573 context, conn->rekey->timeout, 0,
1574 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1577 /* The final callback for the REKEY protocol. This will actually take the
1578 new key material into use. */
1580 SILC_TASK_CALLBACK(silc_client_rekey_final)
1582 SilcProtocol protocol = (SilcProtocol)context;
1583 SilcClientRekeyInternalContext *ctx =
1584 (SilcClientRekeyInternalContext *)protocol->context;
1585 SilcClient client = (SilcClient)ctx->client;
1586 SilcSocketConnection sock = ctx->sock;
1588 SILC_LOG_DEBUG(("Start"));
1590 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1591 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1592 /* Error occured during protocol */
1593 silc_protocol_cancel(protocol, client->schedule);
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);
1605 /* Purge the outgoing data queue to assure that all rekey packets really
1606 go to the network before we quit the protocol. */
1607 silc_client_packet_queue_purge(client, sock);
1610 silc_protocol_free(protocol);
1611 sock->protocol = NULL;
1613 silc_packet_context_free(ctx->packet);
1615 silc_ske_free(ctx->ske);
1616 silc_socket_free(ctx->sock);
1620 /* Processes incoming connection authentication method request packet.
1621 It is a reply to our previously sent request. The packet can be used
1622 to resolve the authentication method for the current session if the
1623 client does not know it beforehand. */
1625 void silc_client_connection_auth_request(SilcClient client,
1626 SilcSocketConnection sock,
1627 SilcPacketContext *packet)
1629 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1630 uint16 conn_type, auth_meth;
1633 /* If we haven't send our request then ignore this one. */
1634 if (!conn->connauth)
1637 /* Parse the payload */
1638 ret = silc_buffer_unformat(packet->buffer,
1639 SILC_STR_UI_SHORT(&conn_type),
1640 SILC_STR_UI_SHORT(&auth_meth),
1643 auth_meth = SILC_AUTH_NONE;
1645 /* Call the request callback to notify application for received
1646 authentication method information. */
1647 if (conn->connauth->callback)
1648 (*conn->connauth->callback)(client, conn, auth_meth,
1649 conn->connauth->context);
1651 silc_schedule_task_del(client->schedule, conn->connauth->timeout);
1653 silc_free(conn->connauth);
1654 conn->connauth = NULL;
1657 /* Timeout task callback called if the server does not reply to our
1658 connection authentication method request in the specified time interval. */
1660 SILC_TASK_CALLBACK(silc_client_request_authentication_method_timeout)
1662 SilcClientConnection conn = (SilcClientConnection)context;
1663 SilcClient client = conn->client;
1665 if (!conn->connauth)
1668 /* Call the request callback to notify application */
1669 if (conn->connauth->callback)
1670 (*conn->connauth->callback)(client, conn, SILC_AUTH_NONE,
1671 conn->connauth->context);
1673 silc_free(conn->connauth);
1674 conn->connauth = NULL;
1677 /* This function can be used to request the current authentication method
1678 from the server. This may be called when connecting to the server
1679 and the client library requests the authentication data from the
1680 application. If the application does not know the current authentication
1681 method it can request it from the server using this function.
1682 The `callback' with `context' will be called after the server has
1683 replied back with the current authentication method. */
1686 silc_client_request_authentication_method(SilcClient client,
1687 SilcClientConnection conn,
1688 SilcConnectionAuthRequest callback,
1691 SilcClientConnAuthRequest connauth;
1694 connauth = silc_calloc(1, sizeof(*connauth));
1695 connauth->callback = callback;
1696 connauth->context = context;
1699 silc_free(conn->connauth);
1701 conn->connauth = connauth;
1703 /* Assemble the request packet and send it to the server */
1704 packet = silc_buffer_alloc(4);
1705 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1706 silc_buffer_format(packet,
1707 SILC_STR_UI_SHORT(SILC_SOCKET_TYPE_CLIENT),
1708 SILC_STR_UI_SHORT(SILC_AUTH_NONE),
1710 silc_client_packet_send(client, conn->sock,
1711 SILC_PACKET_CONNECTION_AUTH_REQUEST,
1712 NULL, 0, NULL, NULL,
1713 packet->data, packet->len, FALSE);
1714 silc_buffer_free(packet);
1716 /* Register a timeout in case server does not reply anything back. */
1718 silc_schedule_task_add(client->schedule, conn->sock->sock,
1719 silc_client_request_authentication_method_timeout,
1720 conn, client->params->connauth_request_secs, 0,
1721 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1724 /* Called when file transfer packet is received. This will parse the
1725 packet and give it to the file transfer protocol. */
1727 void silc_client_ftp(SilcClient client,
1728 SilcSocketConnection sock,
1729 SilcPacketContext *packet)
1731 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
1735 /* Parse the payload */
1736 ret = silc_buffer_unformat(packet->buffer,
1737 SILC_STR_UI_CHAR(&type),
1742 /* We support only type number 1 (== SFTP) */
1746 silc_buffer_pull(packet->buffer, 1);
1748 /* Give it to the file transfer protocol processor. */
1749 //silc_sftp_client_receive_process(xxx, sock, packet);