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 SilcClientID client_id;
33 SilcUInt32 channel_count;
34 SilcUInt32 *cmd_idents;
35 SilcUInt32 cmd_idents_count;
37 } *SilcClientResumeSession;
39 /************************ Static utility functions **************************/
41 /* Command callback. Nothing interesting to do here. */
44 silc_client_register_command_called(SilcClient client,
45 SilcClientConnection conn,
55 /* Continues resuming after resolving. Continue after last reply. */
58 silc_client_resume_continue(SilcClient client,
59 SilcClientConnection conn,
66 if (status == SILC_STATUS_OK || status == SILC_STATUS_LIST_END ||
67 SILC_STATUS_IS_ERROR(status)) {
68 silc_fsm_continue(&conn->internal->event_thread);
75 /****************************** NEW_ID packet *******************************/
77 /* Received new ID packet from server during registering to SILC network */
79 SILC_FSM_STATE(silc_client_new_id)
81 SilcClientConnection conn = fsm_context;
82 SilcClient client = conn->client;
83 SilcPacket packet = state_context;
89 SILC_LOG_DEBUG(("New ID received from server"));
91 if (!silc_id_payload_parse_id(silc_buffer_data(&packet->buffer),
92 silc_buffer_len(&packet->buffer), &id))
95 SILC_LOG_DEBUG(("New ID %s", silc_id_render(&id.u.client_id,
98 /* Create local client entry */
99 conn->local_entry = silc_client_add_client(client, conn,
104 if (!conn->local_entry)
108 conn->local_id = &conn->local_entry->id;
109 conn->internal->local_idp = silc_buffer_copy(&packet->buffer);
111 /* Save cache entry */
112 silc_mutex_lock(conn->internal->lock);
113 if (!silc_idcache_find_by_id_one(conn->internal->client_cache,
115 &conn->internal->local_entry)) {
116 silc_mutex_unlock(conn->internal->lock);
119 silc_mutex_unlock(conn->internal->lock);
122 if (packet->src_id_len) {
123 conn->internal->remote_idp =
124 silc_id_payload_encode_data(packet->src_id,
126 packet->src_id_type);
127 if (!conn->internal->remote_idp)
129 silc_id_payload_parse_id(silc_buffer_data(conn->internal->remote_idp),
130 silc_buffer_len(conn->internal->remote_idp),
134 /* Set IDs to the packet stream */
135 silc_packet_set_ids(conn->stream, SILC_ID_CLIENT, conn->local_id,
136 conn->remote_id.type, SILC_ID_GET_ID(conn->remote_id));
138 /* Signal connection that new ID was received so it can continue
139 with the registering. */
140 if (conn->internal->registering)
141 silc_fsm_continue_sync(&conn->internal->event_thread);
144 /** Packet processed */
145 silc_packet_free(packet);
146 return SILC_FSM_FINISH;
150 /************************ Register to SILC network **************************/
152 /* Register to network */
154 SILC_FSM_STATE(silc_client_st_register)
156 SilcClientConnection conn = fsm_context;
157 SilcClient client = conn->client;
159 SILC_LOG_DEBUG(("Register to network"));
161 /* Send NEW_CLIENT packet to register to network */
162 if (!silc_packet_send_va(conn->stream, SILC_PACKET_NEW_CLIENT, 0,
163 SILC_STR_UI_SHORT(strlen(client->username)),
164 SILC_STR_DATA(client->username,
165 strlen(client->username)),
166 SILC_STR_UI_SHORT(strlen(client->realname)),
167 SILC_STR_DATA(client->realname,
168 strlen(client->realname)),
170 /** Error sending packet */
171 silc_fsm_next(fsm, silc_client_st_register_error);
172 return SILC_FSM_CONTINUE;
175 /** Wait for new ID */
176 conn->internal->registering = TRUE;
177 silc_fsm_next_later(fsm, silc_client_st_register_complete,
178 conn->internal->retry_timer, 0);
179 return SILC_FSM_WAIT;
182 /* Wait for NEW_ID packet to arrive */
184 SILC_FSM_STATE(silc_client_st_register_complete)
186 SilcClientConnection conn = fsm_context;
187 SilcClient client = conn->client;
189 if (conn->internal->disconnected) {
191 silc_fsm_next(fsm, silc_client_st_register_error);
192 return SILC_FSM_CONTINUE;
195 if (!conn->local_id) {
196 if (conn->internal->retry_count++ >= SILC_CLIENT_RETRY_COUNT) {
197 /** Timeout, ID not received */
198 conn->internal->registering = FALSE;
199 conn->internal->retry_count = 0;
200 conn->internal->retry_timer = SILC_CLIENT_RETRY_MIN;
201 silc_fsm_next(fsm, silc_client_st_register_error);
202 return SILC_FSM_CONTINUE;
205 /** Resend registering packet */
206 silc_fsm_next(fsm, silc_client_st_register);
207 conn->internal->retry_timer = ((conn->internal->retry_timer *
208 SILC_CLIENT_RETRY_MUL) +
209 (silc_rng_get_rn16(client->rng) %
210 SILC_CLIENT_RETRY_RAND));
211 return SILC_FSM_CONTINUE;
214 SILC_LOG_DEBUG(("Registered to network"));
216 /* Issue IDENTIFY command for itself to get resolved hostname
217 correctly from server. */
218 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
219 silc_client_register_command_called, NULL,
220 1, 5, silc_buffer_data(conn->internal->local_idp),
221 silc_buffer_len(conn->internal->local_idp));
223 /* Call NICK command if the nickname was set by the application (and is
224 not same as the username). */
225 if (conn->internal->params.nickname &&
226 !silc_utf8_strcasecmp(conn->internal->params.nickname, client->username))
227 silc_client_command_call(client, conn, NULL,
228 "NICK", conn->internal->params.nickname, NULL);
230 /* Issue INFO command to fetch the real server name and server
231 information and other stuff. */
232 silc_client_command_send(client, conn, SILC_COMMAND_INFO,
233 silc_client_register_command_called, NULL,
234 1, 2, silc_buffer_data(conn->internal->remote_idp),
235 silc_buffer_len(conn->internal->remote_idp));
237 /* Call connection callback. We are now inside SILC network. */
238 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
239 conn->callback_context);
241 conn->internal->registering = FALSE;
242 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
243 silc_client_connect_timeout, conn);
245 return SILC_FSM_FINISH;
248 /* Error registering to network */
250 SILC_FSM_STATE(silc_client_st_register_error)
252 SilcClientConnection conn = fsm_context;
253 SilcClient client = conn->client;
255 SILC_LOG_DEBUG(("Error registering to network"));
257 /* Signal to close connection */
258 if (!conn->internal->disconnected) {
259 conn->internal->disconnected = TRUE;
260 SILC_FSM_SEMA_POST(&conn->internal->wait_event);
263 /* Call connect callback */
264 if (conn->internal->callback_called)
265 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
266 conn->callback_context);
267 conn->internal->callback_called = TRUE;
269 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
270 silc_client_connect_timeout, conn);
272 return SILC_FSM_FINISH;
275 /************************* Resume detached session **************************/
277 /* Resume detached session */
279 SILC_FSM_STATE(silc_client_st_resume)
281 SilcClientConnection conn = fsm_context;
282 SilcClient client = conn->client;
283 SilcClientResumeSession resume;
289 SILC_LOG_DEBUG(("Resuming detached session"));
291 resume = silc_calloc(1, sizeof(*resume));
294 silc_fsm_next(fsm, silc_client_st_resume_error);
295 return SILC_FSM_CONTINUE;
297 silc_fsm_set_state_context(fsm, resume);
299 silc_buffer_set(&resume->detach, conn->internal->params.detach_data,
300 conn->internal->params.detach_data_len);
301 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(&resume->detach),
302 silc_buffer_len(&resume->detach));
304 /* Take the old client ID from the detachment data */
305 ret = silc_buffer_unformat(&resume->detach,
307 SILC_STR_UI16_NSTRING_ALLOC(&resume->nickname,
309 SILC_STR_UI16_NSTRING(&id, &id_len),
310 SILC_STR_UI_INT(NULL),
311 SILC_STR_UI_INT(&resume->channel_count),
314 /** Malformed detach data */
315 silc_fsm_next(fsm, silc_client_st_resume_error);
316 return SILC_FSM_CONTINUE;
319 if (!silc_id_str2id(id, id_len, SILC_ID_CLIENT, &resume->client_id,
320 sizeof(resume->client_id))) {
322 silc_fsm_next(fsm, silc_client_st_resume_error);
323 return SILC_FSM_CONTINUE;
326 /* Generate authentication data that server will verify */
327 auth = silc_auth_public_key_auth_generate(conn->public_key,
330 conn->internal->hash,
335 silc_fsm_next(fsm, silc_client_st_resume_error);
336 return SILC_FSM_CONTINUE;
339 /* Send RESUME_CLIENT packet to resume to network */
340 if (!silc_packet_send_va(conn->stream, SILC_PACKET_RESUME_CLIENT, 0,
341 SILC_STR_UI_SHORT(id_len),
342 SILC_STR_DATA(id, id_len),
343 SILC_STR_DATA(silc_buffer_data(auth),
344 silc_buffer_len(auth)),
346 /** Error sending packet */
347 silc_fsm_next(fsm, silc_client_st_resume_error);
348 return SILC_FSM_CONTINUE;
351 /** Wait for new ID */
352 conn->internal->registering = TRUE;
353 silc_fsm_next_later(fsm, silc_client_st_resume_resolve_channels, 15, 0);
354 return SILC_FSM_WAIT;
357 /* Resolve the old session information, user mode and joined channels. */
359 SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
361 SilcClientConnection conn = fsm_context;
362 SilcClient client = conn->client;
363 SilcClientResumeSession resume = state_context;
364 SilcUInt32 *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
365 unsigned char **res_argv = NULL;
368 if (!conn->local_id) {
369 /** Timeout, ID not received */
370 conn->internal->registering = FALSE;
371 silc_fsm_next(fsm, silc_client_st_resume_error);
372 return SILC_FSM_CONTINUE;
375 /* First, send UMODE command to get our own user mode in the network */
376 SILC_LOG_DEBUG(("Resolving user mode"));
377 silc_client_command_send(client, conn, SILC_COMMAND_UMODE,
378 silc_client_register_command_called, NULL,
379 1, 1, silc_buffer_data(conn->internal->local_idp),
380 silc_buffer_len(conn->internal->local_idp));
382 /* Second, send IDENTIFY command for all channels we know about. These
383 are the channels we've joined to according our detachment data. */
384 for (i = 0; i < resume->channel_count; i++) {
385 SilcChannelID channel_id;
390 if (silc_buffer_unformat(&resume->detach,
392 SILC_STR_UI16_NSTRING(NULL, NULL),
393 SILC_STR_UI16_NSTRING(&chid, &chid_len),
394 SILC_STR_UI_INT(NULL),
398 idp = silc_id_payload_encode_data(chid, chid_len, SILC_ID_CHANNEL);
401 res_argv = silc_realloc(res_argv, sizeof(*res_argv) * (res_argc + 1));
402 res_argv_lens = silc_realloc(res_argv_lens, sizeof(*res_argv_lens) *
404 res_argv_types = silc_realloc(res_argv_types, sizeof(*res_argv_types) *
406 res_argv[res_argc] = silc_buffer_steal(idp, &res_argv_lens[res_argc]);
407 res_argv_types[res_argc] = res_argc + 5;
409 silc_buffer_free(idp);
412 /* Send IDENTIFY command */
413 SILC_LOG_DEBUG(("Resolving joined channels"));
414 silc_client_command_send_argv(client, conn, SILC_COMMAND_IDENTIFY,
415 silc_client_resume_continue, conn,
416 res_argc, res_argv, res_argv_lens,
419 for (i = 0; i < resume->channel_count; i++)
420 silc_free(res_argv[i]);
422 silc_free(res_argv_lens);
423 silc_free(res_argv_types);
425 /** Wait for channels */
426 silc_fsm_next(fsm, silc_client_st_resume_resolve_cmodes);
427 return SILC_FSM_WAIT;
430 /* Resolve joined channel modes. */
432 SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
434 SilcClientConnection conn = fsm_context;
435 SilcClientResumeSession resume = state_context;
436 SilcHashTableList htl;
439 SILC_LOG_DEBUG(("Resolving joined channel modes"));
441 silc_hash_table_list(conn->local_entry->channels, &htl);
442 while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
445 silc_hash_table_list_reset(&htl);
447 return SILC_FSM_FINISH;
450 SILC_FSM_STATE(silc_client_st_resume_error)
453 /* Close connection */
455 return SILC_FSM_FINISH;
458 /* Generates the session detachment data. This data can be used later
459 to resume back to the server. */
461 SilcBuffer silc_client_get_detach_data(SilcClient client,
462 SilcClientConnection conn)
465 SilcHashTableList htl;
469 SILC_LOG_DEBUG(("Creating detachment data"));
471 ch_count = silc_hash_table_count(conn->local_entry->channels);
473 /* Save the nickname, Client ID and user mode in SILC network */
474 detach = silc_buffer_alloc(0);
478 silc_buffer_format(detach,
480 SILC_STR_UI_SHORT(strlen(conn->local_entry->nickname)),
481 SILC_STR_DATA(conn->local_entry->nickname,
482 strlen(conn->local_entry->nickname)),
483 SILC_STR_UI_SHORT(silc_buffer_len(conn->internal->
485 SILC_STR_DATA(silc_buffer_data(conn->internal->
487 silc_buffer_len(conn->internal->
489 SILC_STR_UI_INT(conn->local_entry->mode),
490 SILC_STR_UI_INT(ch_count),
493 silc_buffer_free(detach);
497 /* Save all joined channels */
498 silc_hash_table_list(conn->local_entry->channels, &htl);
499 while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
500 unsigned char chid[32];
503 silc_id_id2str(&chu->channel->id, SILC_ID_CHANNEL, chid, sizeof(chid),
505 silc_buffer_format(detach,
507 SILC_STR_UI_SHORT(strlen(chu->channel->channel_name)),
508 SILC_STR_DATA(chu->channel->channel_name,
509 strlen(chu->channel->channel_name)),
510 SILC_STR_UI_SHORT(chid_len),
511 SILC_STR_DATA(chid, chid_len),
512 SILC_STR_UI_INT(chu->channel->mode),
516 silc_hash_table_list_reset(&htl);
518 silc_buffer_start(detach);
519 SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(detach),
520 silc_buffer_len(detach));