5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "silcclient.h"
22 #include "client_internal.h"
24 /************************** Types and definitions ***************************/
26 /* Resume session context */
29 SilcClientConnection conn;
30 SilcBufferStruct detach;
32 SilcUInt32 channel_count;
33 } *SilcClientResumeSession;
35 /************************ Static utility functions **************************/
37 /* Command callback. Nothing interesting to do here. */
40 silc_client_register_command_called(SilcClient client,
41 SilcClientConnection conn,
51 /* Continues resuming after resolving. Continue after last reply. */
54 silc_client_resume_continue(SilcClient client,
55 SilcClientConnection conn,
62 if (status == SILC_STATUS_OK || status == SILC_STATUS_LIST_END ||
63 SILC_STATUS_IS_ERROR(status)) {
64 silc_fsm_continue(&conn->internal->event_thread);
71 /* Function used to call command replies back to application in resuming. */
74 silc_client_resume_command_callback(SilcClient client,
75 SilcClientConnection conn,
76 SilcCommand command, ...)
79 va_start(ap, command);
80 client->internal->ops->command_reply(client, conn, command,
81 SILC_STATUS_OK, SILC_STATUS_OK, ap);
86 /****************************** NEW_ID packet *******************************/
88 /* Received new ID packet from server during registering to SILC network */
90 SILC_FSM_STATE(silc_client_new_id)
92 SilcClientConnection conn = fsm_context;
93 SilcClient client = conn->client;
94 SilcPacket packet = state_context;
100 SILC_LOG_DEBUG(("New ID received from server"));
102 if (!silc_id_payload_parse_id(silc_buffer_data(&packet->buffer),
103 silc_buffer_len(&packet->buffer), &id))
106 SILC_LOG_DEBUG(("New ID %s", silc_id_render(&id.u.client_id,
109 /* Create local client entry */
110 conn->local_entry = silc_client_add_client(client, conn,
115 if (!conn->local_entry)
118 /* Save the ID. Take reference to conn->local_id. */
119 conn->local_id = &conn->local_entry->id;
120 conn->internal->local_idp = silc_buffer_copy(&packet->buffer);
123 if (packet->src_id_len) {
124 conn->internal->remote_idp =
125 silc_id_payload_encode_data(packet->src_id,
127 packet->src_id_type);
128 if (!conn->internal->remote_idp)
130 silc_id_payload_parse_id(silc_buffer_data(conn->internal->remote_idp),
131 silc_buffer_len(conn->internal->remote_idp),
135 /* Set IDs to the packet stream */
136 silc_packet_set_ids(conn->stream, SILC_ID_CLIENT, conn->local_id,
137 conn->remote_id.type, SILC_ID_GET_ID(conn->remote_id));
139 /* Signal connection that new ID was received so it can continue
140 with the registering. */
141 if (conn->internal->registering)
142 silc_fsm_continue_sync(&conn->internal->event_thread);
145 /** Packet processed */
146 silc_packet_free(packet);
147 return SILC_FSM_FINISH;
151 /************************ Register to SILC network **************************/
153 /* Register to network */
155 SILC_FSM_STATE(silc_client_st_register)
157 SilcClientConnection conn = fsm_context;
158 SilcClient client = conn->client;
160 SILC_LOG_DEBUG(("Register to network"));
162 /* Send NEW_CLIENT packet to register to network */
163 if (!silc_packet_send_va(conn->stream, SILC_PACKET_NEW_CLIENT, 0,
164 SILC_STR_UI_SHORT(strlen(client->username)),
165 SILC_STR_DATA(client->username,
166 strlen(client->username)),
167 SILC_STR_UI_SHORT(strlen(client->realname)),
168 SILC_STR_DATA(client->realname,
169 strlen(client->realname)),
171 /** Error sending packet */
172 silc_fsm_next(fsm, silc_client_st_register_error);
173 return SILC_FSM_CONTINUE;
176 /** Wait for new ID */
177 conn->internal->registering = TRUE;
178 silc_fsm_next_later(fsm, silc_client_st_register_complete,
179 conn->internal->retry_timer, 0);
180 return SILC_FSM_WAIT;
183 /* Wait for NEW_ID packet to arrive */
185 SILC_FSM_STATE(silc_client_st_register_complete)
187 SilcClientConnection conn = fsm_context;
188 SilcClient client = conn->client;
190 if (conn->internal->disconnected) {
192 silc_fsm_next(fsm, silc_client_st_register_error);
193 return SILC_FSM_CONTINUE;
196 if (!conn->local_id) {
197 if (conn->internal->retry_count++ >= SILC_CLIENT_RETRY_COUNT) {
198 /** Timeout, ID not received */
199 conn->internal->registering = FALSE;
200 conn->internal->retry_count = 0;
201 conn->internal->retry_timer = SILC_CLIENT_RETRY_MIN;
202 silc_fsm_next(fsm, silc_client_st_register_error);
203 return SILC_FSM_CONTINUE;
206 /** Resend registering packet */
207 silc_fsm_next(fsm, silc_client_st_register);
208 conn->internal->retry_timer = ((conn->internal->retry_timer *
209 SILC_CLIENT_RETRY_MUL) +
210 (silc_rng_get_rn16(client->rng) %
211 SILC_CLIENT_RETRY_RAND));
212 return SILC_FSM_CONTINUE;
215 SILC_LOG_DEBUG(("Registered to network"));
217 /* Issue IDENTIFY command for itself to get resolved hostname
218 correctly from server. */
219 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
220 silc_client_register_command_called, NULL,
221 1, 5, silc_buffer_data(conn->internal->local_idp),
222 silc_buffer_len(conn->internal->local_idp));
224 /* Call NICK command if the nickname was set by the application (and is
225 not same as the username). */
226 if (conn->internal->params.nickname &&
227 !silc_utf8_strcasecmp(conn->internal->params.nickname, client->username))
228 silc_client_command_call(client, conn, NULL,
229 "NICK", conn->internal->params.nickname, NULL);
231 /* Issue INFO command to fetch the real server name and server
232 information and other stuff. */
233 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
234 silc_client_register_command_called, NULL,
235 1, 2, silc_buffer_data(conn->internal->remote_idp),
236 silc_buffer_len(conn->internal->remote_idp));
238 /* Call connection callback. We are now inside SILC network. */
239 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
240 conn->callback_context);
242 conn->internal->registering = FALSE;
243 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
244 silc_client_connect_timeout, conn);
246 return SILC_FSM_FINISH;
249 /* Error registering to network */
251 SILC_FSM_STATE(silc_client_st_register_error)
253 SilcClientConnection conn = fsm_context;
254 SilcClient client = conn->client;
256 SILC_LOG_DEBUG(("Error registering to network"));
258 /* Signal to close connection */
259 if (!conn->internal->disconnected) {
260 conn->internal->disconnected = TRUE;
261 SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
264 /* Call connect callback */
265 if (conn->internal->callback_called)
266 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
267 conn->callback_context);
268 conn->internal->callback_called = TRUE;
270 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
271 silc_client_connect_timeout, conn);
273 return SILC_FSM_FINISH;
276 /************************* Resume detached session **************************/
278 /* Resume detached session */
280 SILC_FSM_STATE(silc_client_st_resume)
282 SilcClientConnection conn = fsm_context;
283 SilcClient client = conn->client;
284 SilcClientResumeSession resume;
288 SilcClientID client_id;
291 SILC_LOG_DEBUG(("Resuming detached session"));
293 resume = silc_calloc(1, sizeof(*resume));
296 silc_fsm_next(fsm, silc_client_st_resume_error);
297 return SILC_FSM_CONTINUE;
299 silc_fsm_set_state_context(fsm, resume);
301 silc_buffer_set(&resume->detach, conn->internal->params.detach_data,
302 conn->internal->params.detach_data_len);
303 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(&resume->detach),
304 silc_buffer_len(&resume->detach));
306 /* Take the old client ID from the detachment data */
307 ret = silc_buffer_unformat(&resume->detach,
309 SILC_STR_UI16_NSTRING_ALLOC(&resume->nickname,
311 SILC_STR_UI16_NSTRING(&id, &id_len),
312 SILC_STR_UI_INT(NULL),
313 SILC_STR_UI_INT(&resume->channel_count),
316 /** Malformed detach data */
317 SILC_LOG_DEBUG(("Malformed detachment data"));
318 silc_fsm_next(fsm, silc_client_st_resume_error);
319 return SILC_FSM_CONTINUE;
322 if (!silc_id_str2id(id, id_len, SILC_ID_CLIENT, &client_id,
323 sizeof(client_id))) {
325 SILC_LOG_DEBUG(("Malformed ID"));
326 silc_fsm_next(fsm, silc_client_st_resume_error);
327 return SILC_FSM_CONTINUE;
330 /* Generate authentication data that server will verify */
331 auth = silc_auth_public_key_auth_generate(conn->public_key,
334 conn->internal->hash,
335 &client_id, SILC_ID_CLIENT);
338 silc_fsm_next(fsm, silc_client_st_resume_error);
339 return SILC_FSM_CONTINUE;
342 /* Send RESUME_CLIENT packet to resume to network */
343 if (!silc_packet_send_va(conn->stream, SILC_PACKET_RESUME_CLIENT, 0,
344 SILC_STR_UI_SHORT(id_len),
345 SILC_STR_DATA(id, id_len),
346 SILC_STR_DATA(silc_buffer_data(auth),
347 silc_buffer_len(auth)),
349 /** Error sending packet */
350 SILC_LOG_DEBUG(("Error sending packet"));
351 silc_fsm_next(fsm, silc_client_st_resume_error);
352 return SILC_FSM_CONTINUE;
355 /** Wait for new ID */
356 conn->internal->registering = TRUE;
357 silc_fsm_next_later(fsm, silc_client_st_resume_resolve_channels, 15, 0);
358 return SILC_FSM_WAIT;
361 /* Resolve the old session information, user mode and joined channels. */
363 SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
365 SilcClientConnection conn = fsm_context;
366 SilcClient client = conn->client;
367 SilcClientResumeSession resume = state_context;
368 SilcUInt32 *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
369 unsigned char **res_argv = NULL;
372 if (conn->internal->disconnected) {
374 silc_fsm_next(fsm, silc_client_st_resume_error);
375 return SILC_FSM_CONTINUE;
378 if (!conn->local_id) {
379 /** Timeout, ID not received */
380 conn->internal->registering = FALSE;
381 silc_fsm_next(fsm, silc_client_st_resume_error);
382 return SILC_FSM_CONTINUE;
385 /** Wait for channels */
386 silc_fsm_next(fsm, silc_client_st_resume_resolve_cmodes);
388 /* Change our nickname */
389 silc_client_change_nickname(client, conn, conn->local_entry,
390 resume->nickname, NULL, NULL, 0);
392 /* Send UMODE command to get our own user mode in the network */
393 SILC_LOG_DEBUG(("Resolving user mode"));
394 silc_client_command_send(client, conn, SILC_COMMAND_UMODE,
395 silc_client_register_command_called, NULL,
396 1, 1, silc_buffer_data(conn->internal->local_idp),
397 silc_buffer_len(conn->internal->local_idp));
399 if (!resume->channel_count)
400 return SILC_FSM_YIELD;
402 /* Send IDENTIFY command for all channels we know about. These are the
403 channels we've joined to according our detachment data. */
404 for (i = 0; i < resume->channel_count; i++) {
405 SilcChannelEntry channel;
409 SilcChannelID channel_id;
412 if (silc_buffer_unformat(&resume->detach,
414 SILC_STR_UI16_NSTRING(&name, NULL),
415 SILC_STR_UI16_NSTRING(&chid, &chid_len),
416 SILC_STR_UI_INT(NULL),
420 if (!silc_id_str2id(chid, chid_len, SILC_ID_CHANNEL, &channel_id,
423 idp = silc_id_payload_encode_data(chid, chid_len, SILC_ID_CHANNEL);
427 /* Add the channel to cache */
428 channel = silc_client_get_channel_by_id(client, conn, &channel_id);
430 silc_client_add_channel(client, conn, name, 0, &channel_id);
432 res_argv = silc_realloc(res_argv, sizeof(*res_argv) * (res_argc + 1));
433 res_argv_lens = silc_realloc(res_argv_lens, sizeof(*res_argv_lens) *
435 res_argv_types = silc_realloc(res_argv_types, sizeof(*res_argv_types) *
437 res_argv[res_argc] = silc_buffer_steal(idp, &res_argv_lens[res_argc]);
438 res_argv_types[res_argc] = res_argc + 5;
440 silc_buffer_free(idp);
443 /* Send IDENTIFY command */
444 SILC_LOG_DEBUG(("Resolving joined channels"));
445 silc_client_command_send_argv(client, conn, SILC_COMMAND_IDENTIFY,
446 silc_client_resume_continue, conn,
447 res_argc, res_argv, res_argv_lens,
450 for (i = 0; i < resume->channel_count; i++)
451 silc_free(res_argv[i]);
453 silc_free(res_argv_lens);
454 silc_free(res_argv_types);
456 return SILC_FSM_WAIT;
459 /* Resolve joined channel modes, users and topics. */
461 SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
463 SilcClientConnection conn = fsm_context;
464 SilcClient client = conn->client;
465 SilcClientResumeSession resume = state_context;
466 SilcIDCacheEntry entry;
467 SilcChannelEntry channel;
471 if (conn->internal->disconnected) {
473 silc_fsm_next(fsm, silc_client_st_resume_error);
474 return SILC_FSM_CONTINUE;
477 SILC_LOG_DEBUG(("Resolving channel details"));
479 /** Wait for channel modes */
480 silc_fsm_next(fsm, silc_client_st_resume_completed);
482 if (!silc_idcache_get_all(conn->internal->channel_cache, &channels))
483 return SILC_FSM_YIELD;
485 /* Resolve channels' mode, users and topic */
486 resume->channel_count = silc_list_count(channels) * 3;
487 silc_list_start(channels);
488 while ((entry = silc_list_get(channels))) {
489 channel = entry->context;
490 idp = silc_id_payload_encode(&channel->id, SILC_ID_CHANNEL);
494 silc_client_command_send(client, conn, SILC_COMMAND_CMODE,
495 silc_client_resume_continue, conn, 1,
496 1, silc_buffer_data(idp),
497 silc_buffer_len(idp));
498 silc_client_command_send(client, conn, SILC_COMMAND_USERS,
499 silc_client_resume_continue, conn, 1,
500 1, silc_buffer_data(idp),
501 silc_buffer_len(idp));
502 silc_client_command_send(client, conn, SILC_COMMAND_TOPIC,
503 silc_client_resume_continue, conn, 1,
504 1, silc_buffer_data(idp),
505 silc_buffer_len(idp));
506 silc_buffer_free(idp);
509 return SILC_FSM_WAIT;
512 /* Resuming completed */
514 SILC_FSM_STATE(silc_client_st_resume_completed)
516 SilcClientConnection conn = fsm_context;
517 SilcClient client = conn->client;
518 SilcClientResumeSession resume = state_context;
519 SilcIDCacheEntry entry;
520 SilcChannelEntry channel;
523 if (conn->internal->disconnected) {
525 silc_fsm_next(fsm, silc_client_st_resume_error);
526 return SILC_FSM_CONTINUE;
529 if (resume->channel_count > 0) {
530 resume->channel_count--;
531 if (resume->channel_count)
532 return SILC_FSM_WAIT;
535 SILC_LOG_DEBUG(("Resuming completed"));
537 /* Issue IDENTIFY command for itself to get resolved hostname
538 correctly from server. */
539 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
540 silc_client_register_command_called, NULL,
541 1, 5, silc_buffer_data(conn->internal->local_idp),
542 silc_buffer_len(conn->internal->local_idp));
544 /* Issue INFO command to fetch the real server name and server
545 information and other stuff. */
546 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
547 silc_client_register_command_called, NULL,
548 1, 2, silc_buffer_data(conn->internal->remote_idp),
549 silc_buffer_len(conn->internal->remote_idp));
551 /* Call connection callback. We have now resumed to SILC network. */
552 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS_RESUME, 0, NULL,
553 conn->callback_context);
555 /* Call UMODE command reply. */
556 if (conn->local_entry->mode)
557 silc_client_resume_command_callback(client, conn, SILC_COMMAND_UMODE,
558 conn->local_entry->mode);
560 /* Call NICK command reply. */
561 silc_client_resume_command_callback(client, conn, SILC_COMMAND_NICK,
563 conn->local_entry->nickname,
564 &conn->local_entry->id);
566 /* Call JOIN command replies for all joined channel */
567 silc_idcache_get_all(conn->internal->channel_cache, &channels);
568 silc_list_start(channels);
569 while ((entry = silc_list_get(channels))) {
570 SilcHashTableList htl;
571 const char *cipher, *hmac;
573 channel = entry->context;
574 cipher = (channel->internal.send_key ?
575 silc_cipher_get_name(channel->internal.send_key) : NULL);
576 hmac = (channel->internal.hmac ?
577 silc_hmac_get_name(channel->internal.hmac) : NULL);
578 silc_hash_table_list(channel->user_list, &htl);
579 silc_client_resume_command_callback(client, conn, SILC_COMMAND_JOIN,
580 channel->channel_name, channel,
581 channel->mode, &htl, channel->topic,
582 cipher, hmac, channel->founder_key,
583 channel->channel_pubkeys,
584 channel->user_limit);
585 silc_hash_table_list_reset(&htl);
588 conn->internal->registering = FALSE;
589 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
590 silc_client_connect_timeout, conn);
591 silc_free(resume->nickname);
594 return SILC_FSM_FINISH;
597 /* Error resuming to network */
599 SILC_FSM_STATE(silc_client_st_resume_error)
601 SilcClientConnection conn = fsm_context;
602 SilcClient client = conn->client;
603 SilcClientResumeSession resume = state_context;
605 if (conn->internal->disconnected) {
607 silc_free(resume->nickname);
610 return SILC_FSM_FINISH;
613 SILC_LOG_DEBUG(("Error resuming to network"));
615 /* Signal to close connection */
616 if (!conn->internal->disconnected) {
617 conn->internal->disconnected = TRUE;
618 SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
621 /* Call connect callback */
622 if (conn->internal->callback_called)
623 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
624 conn->callback_context);
625 conn->internal->callback_called = TRUE;
627 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
628 silc_client_connect_timeout, conn);
631 silc_free(resume->nickname);
635 return SILC_FSM_FINISH;
638 /* Generates the session detachment data. This data can be used later
639 to resume back to the server. */
641 SilcBuffer silc_client_get_detach_data(SilcClient client,
642 SilcClientConnection conn)
645 SilcHashTableList htl;
647 unsigned char id[64];
651 SILC_LOG_DEBUG(("Creating detachment data"));
653 ch_count = silc_hash_table_count(conn->local_entry->channels);
654 silc_id_id2str(conn->local_id, SILC_ID_CLIENT, id, sizeof(id), &id_len);
656 /* Save the nickname, Client ID and user mode in SILC network */
657 detach = silc_buffer_alloc(0);
661 silc_buffer_format(detach,
663 SILC_STR_UI_SHORT(strlen(conn->local_entry->nickname)),
664 SILC_STR_DATA(conn->local_entry->nickname,
665 strlen(conn->local_entry->nickname)),
666 SILC_STR_UI_SHORT(id_len),
667 SILC_STR_DATA(id, id_len),
668 SILC_STR_UI_INT(conn->local_entry->mode),
669 SILC_STR_UI_INT(ch_count),
672 silc_buffer_free(detach);
676 /* Save all joined channels */
677 silc_hash_table_list(conn->local_entry->channels, &htl);
678 while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
679 unsigned char chid[32];
682 silc_id_id2str(&chu->channel->id, SILC_ID_CHANNEL, chid, sizeof(chid),
684 silc_buffer_format(detach,
686 SILC_STR_UI_SHORT(strlen(chu->channel->channel_name)),
687 SILC_STR_DATA(chu->channel->channel_name,
688 strlen(chu->channel->channel_name)),
689 SILC_STR_UI_SHORT(chid_len),
690 SILC_STR_DATA(chid, chid_len),
691 SILC_STR_UI_INT(chu->channel->mode),
694 silc_hash_table_list_reset(&htl);
696 silc_buffer_start(detach);
697 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(detach),
698 silc_buffer_len(detach));