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;
32 SilcUInt32 channel_count;
33 } *SilcClientResumeSession;
35 /************************ Static utility functions **************************/
37 /* Continues resuming after resolving. Continue after last reply. */
40 silc_client_resume_continue(SilcClient client,
41 SilcClientConnection conn,
48 if (status == SILC_STATUS_OK || status == SILC_STATUS_LIST_END ||
49 SILC_STATUS_IS_ERROR(status)) {
50 silc_fsm_continue(&conn->internal->event_thread);
57 /* Function used to call command replies back to application in resuming. */
60 silc_client_resume_command_callback(SilcClient client,
61 SilcClientConnection conn,
62 SilcCommand command, ...)
65 va_start(ap, command);
66 client->internal->ops->command_reply(client, conn, command,
67 SILC_STATUS_OK, SILC_STATUS_OK, ap);
72 /****************************** NEW_ID packet *******************************/
74 /* Received new ID packet from server during registering to SILC network */
76 SILC_FSM_STATE(silc_client_new_id)
78 SilcClientConnection conn = fsm_context;
79 SilcClient client = conn->client;
80 SilcPacket packet = state_context;
86 SILC_LOG_DEBUG(("New ID received from server"));
88 if (!silc_id_payload_parse_id(silc_buffer_data(&packet->buffer),
89 silc_buffer_len(&packet->buffer), &id))
92 SILC_LOG_DEBUG(("New ID %s", silc_id_render(&id.u.client_id,
95 /* Create local client entry */
96 conn->local_entry = silc_client_add_client(client, conn,
101 if (!conn->local_entry)
104 /* Save the ID. Take reference to conn->local_id. */
105 conn->local_id = &conn->local_entry->id;
106 conn->internal->local_idp = silc_buffer_copy(&packet->buffer);
109 if (packet->src_id_len) {
110 conn->internal->remote_idp =
111 silc_id_payload_encode_data(packet->src_id,
113 packet->src_id_type);
114 if (!conn->internal->remote_idp)
116 silc_id_payload_parse_id(silc_buffer_data(conn->internal->remote_idp),
117 silc_buffer_len(conn->internal->remote_idp),
121 /* Set IDs to the packet stream */
122 silc_packet_set_ids(conn->stream, SILC_ID_CLIENT, conn->local_id,
123 conn->remote_id.type, SILC_ID_GET_ID(conn->remote_id));
125 /* Signal connection that new ID was received so it can continue
126 with the registering. */
127 if (conn->internal->registering)
128 silc_fsm_continue_sync(&conn->internal->event_thread);
131 /** Packet processed */
132 silc_packet_free(packet);
133 return SILC_FSM_FINISH;
137 /************************ Register to SILC network **************************/
139 /* Register to network */
141 SILC_FSM_STATE(silc_client_st_register)
143 SilcClientConnection conn = fsm_context;
144 SilcClient client = conn->client;
147 SILC_LOG_DEBUG(("Register to network"));
149 /* From SILC protocol version 1.3, nickname is in NEW_CLIENT packet */
150 if (conn->internal->remote_version >= 13)
151 nick = (conn->internal->params.nickname ?
152 conn->internal->params.nickname : client->username);
154 /* Send NEW_CLIENT packet to register to network */
155 if (!silc_packet_send_va(conn->stream, SILC_PACKET_NEW_CLIENT, 0,
156 SILC_STR_UI_SHORT(strlen(client->username)),
157 SILC_STR_DATA(client->username,
158 strlen(client->username)),
159 SILC_STR_UI_SHORT(strlen(client->realname)),
160 SILC_STR_DATA(client->realname,
161 strlen(client->realname)),
162 SILC_STR_UI_SHORT(nick ? strlen(nick) : 0),
163 SILC_STR_DATA(nick, nick ? strlen(nick) : 0),
165 /** Error sending packet */
166 silc_fsm_next(fsm, silc_client_st_register_error);
167 return SILC_FSM_CONTINUE;
170 /** Wait for new ID */
171 conn->internal->registering = TRUE;
172 silc_fsm_next_later(fsm, silc_client_st_register_complete,
173 conn->internal->retry_timer, 0);
174 return SILC_FSM_WAIT;
177 /* Wait for NEW_ID packet to arrive */
179 SILC_FSM_STATE(silc_client_st_register_complete)
181 SilcClientConnection conn = fsm_context;
182 SilcClient client = conn->client;
184 if (conn->internal->disconnected) {
186 silc_fsm_next(fsm, silc_client_st_register_error);
187 return SILC_FSM_CONTINUE;
190 if (!conn->local_id) {
191 if (conn->internal->retry_count++ >= SILC_CLIENT_RETRY_COUNT) {
192 /** Timeout, ID not received */
193 conn->internal->registering = FALSE;
194 conn->internal->retry_count = 0;
195 conn->internal->retry_timer = SILC_CLIENT_RETRY_MIN;
196 silc_fsm_next(fsm, silc_client_st_register_error);
197 return SILC_FSM_CONTINUE;
200 /** Resend registering packet */
201 silc_fsm_next(fsm, silc_client_st_register);
202 conn->internal->retry_timer = ((conn->internal->retry_timer *
203 SILC_CLIENT_RETRY_MUL) +
204 (silc_rng_get_rn16(client->rng) %
205 SILC_CLIENT_RETRY_RAND));
206 return SILC_FSM_CONTINUE;
209 SILC_LOG_DEBUG(("Registered to network"));
211 /* Issue IDENTIFY command for itself to get resolved hostname
212 correctly from server. */
213 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
214 silc_client_command_called_dummy, NULL,
215 1, 5, silc_buffer_data(conn->internal->local_idp),
216 silc_buffer_len(conn->internal->local_idp));
218 /* With SILC protocol version 1.2 call NICK command if the nickname was
219 set by the application. */
220 if (conn->internal->params.nickname && conn->internal->remote_version < 13 &&
221 !silc_utf8_strcasecmp(conn->internal->params.nickname, client->username))
222 silc_client_command_call(client, conn, NULL,
223 "NICK", conn->internal->params.nickname, NULL);
225 /* Issue INFO command to fetch the real server name and server
226 information and other stuff. */
227 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
228 silc_client_command_called_dummy, NULL,
229 1, 2, silc_buffer_data(conn->internal->remote_idp),
230 silc_buffer_len(conn->internal->remote_idp));
232 /* Call connection callback. We are now inside SILC network. */
233 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
234 conn->callback_context);
236 conn->internal->registering = FALSE;
237 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
238 silc_client_connect_timeout, conn);
240 return SILC_FSM_FINISH;
243 /* Error registering to network */
245 SILC_FSM_STATE(silc_client_st_register_error)
247 SilcClientConnection conn = fsm_context;
249 SILC_LOG_DEBUG(("Error registering to network"));
251 /* Signal to close connection */
252 conn->internal->status = SILC_CLIENT_CONN_ERROR;
253 if (!conn->internal->disconnected) {
254 conn->internal->disconnected = TRUE;
255 SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
258 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
259 silc_client_connect_timeout, conn);
261 return SILC_FSM_FINISH;
264 /************************* Resume detached session **************************/
266 /* Resume detached session */
268 SILC_FSM_STATE(silc_client_st_resume)
270 SilcClientConnection conn = fsm_context;
271 SilcClient client = conn->client;
272 SilcClientResumeSession resume;
276 SilcClientID client_id;
279 SILC_LOG_DEBUG(("Resuming detached session"));
281 resume = silc_calloc(1, sizeof(*resume));
284 silc_fsm_next(fsm, silc_client_st_resume_error);
285 return SILC_FSM_CONTINUE;
287 silc_fsm_set_state_context(fsm, resume);
289 silc_buffer_set(&resume->detach, conn->internal->params.detach_data,
290 conn->internal->params.detach_data_len);
291 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(&resume->detach),
292 silc_buffer_len(&resume->detach));
294 /* Take the old client ID from the detachment data */
295 ret = silc_buffer_unformat(&resume->detach,
297 SILC_STR_UI16_NSTRING_ALLOC(&resume->nickname,
299 SILC_STR_UI16_NSTRING(&id, &id_len),
300 SILC_STR_UI_INT(NULL),
301 SILC_STR_UI_INT(&resume->channel_count),
304 /** Malformed detach data */
305 SILC_LOG_DEBUG(("Malformed detachment data"));
306 silc_fsm_next(fsm, silc_client_st_resume_error);
307 return SILC_FSM_CONTINUE;
310 if (!silc_id_str2id(id, id_len, SILC_ID_CLIENT, &client_id,
311 sizeof(client_id))) {
313 SILC_LOG_DEBUG(("Malformed ID"));
314 silc_fsm_next(fsm, silc_client_st_resume_error);
315 return SILC_FSM_CONTINUE;
318 /* Generate authentication data that server will verify */
319 auth = silc_auth_public_key_auth_generate(conn->public_key,
322 conn->internal->hash,
323 &client_id, SILC_ID_CLIENT);
326 silc_fsm_next(fsm, silc_client_st_resume_error);
327 return SILC_FSM_CONTINUE;
330 /* Send RESUME_CLIENT packet to resume to network */
331 if (!silc_packet_send_va(conn->stream, SILC_PACKET_RESUME_CLIENT, 0,
332 SILC_STR_UI_SHORT(id_len),
333 SILC_STR_DATA(id, id_len),
334 SILC_STR_DATA(silc_buffer_data(auth),
335 silc_buffer_len(auth)),
337 /** Error sending packet */
338 SILC_LOG_DEBUG(("Error sending packet"));
339 silc_fsm_next(fsm, silc_client_st_resume_error);
340 return SILC_FSM_CONTINUE;
343 /** Wait for new ID */
344 conn->internal->registering = TRUE;
345 silc_fsm_next_later(fsm, silc_client_st_resume_resolve_channels, 15, 0);
346 return SILC_FSM_WAIT;
349 /* Resolve the old session information, user mode and joined channels. */
351 SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
353 SilcClientConnection conn = fsm_context;
354 SilcClient client = conn->client;
355 SilcClientResumeSession resume = state_context;
356 SilcUInt32 *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
357 unsigned char **res_argv = NULL;
360 if (conn->internal->disconnected) {
362 silc_fsm_next(fsm, silc_client_st_resume_error);
363 return SILC_FSM_CONTINUE;
366 if (!conn->local_id) {
367 /** Timeout, ID not received */
368 conn->internal->registering = FALSE;
369 silc_fsm_next(fsm, silc_client_st_resume_error);
370 return SILC_FSM_CONTINUE;
373 /** Wait for channels */
374 silc_fsm_next(fsm, silc_client_st_resume_resolve_cmodes);
376 /* Change our nickname */
377 silc_client_change_nickname(client, conn, conn->local_entry,
378 resume->nickname, NULL, NULL, 0);
380 /* Send UMODE command to get our own user mode in the network */
381 SILC_LOG_DEBUG(("Resolving user mode"));
382 silc_client_command_send(client, conn, SILC_COMMAND_UMODE,
383 silc_client_command_called_dummy, NULL,
384 1, 1, silc_buffer_data(conn->internal->local_idp),
385 silc_buffer_len(conn->internal->local_idp));
387 if (!resume->channel_count)
388 return SILC_FSM_YIELD;
390 /* Send IDENTIFY command for all channels we know about. These are the
391 channels we've joined to according our detachment data. */
392 for (i = 0; i < resume->channel_count; i++) {
393 SilcChannelEntry channel;
397 SilcChannelID channel_id;
400 if (silc_buffer_unformat(&resume->detach,
402 SILC_STR_UI16_NSTRING(&name, NULL),
403 SILC_STR_UI16_NSTRING(&chid, &chid_len),
404 SILC_STR_UI_INT(NULL),
408 if (!silc_id_str2id(chid, chid_len, SILC_ID_CHANNEL, &channel_id,
411 idp = silc_id_payload_encode_data(chid, chid_len, SILC_ID_CHANNEL);
415 /* Add the channel to cache */
416 channel = silc_client_get_channel_by_id(client, conn, &channel_id);
418 silc_client_add_channel(client, conn, name, 0, &channel_id);
420 res_argv = silc_realloc(res_argv, sizeof(*res_argv) * (res_argc + 1));
421 res_argv_lens = silc_realloc(res_argv_lens, sizeof(*res_argv_lens) *
423 res_argv_types = silc_realloc(res_argv_types, sizeof(*res_argv_types) *
425 res_argv[res_argc] = silc_buffer_steal(idp, &res_argv_lens[res_argc]);
426 res_argv_types[res_argc] = res_argc + 5;
428 silc_buffer_free(idp);
431 /* Send IDENTIFY command */
432 SILC_LOG_DEBUG(("Resolving joined channels"));
433 silc_client_command_send_argv(client, conn, SILC_COMMAND_IDENTIFY,
434 silc_client_resume_continue, conn,
435 res_argc, res_argv, res_argv_lens,
438 for (i = 0; i < resume->channel_count; i++)
439 silc_free(res_argv[i]);
441 silc_free(res_argv_lens);
442 silc_free(res_argv_types);
444 return SILC_FSM_WAIT;
447 /* Resolve joined channel modes, users and topics. */
449 SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
451 SilcClientConnection conn = fsm_context;
452 SilcClient client = conn->client;
453 SilcClientResumeSession resume = state_context;
454 SilcIDCacheEntry entry;
455 SilcChannelEntry channel;
459 if (conn->internal->disconnected) {
461 silc_fsm_next(fsm, silc_client_st_resume_error);
462 return SILC_FSM_CONTINUE;
465 SILC_LOG_DEBUG(("Resolving channel details"));
467 /** Wait for channel modes */
468 silc_fsm_next(fsm, silc_client_st_resume_completed);
470 if (!silc_idcache_get_all(conn->internal->channel_cache, &channels))
471 return SILC_FSM_YIELD;
473 /* Resolve channels' mode, users and topic */
474 resume->channel_count = silc_list_count(channels) * 3;
475 silc_list_start(channels);
476 while ((entry = silc_list_get(channels))) {
477 channel = entry->context;
478 idp = silc_id_payload_encode(&channel->id, SILC_ID_CHANNEL);
482 silc_client_command_send(client, conn, SILC_COMMAND_CMODE,
483 silc_client_resume_continue, conn, 1,
484 1, silc_buffer_data(idp),
485 silc_buffer_len(idp));
486 silc_client_command_send(client, conn, SILC_COMMAND_USERS,
487 silc_client_resume_continue, conn, 1,
488 1, silc_buffer_data(idp),
489 silc_buffer_len(idp));
490 silc_client_command_send(client, conn, SILC_COMMAND_TOPIC,
491 silc_client_resume_continue, conn, 1,
492 1, silc_buffer_data(idp),
493 silc_buffer_len(idp));
494 silc_buffer_free(idp);
497 return SILC_FSM_WAIT;
500 /* Resuming completed */
502 SILC_FSM_STATE(silc_client_st_resume_completed)
504 SilcClientConnection conn = fsm_context;
505 SilcClient client = conn->client;
506 SilcClientResumeSession resume = state_context;
507 SilcIDCacheEntry entry;
508 SilcChannelEntry channel;
511 if (conn->internal->disconnected) {
513 silc_fsm_next(fsm, silc_client_st_resume_error);
514 return SILC_FSM_CONTINUE;
517 if (resume->channel_count > 0) {
518 resume->channel_count--;
519 if (resume->channel_count)
520 return SILC_FSM_WAIT;
523 SILC_LOG_DEBUG(("Resuming completed"));
525 /* Issue IDENTIFY command for itself to get resolved hostname
526 correctly from server. */
527 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
528 silc_client_command_called_dummy, NULL,
529 1, 5, silc_buffer_data(conn->internal->local_idp),
530 silc_buffer_len(conn->internal->local_idp));
532 /* Issue INFO command to fetch the real server name and server
533 information and other stuff. */
534 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
535 silc_client_command_called_dummy, NULL,
536 1, 2, silc_buffer_data(conn->internal->remote_idp),
537 silc_buffer_len(conn->internal->remote_idp));
539 /* Call connection callback. We have now resumed to SILC network. */
540 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS_RESUME, 0, NULL,
541 conn->callback_context);
543 /* Call UMODE command reply. */
544 if (conn->local_entry->mode)
545 silc_client_resume_command_callback(client, conn, SILC_COMMAND_UMODE,
546 conn->local_entry->mode);
548 /* Call NICK command reply. */
549 silc_client_resume_command_callback(client, conn, SILC_COMMAND_NICK,
551 conn->local_entry->nickname,
552 &conn->local_entry->id);
554 /* Call JOIN command replies for all joined channel */
555 silc_idcache_get_all(conn->internal->channel_cache, &channels);
556 silc_list_start(channels);
557 while ((entry = silc_list_get(channels))) {
558 SilcHashTableList htl;
559 const char *cipher, *hmac;
561 channel = entry->context;
562 cipher = (channel->internal.send_key ?
563 silc_cipher_get_name(channel->internal.send_key) : NULL);
564 hmac = (channel->internal.hmac ?
565 silc_hmac_get_name(channel->internal.hmac) : NULL);
566 silc_hash_table_list(channel->user_list, &htl);
567 silc_client_resume_command_callback(client, conn, SILC_COMMAND_JOIN,
568 channel->channel_name, channel,
569 channel->mode, &htl, channel->topic,
570 cipher, hmac, channel->founder_key,
571 channel->channel_pubkeys,
572 channel->user_limit);
573 silc_hash_table_list_reset(&htl);
576 conn->internal->registering = FALSE;
577 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
578 silc_client_connect_timeout, conn);
579 silc_free(resume->nickname);
582 return SILC_FSM_FINISH;
585 /* Error resuming to network */
587 SILC_FSM_STATE(silc_client_st_resume_error)
589 SilcClientConnection conn = fsm_context;
590 SilcClientResumeSession resume = state_context;
592 if (conn->internal->disconnected) {
594 silc_free(resume->nickname);
597 return SILC_FSM_FINISH;
600 SILC_LOG_DEBUG(("Error resuming to network"));
602 /* Signal to close connection */
603 conn->internal->status = SILC_CLIENT_CONN_ERROR;
604 if (!conn->internal->disconnected) {
605 conn->internal->disconnected = TRUE;
606 SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
609 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
610 silc_client_connect_timeout, conn);
613 silc_free(resume->nickname);
617 return SILC_FSM_FINISH;
620 /* Generates the session detachment data. This data can be used later
621 to resume back to the server. */
623 SilcBuffer silc_client_get_detach_data(SilcClient client,
624 SilcClientConnection conn)
627 SilcHashTableList htl;
629 unsigned char id[64];
633 SILC_LOG_DEBUG(("Creating detachment data"));
635 ch_count = silc_hash_table_count(conn->local_entry->channels);
636 silc_id_id2str(conn->local_id, SILC_ID_CLIENT, id, sizeof(id), &id_len);
638 /* Save the nickname, Client ID and user mode in SILC network */
639 detach = silc_buffer_alloc(0);
643 silc_buffer_format(detach,
645 SILC_STR_UI_SHORT(strlen(conn->local_entry->nickname)),
646 SILC_STR_DATA(conn->local_entry->nickname,
647 strlen(conn->local_entry->nickname)),
648 SILC_STR_UI_SHORT(id_len),
649 SILC_STR_DATA(id, id_len),
650 SILC_STR_UI_INT(conn->local_entry->mode),
651 SILC_STR_UI_INT(ch_count),
654 silc_buffer_free(detach);
658 /* Save all joined channels */
659 silc_hash_table_list(conn->local_entry->channels, &htl);
660 while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
661 unsigned char chid[32];
664 silc_id_id2str(&chu->channel->id, SILC_ID_CHANNEL, chid, sizeof(chid),
666 silc_buffer_format(detach,
668 SILC_STR_UI_SHORT(strlen(chu->channel->channel_name)),
669 SILC_STR_DATA(chu->channel->channel_name,
670 strlen(chu->channel->channel_name)),
671 SILC_STR_UI_SHORT(chid_len),
672 SILC_STR_DATA(chid, chid_len),
673 SILC_STR_UI_INT(chu->channel->mode),
676 silc_hash_table_list_reset(&htl);
678 silc_buffer_start(detach);
679 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(detach),
680 silc_buffer_len(detach));