5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2006 - 2007 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;
35 SilcUInt32 channel_count;
36 } *SilcClientResumeSession;
38 /************************ Static utility functions **************************/
40 /* Continues resuming after resolving. Continue after last reply. */
43 silc_client_resume_continue(SilcClient client,
44 SilcClientConnection conn,
51 if (status == SILC_STATUS_OK || status == SILC_STATUS_LIST_END ||
52 SILC_STATUS_IS_ERROR(status)) {
53 silc_fsm_continue(&conn->internal->event_thread);
60 /* Function used to call command replies back to application in resuming. */
63 silc_client_resume_command_callback(SilcClient client,
64 SilcClientConnection conn,
65 SilcCommand command, ...)
68 va_start(ap, command);
69 client->internal->ops->command_reply(client, conn, command,
70 SILC_STATUS_OK, SILC_STATUS_OK, ap);
74 /* Resume authentication data generation callback */
76 static void silc_client_resume_auth_generated(const SilcBuffer data,
79 SilcClientConnection conn = context;
80 SilcClientResumeSession resume =
81 silc_fsm_get_state_context(&conn->internal->event_thread);
84 silc_fsm_next(&conn->internal->event_thread, silc_client_st_resume_error);
86 resume->auth = silc_buffer_copy(data);
88 SILC_FSM_CALL_CONTINUE_SYNC(&conn->internal->event_thread);
92 /****************************** NEW_ID packet *******************************/
94 /* Received new ID packet from server during registering to SILC network */
96 SILC_FSM_STATE(silc_client_new_id)
98 SilcClientConnection conn = fsm_context;
99 SilcClient client = conn->client;
100 SilcPacket packet = state_context;
107 SILC_LOG_DEBUG(("New ID received from server"));
109 if (!silc_id_payload_parse_id(silc_buffer_data(&packet->buffer),
110 silc_buffer_len(&packet->buffer), &id))
113 SILC_LOG_DEBUG(("New ID %s", silc_id_render(&id.u.client_id,
116 /* From SILC protocol version 1.3, nickname is in NEW_CLIENT packet */
117 if (conn->internal->remote_version >= 13)
118 nick = (conn->internal->params.nickname ?
119 conn->internal->params.nickname : client->username);
121 nick = client->username;
123 /* Create local client entry */
124 conn->local_entry = silc_client_add_client(client, conn, nick,
128 if (!conn->local_entry)
131 /* Save the ID. Take reference to conn->local_id. */
132 conn->local_id = &conn->local_entry->id;
133 conn->internal->local_idp = silc_buffer_copy(&packet->buffer);
136 if (packet->src_id_len) {
137 conn->internal->remote_idp =
138 silc_id_payload_encode_data(packet->src_id,
140 packet->src_id_type);
141 if (!conn->internal->remote_idp)
143 silc_id_payload_parse_id(silc_buffer_data(conn->internal->remote_idp),
144 silc_buffer_len(conn->internal->remote_idp),
148 /* Set IDs to the packet stream */
149 silc_packet_set_ids(conn->stream, SILC_ID_CLIENT, conn->local_id,
150 conn->remote_id.type, SILC_ID_GET_ID(conn->remote_id));
152 /* Signal connection that new ID was received so it can continue
153 with the registering. */
154 if (conn->internal->registering)
155 silc_fsm_continue_sync(&conn->internal->event_thread);
158 /** Packet processed */
159 silc_packet_free(packet);
160 return SILC_FSM_FINISH;
164 /************************ Register to SILC network **************************/
166 /* Register to network */
168 SILC_FSM_STATE(silc_client_st_register)
170 SilcClientConnection conn = fsm_context;
171 SilcClient client = conn->client;
174 SILC_LOG_DEBUG(("Register to network"));
176 /* From SILC protocol version 1.3, nickname is in NEW_CLIENT packet */
177 if (conn->internal->remote_version >= 13)
178 nick = (conn->internal->params.nickname ?
179 conn->internal->params.nickname : client->username);
181 /* Send NEW_CLIENT packet to register to network */
182 if (!silc_packet_send_va(conn->stream, SILC_PACKET_NEW_CLIENT, 0,
183 SILC_STR_UI_SHORT(strlen(client->username)),
184 SILC_STR_DATA(client->username,
185 strlen(client->username)),
186 SILC_STR_UI_SHORT(strlen(client->realname)),
187 SILC_STR_DATA(client->realname,
188 strlen(client->realname)),
189 SILC_STR_UI_SHORT(nick ? strlen(nick) : 0),
190 SILC_STR_DATA(nick, nick ? strlen(nick) : 0),
192 /** Error sending packet */
193 silc_fsm_next(fsm, silc_client_st_register_error);
194 return SILC_FSM_CONTINUE;
197 /** Wait for new ID */
198 conn->internal->registering = TRUE;
199 silc_fsm_next_later(fsm, silc_client_st_register_complete,
200 conn->internal->retry_timer, 0);
201 return SILC_FSM_WAIT;
204 /* Wait for NEW_ID packet to arrive */
206 SILC_FSM_STATE(silc_client_st_register_complete)
208 SilcClientConnection conn = fsm_context;
209 SilcClient client = conn->client;
211 if (conn->internal->disconnected) {
213 silc_fsm_next(fsm, silc_client_st_register_error);
214 return SILC_FSM_CONTINUE;
217 if (!conn->local_id) {
218 if (conn->internal->retry_count++ >= SILC_CLIENT_RETRY_COUNT) {
219 /** Timeout, ID not received */
220 conn->internal->registering = FALSE;
221 conn->internal->retry_count = 0;
222 conn->internal->retry_timer = SILC_CLIENT_RETRY_MIN;
223 silc_fsm_next(fsm, silc_client_st_register_error);
224 return SILC_FSM_CONTINUE;
227 /** Resend registering packet */
228 silc_fsm_next(fsm, silc_client_st_register);
229 conn->internal->retry_timer = ((conn->internal->retry_timer *
230 SILC_CLIENT_RETRY_MUL) +
231 (silc_rng_get_rn16(client->rng) %
232 SILC_CLIENT_RETRY_RAND));
233 return SILC_FSM_CONTINUE;
236 SILC_LOG_DEBUG(("Registered to network"));
238 /* Issue IDENTIFY command for itself to get resolved hostname
239 correctly from server. */
240 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
241 silc_client_command_called_dummy, NULL,
242 1, 5, silc_buffer_data(conn->internal->local_idp),
243 silc_buffer_len(conn->internal->local_idp));
245 /* With SILC protocol version 1.2 call NICK command if the nickname was
246 set by the application. */
247 if (conn->internal->params.nickname && conn->internal->remote_version < 13 &&
248 !silc_utf8_strcasecmp(conn->internal->params.nickname, client->username))
249 silc_client_command_call(client, conn, NULL,
250 "NICK", conn->internal->params.nickname, NULL);
252 /* Issue INFO command to fetch the real server name and server
253 information and other stuff. */
254 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
255 silc_client_command_called_dummy, NULL,
256 1, 2, silc_buffer_data(conn->internal->remote_idp),
257 silc_buffer_len(conn->internal->remote_idp));
259 /* Call connection callback. We are now inside SILC network. */
260 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
261 conn->callback_context);
263 conn->internal->registering = FALSE;
264 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
265 silc_client_connect_timeout, conn);
266 silc_async_free(conn->internal->cop);
267 conn->internal->cop = NULL;
269 return SILC_FSM_FINISH;
272 /* Error registering to network */
274 SILC_FSM_STATE(silc_client_st_register_error)
276 SilcClientConnection conn = fsm_context;
278 SILC_LOG_DEBUG(("Error registering to network"));
280 /* Signal to close connection */
281 conn->internal->status = SILC_CLIENT_CONN_ERROR;
282 if (!conn->internal->disconnected) {
283 conn->internal->disconnected = TRUE;
284 SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
287 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
288 silc_client_connect_timeout, conn);
290 return SILC_FSM_FINISH;
293 /************************* Resume detached session **************************/
295 /* Resume detached session */
297 SILC_FSM_STATE(silc_client_st_resume)
299 SilcClientConnection conn = fsm_context;
300 SilcClient client = conn->client;
301 SilcClientResumeSession resume;
304 SilcClientID client_id;
307 SILC_LOG_DEBUG(("Resuming detached session"));
309 resume = silc_calloc(1, sizeof(*resume));
312 silc_fsm_next(fsm, silc_client_st_resume_error);
313 return SILC_FSM_CONTINUE;
315 silc_fsm_set_state_context(fsm, resume);
317 silc_buffer_set(&resume->detach, conn->internal->params.detach_data,
318 conn->internal->params.detach_data_len);
319 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(&resume->detach),
320 silc_buffer_len(&resume->detach));
322 /* Take the old client ID from the detachment data */
323 ret = silc_buffer_unformat(&resume->detach,
325 SILC_STR_UI16_NSTRING_ALLOC(&resume->nickname,
327 SILC_STR_UI16_NSTRING(&id, &id_len),
328 SILC_STR_UI_INT(NULL),
329 SILC_STR_UI_INT(&resume->channel_count),
332 /** Malformed detach data */
333 SILC_LOG_DEBUG(("Malformed detachment data"));
334 silc_fsm_next(fsm, silc_client_st_resume_error);
335 return SILC_FSM_CONTINUE;
338 if (!silc_id_str2id(id, id_len, SILC_ID_CLIENT, &client_id,
339 sizeof(client_id))) {
341 SILC_LOG_DEBUG(("Malformed ID"));
342 silc_fsm_next(fsm, silc_client_st_resume_error);
343 return SILC_FSM_CONTINUE;
346 resume->id_len = id_len;
348 /* Generate authentication data that server will verify */
349 silc_fsm_next(fsm, silc_client_st_resume_send);
350 SILC_FSM_CALL(silc_auth_public_key_auth_generate(
351 conn->public_key, conn->private_key,
352 client->rng, conn->internal->hash,
353 &client_id, SILC_ID_CLIENT,
354 silc_client_resume_auth_generated, conn));
358 /* Send RESUME_CLIENT packet */
360 SILC_FSM_STATE(silc_client_st_resume_send)
362 SilcClientConnection conn = fsm_context;
363 SilcClientResumeSession resume = state_context;
365 SILC_LOG_DEBUG(("Send RESUME_CLIENT packet"));
367 /* Send RESUME_CLIENT packet to resume to network */
368 if (!silc_packet_send_va(conn->stream, SILC_PACKET_RESUME_CLIENT, 0,
369 SILC_STR_UI_SHORT(resume->id_len),
370 SILC_STR_DATA(resume->id, resume->id_len),
371 SILC_STR_DATA(silc_buffer_data(resume->auth),
372 silc_buffer_len(resume->auth)),
374 /** Error sending packet */
375 SILC_LOG_DEBUG(("Error sending packet"));
376 silc_fsm_next(fsm, silc_client_st_resume_error);
377 return SILC_FSM_CONTINUE;
380 /** Wait for new ID */
381 conn->internal->registering = TRUE;
382 silc_fsm_next_later(fsm, silc_client_st_resume_resolve_channels, 15, 0);
383 return SILC_FSM_WAIT;
386 /* Resolve the old session information, user mode and joined channels. */
388 SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
390 SilcClientConnection conn = fsm_context;
391 SilcClient client = conn->client;
392 SilcClientResumeSession resume = state_context;
393 SilcUInt32 *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
394 unsigned char **res_argv = NULL;
397 if (conn->internal->disconnected) {
399 silc_fsm_next(fsm, silc_client_st_resume_error);
400 return SILC_FSM_CONTINUE;
403 if (!conn->local_id) {
404 /** Timeout, ID not received */
405 conn->internal->registering = FALSE;
406 silc_fsm_next(fsm, silc_client_st_resume_error);
407 return SILC_FSM_CONTINUE;
410 /** Wait for channels */
411 silc_fsm_next(fsm, silc_client_st_resume_resolve_cmodes);
413 /* Change our nickname */
414 silc_client_change_nickname(client, conn, conn->local_entry,
415 resume->nickname, NULL, NULL, 0);
417 /* Send UMODE command to get our own user mode in the network */
418 SILC_LOG_DEBUG(("Resolving user mode"));
419 silc_client_command_send(client, conn, SILC_COMMAND_UMODE,
420 silc_client_command_called_dummy, NULL,
421 1, 1, silc_buffer_data(conn->internal->local_idp),
422 silc_buffer_len(conn->internal->local_idp));
424 if (!resume->channel_count)
425 return SILC_FSM_YIELD;
427 /* Send IDENTIFY command for all channels we know about. These are the
428 channels we've joined to according our detachment data. */
429 for (i = 0; i < resume->channel_count; i++) {
430 SilcChannelEntry channel;
434 SilcChannelID channel_id;
437 if (silc_buffer_unformat(&resume->detach,
439 SILC_STR_UI16_NSTRING(&name, NULL),
440 SILC_STR_UI16_NSTRING(&chid, &chid_len),
441 SILC_STR_UI_INT(NULL),
445 if (!silc_id_str2id(chid, chid_len, SILC_ID_CHANNEL, &channel_id,
448 idp = silc_id_payload_encode_data(chid, chid_len, SILC_ID_CHANNEL);
452 /* Add the channel to cache */
453 channel = silc_client_get_channel_by_id(client, conn, &channel_id);
455 silc_client_add_channel(client, conn, name, 0, &channel_id);
457 res_argv = silc_realloc(res_argv, sizeof(*res_argv) * (res_argc + 1));
458 res_argv_lens = silc_realloc(res_argv_lens, sizeof(*res_argv_lens) *
460 res_argv_types = silc_realloc(res_argv_types, sizeof(*res_argv_types) *
462 res_argv[res_argc] = silc_buffer_steal(idp, &res_argv_lens[res_argc]);
463 res_argv_types[res_argc] = res_argc + 5;
465 silc_buffer_free(idp);
468 /* Send IDENTIFY command */
469 SILC_LOG_DEBUG(("Resolving joined channels"));
470 silc_client_command_send_argv(client, conn, SILC_COMMAND_IDENTIFY,
471 silc_client_resume_continue, conn,
472 res_argc, res_argv, res_argv_lens,
475 for (i = 0; i < resume->channel_count; i++)
476 silc_free(res_argv[i]);
478 silc_free(res_argv_lens);
479 silc_free(res_argv_types);
481 return SILC_FSM_WAIT;
484 /* Resolve joined channel modes, users and topics. */
486 SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
488 SilcClientConnection conn = fsm_context;
489 SilcClient client = conn->client;
490 SilcClientResumeSession resume = state_context;
491 SilcIDCacheEntry entry;
492 SilcChannelEntry channel;
496 if (conn->internal->disconnected) {
498 silc_fsm_next(fsm, silc_client_st_resume_error);
499 return SILC_FSM_CONTINUE;
502 SILC_LOG_DEBUG(("Resolving channel details"));
504 /** Wait for channel modes */
505 silc_fsm_next(fsm, silc_client_st_resume_completed);
507 if (!silc_idcache_get_all(conn->internal->channel_cache, &channels))
508 return SILC_FSM_YIELD;
510 /* Resolve channels' mode, users and topic */
511 resume->channel_count = silc_list_count(channels) * 3;
512 silc_list_start(channels);
513 while ((entry = silc_list_get(channels))) {
514 channel = entry->context;
515 idp = silc_id_payload_encode(&channel->id, SILC_ID_CHANNEL);
519 silc_client_command_send(client, conn, SILC_COMMAND_CMODE,
520 silc_client_resume_continue, conn, 1,
521 1, silc_buffer_data(idp),
522 silc_buffer_len(idp));
523 silc_client_command_send(client, conn, SILC_COMMAND_USERS,
524 silc_client_resume_continue, conn, 1,
525 1, silc_buffer_data(idp),
526 silc_buffer_len(idp));
527 silc_client_command_send(client, conn, SILC_COMMAND_TOPIC,
528 silc_client_resume_continue, conn, 1,
529 1, silc_buffer_data(idp),
530 silc_buffer_len(idp));
531 silc_buffer_free(idp);
534 return SILC_FSM_WAIT;
537 /* Resuming completed */
539 SILC_FSM_STATE(silc_client_st_resume_completed)
541 SilcClientConnection conn = fsm_context;
542 SilcClient client = conn->client;
543 SilcClientResumeSession resume = state_context;
544 SilcIDCacheEntry entry;
545 SilcChannelEntry channel;
548 if (conn->internal->disconnected) {
550 silc_fsm_next(fsm, silc_client_st_resume_error);
551 return SILC_FSM_CONTINUE;
554 if (resume->channel_count > 0) {
555 resume->channel_count--;
556 if (resume->channel_count)
557 return SILC_FSM_WAIT;
560 SILC_LOG_DEBUG(("Resuming completed"));
562 /* Issue IDENTIFY command for itself to get resolved hostname
563 correctly from server. */
564 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
565 silc_client_command_called_dummy, NULL,
566 1, 5, silc_buffer_data(conn->internal->local_idp),
567 silc_buffer_len(conn->internal->local_idp));
569 /* Issue INFO command to fetch the real server name and server
570 information and other stuff. */
571 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
572 silc_client_command_called_dummy, NULL,
573 1, 2, silc_buffer_data(conn->internal->remote_idp),
574 silc_buffer_len(conn->internal->remote_idp));
576 /* Call connection callback. We have now resumed to SILC network. */
577 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS_RESUME, 0, NULL,
578 conn->callback_context);
580 /* Call UMODE command reply. */
581 if (conn->local_entry->mode)
582 silc_client_resume_command_callback(client, conn, SILC_COMMAND_UMODE,
583 conn->local_entry->mode);
585 /* Call NICK command reply. */
586 silc_client_resume_command_callback(client, conn, SILC_COMMAND_NICK,
588 conn->local_entry->nickname,
589 &conn->local_entry->id);
591 /* Call JOIN command replies for all joined channel */
592 silc_idcache_get_all(conn->internal->channel_cache, &channels);
593 silc_list_start(channels);
594 while ((entry = silc_list_get(channels))) {
595 SilcHashTableList htl;
596 const char *cipher, *hmac;
598 channel = entry->context;
599 cipher = (channel->internal.send_key ?
600 silc_cipher_get_name(channel->internal.send_key) : NULL);
601 hmac = (channel->internal.hmac ?
602 silc_hmac_get_name(channel->internal.hmac) : NULL);
603 silc_hash_table_list(channel->user_list, &htl);
604 silc_client_resume_command_callback(client, conn, SILC_COMMAND_JOIN,
605 channel->channel_name, channel,
606 channel->mode, &htl, channel->topic,
607 cipher, hmac, channel->founder_key,
608 channel->channel_pubkeys,
609 channel->user_limit);
610 silc_hash_table_list_reset(&htl);
613 conn->internal->registering = FALSE;
614 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
615 silc_client_connect_timeout, conn);
616 silc_free(resume->nickname);
618 silc_async_free(conn->internal->cop);
619 conn->internal->cop = NULL;
621 return SILC_FSM_FINISH;
624 /* Error resuming to network */
626 SILC_FSM_STATE(silc_client_st_resume_error)
628 SilcClientConnection conn = fsm_context;
629 SilcClientResumeSession resume = state_context;
631 if (conn->internal->disconnected) {
633 silc_free(resume->nickname);
636 return SILC_FSM_FINISH;
639 SILC_LOG_DEBUG(("Error resuming to network"));
641 /* Signal to close connection */
642 conn->internal->status = SILC_CLIENT_CONN_ERROR;
643 if (!conn->internal->disconnected) {
644 conn->internal->disconnected = TRUE;
645 SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
648 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
649 silc_client_connect_timeout, conn);
652 silc_free(resume->nickname);
656 return SILC_FSM_FINISH;
659 /* Generates the session detachment data. This data can be used later
660 to resume back to the server. */
662 SilcBuffer silc_client_get_detach_data(SilcClient client,
663 SilcClientConnection conn)
666 SilcHashTableList htl;
668 unsigned char id[64];
672 SILC_LOG_DEBUG(("Creating detachment data"));
674 ch_count = silc_hash_table_count(conn->local_entry->channels);
675 silc_id_id2str(conn->local_id, SILC_ID_CLIENT, id, sizeof(id), &id_len);
677 /* Save the nickname, Client ID and user mode in SILC network */
678 detach = silc_buffer_alloc(0);
682 silc_buffer_format(detach,
684 SILC_STR_UI_SHORT(strlen(conn->local_entry->nickname)),
685 SILC_STR_DATA(conn->local_entry->nickname,
686 strlen(conn->local_entry->nickname)),
687 SILC_STR_UI_SHORT(id_len),
688 SILC_STR_DATA(id, id_len),
689 SILC_STR_UI_INT(conn->local_entry->mode),
690 SILC_STR_UI_INT(ch_count),
693 silc_buffer_free(detach);
697 /* Save all joined channels */
698 silc_hash_table_list(conn->local_entry->channels, &htl);
699 while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
700 unsigned char chid[32];
703 silc_id_id2str(&chu->channel->id, SILC_ID_CHANNEL, chid, sizeof(chid),
705 silc_buffer_format(detach,
707 SILC_STR_UI_SHORT(strlen(chu->channel->channel_name)),
708 SILC_STR_DATA(chu->channel->channel_name,
709 strlen(chu->channel->channel_name)),
710 SILC_STR_UI_SHORT(chid_len),
711 SILC_STR_DATA(chid, chid_len),
712 SILC_STR_UI_INT(chu->channel->mode),
715 silc_hash_table_list_reset(&htl);
717 silc_buffer_start(detach);
718 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(detach),
719 silc_buffer_len(detach));