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_SEMA_POST(&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 /* Change our nickname */
386 silc_client_change_nickname(client, conn, conn->local_entry,
387 resume->nickname, NULL, NULL, 0);
389 /* Send UMODE command to get our own user mode in the network */
390 SILC_LOG_DEBUG(("Resolving user mode"));
391 silc_client_command_send(client, conn, SILC_COMMAND_UMODE,
392 silc_client_register_command_called, NULL,
393 1, 1, silc_buffer_data(conn->internal->local_idp),
394 silc_buffer_len(conn->internal->local_idp));
396 /* Send IDENTIFY command for all channels we know about. These are the
397 channels we've joined to according our detachment data. */
398 for (i = 0; i < resume->channel_count; i++) {
399 SilcChannelEntry channel;
403 SilcChannelID channel_id;
406 if (silc_buffer_unformat(&resume->detach,
408 SILC_STR_UI16_NSTRING(&name, NULL),
409 SILC_STR_UI16_NSTRING(&chid, &chid_len),
410 SILC_STR_UI_INT(NULL),
414 if (!silc_id_str2id(chid, chid_len, SILC_ID_CHANNEL, &channel_id,
417 idp = silc_id_payload_encode_data(chid, chid_len, SILC_ID_CHANNEL);
421 /* Add the channel to cache */
422 channel = silc_client_get_channel_by_id(client, conn, &channel_id);
424 silc_client_add_channel(client, conn, name, 0, &channel_id);
426 res_argv = silc_realloc(res_argv, sizeof(*res_argv) * (res_argc + 1));
427 res_argv_lens = silc_realloc(res_argv_lens, sizeof(*res_argv_lens) *
429 res_argv_types = silc_realloc(res_argv_types, sizeof(*res_argv_types) *
431 res_argv[res_argc] = silc_buffer_steal(idp, &res_argv_lens[res_argc]);
432 res_argv_types[res_argc] = res_argc + 5;
434 silc_buffer_free(idp);
437 /* Send IDENTIFY command */
438 SILC_LOG_DEBUG(("Resolving joined channels"));
439 silc_client_command_send_argv(client, conn, SILC_COMMAND_IDENTIFY,
440 silc_client_resume_continue, conn,
441 res_argc, res_argv, res_argv_lens,
444 for (i = 0; i < resume->channel_count; i++)
445 silc_free(res_argv[i]);
447 silc_free(res_argv_lens);
448 silc_free(res_argv_types);
450 /** Wait for channels */
451 silc_fsm_next(fsm, silc_client_st_resume_resolve_cmodes);
452 return SILC_FSM_WAIT;
455 /* Resolve joined channel modes, users and topics. */
457 SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
459 SilcClientConnection conn = fsm_context;
460 SilcClient client = conn->client;
461 SilcClientResumeSession resume = state_context;
462 SilcIDCacheEntry entry;
463 SilcChannelEntry channel;
467 if (conn->internal->disconnected) {
469 silc_fsm_next(fsm, silc_client_st_resume_error);
470 return SILC_FSM_CONTINUE;
473 SILC_LOG_DEBUG(("Resolving channel details"));
475 /** Wait for channel modes */
476 silc_fsm_next(fsm, silc_client_st_resume_completed);
478 if (!silc_idcache_get_all(conn->internal->channel_cache, &channels))
479 return SILC_FSM_CONTINUE;
481 /* Resolve channels' mode, users and topic */
482 resume->channel_count = silc_list_count(channels) * 3;
483 silc_list_start(channels);
484 while ((entry = silc_list_get(channels))) {
485 channel = entry->context;
486 idp = silc_id_payload_encode(&channel->id, SILC_ID_CHANNEL);
490 silc_client_command_send(client, conn, SILC_COMMAND_CMODE,
491 silc_client_resume_continue, conn, 1,
492 1, silc_buffer_data(idp),
493 silc_buffer_len(idp));
494 silc_client_command_send(client, conn, SILC_COMMAND_USERS,
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_TOPIC,
499 silc_client_resume_continue, conn, 1,
500 1, silc_buffer_data(idp),
501 silc_buffer_len(idp));
502 silc_buffer_free(idp);
505 return SILC_FSM_WAIT;
508 /* Resuming completed */
510 SILC_FSM_STATE(silc_client_st_resume_completed)
512 SilcClientConnection conn = fsm_context;
513 SilcClient client = conn->client;
514 SilcClientResumeSession resume = state_context;
515 SilcIDCacheEntry entry;
516 SilcChannelEntry channel;
519 if (conn->internal->disconnected) {
521 silc_fsm_next(fsm, silc_client_st_resume_error);
522 return SILC_FSM_CONTINUE;
525 if (resume->channel_count > 0) {
526 resume->channel_count--;
527 if (resume->channel_count)
528 return SILC_FSM_WAIT;
531 SILC_LOG_DEBUG(("Resuming completed"));
533 /* Issue IDENTIFY command for itself to get resolved hostname
534 correctly from server. */
535 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
536 silc_client_register_command_called, NULL,
537 1, 5, silc_buffer_data(conn->internal->local_idp),
538 silc_buffer_len(conn->internal->local_idp));
540 /* Issue INFO command to fetch the real server name and server
541 information and other stuff. */
542 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
543 silc_client_register_command_called, NULL,
544 1, 2, silc_buffer_data(conn->internal->remote_idp),
545 silc_buffer_len(conn->internal->remote_idp));
547 /* Call connection callback. We have now resumed to SILC network. */
548 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS_RESUME, 0, NULL,
549 conn->callback_context);
551 /* Call NICK command reply. */
552 silc_client_resume_command_callback(client, conn, SILC_COMMAND_NICK,
554 conn->local_entry->nickname,
555 &conn->local_entry->id);
557 /* Call JOIN command replies for all joined channel */
558 silc_idcache_get_all(conn->internal->channel_cache, &channels);
559 silc_list_start(channels);
560 while ((entry = silc_list_get(channels))) {
561 SilcHashTableList htl;
562 const char *cipher, *hmac;
564 channel = entry->context;
565 cipher = (channel->internal.channel_key ?
566 silc_cipher_get_name(channel->internal.channel_key) : NULL);
567 hmac = (channel->internal.hmac ?
568 silc_hmac_get_name(channel->internal.hmac) : NULL);
569 silc_hash_table_list(channel->user_list, &htl);
570 silc_client_resume_command_callback(client, conn, SILC_COMMAND_JOIN,
571 channel->channel_name, channel,
572 channel->mode, &htl, channel->topic,
573 cipher, hmac, channel->founder_key,
574 channel->channel_pubkeys,
575 channel->user_limit);
576 silc_hash_table_list_reset(&htl);
579 conn->internal->registering = FALSE;
580 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
581 silc_client_connect_timeout, conn);
582 silc_free(resume->nickname);
585 return SILC_FSM_FINISH;
588 /* Error resuming to network */
590 SILC_FSM_STATE(silc_client_st_resume_error)
592 SilcClientConnection conn = fsm_context;
593 SilcClient client = conn->client;
594 SilcClientResumeSession resume = state_context;
596 if (conn->internal->disconnected) {
598 silc_free(resume->nickname);
601 return SILC_FSM_FINISH;
604 SILC_LOG_DEBUG(("Error resuming to network"));
606 /* Signal to close connection */
607 if (!conn->internal->disconnected) {
608 conn->internal->disconnected = TRUE;
609 SILC_FSM_SEMA_POST(&conn->internal->wait_event);
612 /* Call connect callback */
613 if (conn->internal->callback_called)
614 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
615 conn->callback_context);
616 conn->internal->callback_called = TRUE;
618 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
619 silc_client_connect_timeout, conn);
622 silc_free(resume->nickname);
626 return SILC_FSM_FINISH;
629 /* Generates the session detachment data. This data can be used later
630 to resume back to the server. */
632 SilcBuffer silc_client_get_detach_data(SilcClient client,
633 SilcClientConnection conn)
636 SilcHashTableList htl;
638 unsigned char id[64];
642 SILC_LOG_DEBUG(("Creating detachment data"));
644 ch_count = silc_hash_table_count(conn->local_entry->channels);
645 silc_id_id2str(conn->local_id, SILC_ID_CLIENT, id, sizeof(id), &id_len);
647 /* Save the nickname, Client ID and user mode in SILC network */
648 detach = silc_buffer_alloc(0);
652 silc_buffer_format(detach,
654 SILC_STR_UI_SHORT(strlen(conn->local_entry->nickname)),
655 SILC_STR_DATA(conn->local_entry->nickname,
656 strlen(conn->local_entry->nickname)),
657 SILC_STR_UI_SHORT(id_len),
658 SILC_STR_DATA(id, id_len),
659 SILC_STR_UI_INT(conn->local_entry->mode),
660 SILC_STR_UI_INT(ch_count),
663 silc_buffer_free(detach);
667 /* Save all joined channels */
668 silc_hash_table_list(conn->local_entry->channels, &htl);
669 while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
670 unsigned char chid[32];
673 silc_id_id2str(&chu->channel->id, SILC_ID_CHANNEL, chid, sizeof(chid),
675 silc_buffer_format(detach,
677 SILC_STR_UI_SHORT(strlen(chu->channel->channel_name)),
678 SILC_STR_DATA(chu->channel->channel_name,
679 strlen(chu->channel->channel_name)),
680 SILC_STR_UI_SHORT(chid_len),
681 SILC_STR_DATA(chid, chid_len),
682 SILC_STR_UI_INT(chu->channel->mode),
685 silc_hash_table_list_reset(&htl);
687 silc_buffer_start(detach);
688 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(detach),
689 silc_buffer_len(detach));