Call the connection callback in disconnection always as the last
[silc.git] / lib / silcclient / client_register.c
1 /*
2
3   client_register.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2006 - 2007 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 #include "silc.h"
21 #include "silcclient.h"
22 #include "client_internal.h"
23
24 /************************** Types and definitions ***************************/
25
26 /* Resume session context */
27 typedef struct {
28   SilcClient client;
29   SilcClientConnection conn;
30   SilcBufferStruct detach;
31   char *nickname;
32   SilcUInt32 channel_count;
33 } *SilcClientResumeSession;
34
35 /************************ Static utility functions **************************/
36
37 /* Command callback.  Nothing interesting to do here. */
38
39 static SilcBool
40 silc_client_register_command_called(SilcClient client,
41                                     SilcClientConnection conn,
42                                     SilcCommand command,
43                                     SilcStatus status,
44                                     SilcStatus error,
45                                     void *context,
46                                     va_list ap)
47 {
48   return FALSE;
49 }
50
51 /* Continues resuming after resolving.  Continue after last reply. */
52
53 static SilcBool
54 silc_client_resume_continue(SilcClient client,
55                             SilcClientConnection conn,
56                             SilcCommand command,
57                             SilcStatus status,
58                             SilcStatus error,
59                             void *context,
60                             va_list ap)
61 {
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);
65     return FALSE;
66   }
67
68   return TRUE;
69 }
70
71 /* Function used to call command replies back to application in resuming. */
72
73 static void
74 silc_client_resume_command_callback(SilcClient client,
75                                     SilcClientConnection conn,
76                                     SilcCommand command, ...)
77 {
78   va_list ap;
79   va_start(ap, command);
80   client->internal->ops->command_reply(client, conn, command,
81                                        SILC_STATUS_OK, SILC_STATUS_OK, ap);
82   va_end(ap);
83 }
84
85
86 /****************************** NEW_ID packet *******************************/
87
88 /* Received new ID packet from server during registering to SILC network */
89
90 SILC_FSM_STATE(silc_client_new_id)
91 {
92   SilcClientConnection conn = fsm_context;
93   SilcClient client = conn->client;
94   SilcPacket packet = state_context;
95   SilcID id;
96
97   if (conn->local_id)
98     goto out;
99
100   SILC_LOG_DEBUG(("New ID received from server"));
101
102   if (!silc_id_payload_parse_id(silc_buffer_data(&packet->buffer),
103                                 silc_buffer_len(&packet->buffer), &id))
104     goto out;
105
106   SILC_LOG_DEBUG(("New ID %s", silc_id_render(&id.u.client_id,
107                                               SILC_ID_CLIENT)));
108
109   /* Create local client entry */
110   conn->local_entry = silc_client_add_client(client, conn,
111                                              client->username,
112                                              client->username,
113                                              client->realname,
114                                              &id.u.client_id, 0);
115   if (!conn->local_entry)
116     goto out;
117
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);
121
122   /* Save remote ID */
123   if (packet->src_id_len) {
124     conn->internal->remote_idp =
125       silc_id_payload_encode_data(packet->src_id,
126                                   packet->src_id_len,
127                                   packet->src_id_type);
128     if (!conn->internal->remote_idp)
129       goto out;
130     silc_id_payload_parse_id(silc_buffer_data(conn->internal->remote_idp),
131                              silc_buffer_len(conn->internal->remote_idp),
132                              &conn->remote_id);
133   }
134
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));
138
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);
143
144  out:
145   /** Packet processed */
146   silc_packet_free(packet);
147   return SILC_FSM_FINISH;
148 }
149
150
151 /************************ Register to SILC network **************************/
152
153 /* Register to network */
154
155 SILC_FSM_STATE(silc_client_st_register)
156 {
157   SilcClientConnection conn = fsm_context;
158   SilcClient client = conn->client;
159
160   SILC_LOG_DEBUG(("Register to network"));
161
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)),
170                            SILC_STR_END)) {
171     /** Error sending packet */
172     silc_fsm_next(fsm, silc_client_st_register_error);
173     return SILC_FSM_CONTINUE;
174   }
175
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;
181 }
182
183 /* Wait for NEW_ID packet to arrive */
184
185 SILC_FSM_STATE(silc_client_st_register_complete)
186 {
187   SilcClientConnection conn = fsm_context;
188   SilcClient client = conn->client;
189
190   if (conn->internal->disconnected) {
191     /** Disconnected */
192     silc_fsm_next(fsm, silc_client_st_register_error);
193     return SILC_FSM_CONTINUE;
194   }
195
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;
204     }
205
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;
213   }
214
215   SILC_LOG_DEBUG(("Registered to network"));
216
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));
223
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);
230
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));
237
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);
241
242   conn->internal->registering = FALSE;
243   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
244                                 silc_client_connect_timeout, conn);
245
246   return SILC_FSM_FINISH;
247 }
248
249 /* Error registering to network */
250
251 SILC_FSM_STATE(silc_client_st_register_error)
252 {
253   SilcClientConnection conn = fsm_context;
254
255   SILC_LOG_DEBUG(("Error registering to network"));
256
257   /* Signal to close connection */
258   conn->internal->status = SILC_CLIENT_CONN_ERROR;
259   if (!conn->internal->disconnected) {
260     conn->internal->disconnected = TRUE;
261     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
262   }
263
264   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
265                                 silc_client_connect_timeout, conn);
266
267   return SILC_FSM_FINISH;
268 }
269
270 /************************* Resume detached session **************************/
271
272 /* Resume detached session */
273
274 SILC_FSM_STATE(silc_client_st_resume)
275 {
276   SilcClientConnection conn = fsm_context;
277   SilcClient client = conn->client;
278   SilcClientResumeSession resume;
279   SilcBuffer auth;
280   unsigned char *id;
281   SilcUInt16 id_len;
282   SilcClientID client_id;
283   int ret;
284
285   SILC_LOG_DEBUG(("Resuming detached session"));
286
287   resume = silc_calloc(1, sizeof(*resume));
288   if (!resume) {
289     /** Out of memory */
290     silc_fsm_next(fsm, silc_client_st_resume_error);
291     return SILC_FSM_CONTINUE;
292   }
293   silc_fsm_set_state_context(fsm, resume);
294
295   silc_buffer_set(&resume->detach, conn->internal->params.detach_data,
296                   conn->internal->params.detach_data_len);
297   SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(&resume->detach),
298                    silc_buffer_len(&resume->detach));
299
300   /* Take the old client ID from the detachment data */
301   ret = silc_buffer_unformat(&resume->detach,
302                              SILC_STR_ADVANCE,
303                              SILC_STR_UI16_NSTRING_ALLOC(&resume->nickname,
304                                                          NULL),
305                              SILC_STR_UI16_NSTRING(&id, &id_len),
306                              SILC_STR_UI_INT(NULL),
307                              SILC_STR_UI_INT(&resume->channel_count),
308                              SILC_STR_END);
309   if (ret < 0) {
310     /** Malformed detach data */
311     SILC_LOG_DEBUG(("Malformed detachment data"));
312     silc_fsm_next(fsm, silc_client_st_resume_error);
313     return SILC_FSM_CONTINUE;
314   }
315
316   if (!silc_id_str2id(id, id_len, SILC_ID_CLIENT, &client_id,
317                       sizeof(client_id))) {
318     /** Malformed ID */
319     SILC_LOG_DEBUG(("Malformed ID"));
320     silc_fsm_next(fsm, silc_client_st_resume_error);
321     return SILC_FSM_CONTINUE;
322   }
323
324   /* Generate authentication data that server will verify */
325   auth = silc_auth_public_key_auth_generate(conn->public_key,
326                                             conn->private_key,
327                                             client->rng,
328                                             conn->internal->hash,
329                                             &client_id, SILC_ID_CLIENT);
330   if (!auth) {
331     /** Out of memory */
332     silc_fsm_next(fsm, silc_client_st_resume_error);
333     return SILC_FSM_CONTINUE;
334   }
335
336   /* Send RESUME_CLIENT packet to resume to network */
337   if (!silc_packet_send_va(conn->stream, SILC_PACKET_RESUME_CLIENT, 0,
338                            SILC_STR_UI_SHORT(id_len),
339                            SILC_STR_DATA(id, id_len),
340                            SILC_STR_DATA(silc_buffer_data(auth),
341                                          silc_buffer_len(auth)),
342                            SILC_STR_END)) {
343     /** Error sending packet */
344     SILC_LOG_DEBUG(("Error sending packet"));
345     silc_fsm_next(fsm, silc_client_st_resume_error);
346     return SILC_FSM_CONTINUE;
347   }
348
349   /** Wait for new ID */
350   conn->internal->registering = TRUE;
351   silc_fsm_next_later(fsm, silc_client_st_resume_resolve_channels, 15, 0);
352   return SILC_FSM_WAIT;
353 }
354
355 /* Resolve the old session information, user mode and joined channels. */
356
357 SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
358 {
359   SilcClientConnection conn = fsm_context;
360   SilcClient client = conn->client;
361   SilcClientResumeSession resume = state_context;
362   SilcUInt32 *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
363   unsigned char **res_argv = NULL;
364   int i;
365
366   if (conn->internal->disconnected) {
367     /** Disconnected */
368     silc_fsm_next(fsm, silc_client_st_resume_error);
369     return SILC_FSM_CONTINUE;
370   }
371
372   if (!conn->local_id) {
373     /** Timeout, ID not received */
374     conn->internal->registering = FALSE;
375     silc_fsm_next(fsm, silc_client_st_resume_error);
376     return SILC_FSM_CONTINUE;
377   }
378
379   /** Wait for channels */
380   silc_fsm_next(fsm, silc_client_st_resume_resolve_cmodes);
381
382   /* Change our nickname */
383   silc_client_change_nickname(client, conn, conn->local_entry,
384                               resume->nickname, NULL, NULL, 0);
385
386   /* Send UMODE command to get our own user mode in the network */
387   SILC_LOG_DEBUG(("Resolving user mode"));
388   silc_client_command_send(client, conn, SILC_COMMAND_UMODE,
389                            silc_client_register_command_called, NULL,
390                            1, 1, silc_buffer_data(conn->internal->local_idp),
391                            silc_buffer_len(conn->internal->local_idp));
392
393   if (!resume->channel_count)
394     return SILC_FSM_YIELD;
395
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;
400     unsigned char *chid;
401     SilcUInt16 chid_len;
402     SilcBuffer idp;
403     SilcChannelID channel_id;
404     char *name;
405
406     if (silc_buffer_unformat(&resume->detach,
407                              SILC_STR_ADVANCE,
408                              SILC_STR_UI16_NSTRING(&name, NULL),
409                              SILC_STR_UI16_NSTRING(&chid, &chid_len),
410                              SILC_STR_UI_INT(NULL),
411                              SILC_STR_END) < 0)
412       continue;
413
414     if (!silc_id_str2id(chid, chid_len, SILC_ID_CHANNEL, &channel_id,
415                         sizeof(channel_id)))
416       continue;
417     idp = silc_id_payload_encode_data(chid, chid_len, SILC_ID_CHANNEL);
418     if (!idp)
419       continue;
420
421     /* Add the channel to cache */
422     channel = silc_client_get_channel_by_id(client, conn, &channel_id);
423     if (!channel)
424       silc_client_add_channel(client, conn, name, 0, &channel_id);
425
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) *
428                                  (res_argc + 1));
429     res_argv_types = silc_realloc(res_argv_types, sizeof(*res_argv_types) *
430                                   (res_argc + 1));
431     res_argv[res_argc] = silc_buffer_steal(idp, &res_argv_lens[res_argc]);
432     res_argv_types[res_argc] = res_argc + 5;
433     res_argc++;
434     silc_buffer_free(idp);
435   }
436
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,
442                                 res_argv_types);
443
444   for (i = 0; i < resume->channel_count; i++)
445     silc_free(res_argv[i]);
446   silc_free(res_argv);
447   silc_free(res_argv_lens);
448   silc_free(res_argv_types);
449
450   return SILC_FSM_WAIT;
451 }
452
453 /* Resolve joined channel modes, users and topics. */
454
455 SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
456 {
457   SilcClientConnection conn = fsm_context;
458   SilcClient client = conn->client;
459   SilcClientResumeSession resume = state_context;
460   SilcIDCacheEntry entry;
461   SilcChannelEntry channel;
462   SilcList channels;
463   SilcBuffer idp;
464
465   if (conn->internal->disconnected) {
466     /** Disconnected */
467     silc_fsm_next(fsm, silc_client_st_resume_error);
468     return SILC_FSM_CONTINUE;
469   }
470
471   SILC_LOG_DEBUG(("Resolving channel details"));
472
473   /** Wait for channel modes */
474   silc_fsm_next(fsm, silc_client_st_resume_completed);
475
476   if (!silc_idcache_get_all(conn->internal->channel_cache, &channels))
477     return SILC_FSM_YIELD;
478
479   /* Resolve channels' mode, users and topic */
480   resume->channel_count = silc_list_count(channels) * 3;
481   silc_list_start(channels);
482   while ((entry = silc_list_get(channels))) {
483     channel = entry->context;
484     idp = silc_id_payload_encode(&channel->id, SILC_ID_CHANNEL);
485     if (!idp)
486       continue;
487
488     silc_client_command_send(client, conn, SILC_COMMAND_CMODE,
489                              silc_client_resume_continue, conn, 1,
490                              1, silc_buffer_data(idp),
491                              silc_buffer_len(idp));
492     silc_client_command_send(client, conn, SILC_COMMAND_USERS,
493                              silc_client_resume_continue, conn, 1,
494                              1, silc_buffer_data(idp),
495                              silc_buffer_len(idp));
496     silc_client_command_send(client, conn, SILC_COMMAND_TOPIC,
497                              silc_client_resume_continue, conn, 1,
498                              1, silc_buffer_data(idp),
499                              silc_buffer_len(idp));
500     silc_buffer_free(idp);
501   }
502
503   return SILC_FSM_WAIT;
504 }
505
506 /* Resuming completed */
507
508 SILC_FSM_STATE(silc_client_st_resume_completed)
509 {
510   SilcClientConnection conn = fsm_context;
511   SilcClient client = conn->client;
512   SilcClientResumeSession resume = state_context;
513   SilcIDCacheEntry entry;
514   SilcChannelEntry channel;
515   SilcList channels;
516
517   if (conn->internal->disconnected) {
518     /** Disconnected */
519     silc_fsm_next(fsm, silc_client_st_resume_error);
520     return SILC_FSM_CONTINUE;
521   }
522
523   if (resume->channel_count > 0) {
524     resume->channel_count--;
525     if (resume->channel_count)
526       return SILC_FSM_WAIT;
527   }
528
529   SILC_LOG_DEBUG(("Resuming completed"));
530
531   /* Issue IDENTIFY command for itself to get resolved hostname
532      correctly from server. */
533   silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
534                            silc_client_register_command_called, NULL,
535                            1, 5, silc_buffer_data(conn->internal->local_idp),
536                            silc_buffer_len(conn->internal->local_idp));
537
538   /* Issue INFO command to fetch the real server name and server
539      information and other stuff. */
540   silc_client_command_send(client, conn, SILC_COMMAND_INFO,
541                            silc_client_register_command_called, NULL,
542                            1, 2, silc_buffer_data(conn->internal->remote_idp),
543                            silc_buffer_len(conn->internal->remote_idp));
544
545   /* Call connection callback.  We have now resumed to SILC network. */
546   conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS_RESUME, 0, NULL,
547                  conn->callback_context);
548
549   /* Call UMODE command reply. */
550   if (conn->local_entry->mode)
551     silc_client_resume_command_callback(client, conn, SILC_COMMAND_UMODE,
552                                         conn->local_entry->mode);
553
554   /* Call NICK command reply. */
555   silc_client_resume_command_callback(client, conn, SILC_COMMAND_NICK,
556                                       conn->local_entry,
557                                       conn->local_entry->nickname,
558                                       &conn->local_entry->id);
559
560   /* Call JOIN command replies for all joined channel */
561   silc_idcache_get_all(conn->internal->channel_cache, &channels);
562   silc_list_start(channels);
563   while ((entry = silc_list_get(channels))) {
564     SilcHashTableList htl;
565     const char *cipher, *hmac;
566
567     channel = entry->context;
568     cipher = (channel->internal.send_key ?
569               silc_cipher_get_name(channel->internal.send_key) : NULL);
570     hmac = (channel->internal.hmac ?
571             silc_hmac_get_name(channel->internal.hmac) : NULL);
572     silc_hash_table_list(channel->user_list, &htl);
573     silc_client_resume_command_callback(client, conn, SILC_COMMAND_JOIN,
574                                         channel->channel_name, channel,
575                                         channel->mode, &htl, channel->topic,
576                                         cipher, hmac, channel->founder_key,
577                                         channel->channel_pubkeys,
578                                         channel->user_limit);
579     silc_hash_table_list_reset(&htl);
580   }
581
582   conn->internal->registering = FALSE;
583   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
584                                 silc_client_connect_timeout, conn);
585   silc_free(resume->nickname);
586   silc_free(resume);
587
588   return SILC_FSM_FINISH;
589 }
590
591 /* Error resuming to network */
592
593 SILC_FSM_STATE(silc_client_st_resume_error)
594 {
595   SilcClientConnection conn = fsm_context;
596   SilcClientResumeSession resume = state_context;
597
598   if (conn->internal->disconnected) {
599     if (resume) {
600       silc_free(resume->nickname);
601       silc_free(resume);
602     }
603     return SILC_FSM_FINISH;
604   }
605
606   SILC_LOG_DEBUG(("Error resuming to network"));
607
608   /* Signal to close connection */
609   conn->internal->status = SILC_CLIENT_CONN_ERROR;
610   if (!conn->internal->disconnected) {
611     conn->internal->disconnected = TRUE;
612     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
613   }
614
615   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
616                                 silc_client_connect_timeout, conn);
617
618   if (resume) {
619     silc_free(resume->nickname);
620     silc_free(resume);
621   }
622
623   return SILC_FSM_FINISH;
624 }
625
626 /* Generates the session detachment data. This data can be used later
627    to resume back to the server. */
628
629 SilcBuffer silc_client_get_detach_data(SilcClient client,
630                                        SilcClientConnection conn)
631 {
632   SilcBuffer detach;
633   SilcHashTableList htl;
634   SilcChannelUser chu;
635   unsigned char id[64];
636   SilcUInt32 id_len;
637   int ret, ch_count;
638
639   SILC_LOG_DEBUG(("Creating detachment data"));
640
641   ch_count = silc_hash_table_count(conn->local_entry->channels);
642   silc_id_id2str(conn->local_id, SILC_ID_CLIENT, id, sizeof(id), &id_len);
643
644   /* Save the nickname, Client ID and user mode in SILC network */
645   detach = silc_buffer_alloc(0);
646   if (!detach)
647     return NULL;
648   ret =
649     silc_buffer_format(detach,
650                        SILC_STR_ADVANCE,
651                        SILC_STR_UI_SHORT(strlen(conn->local_entry->nickname)),
652                        SILC_STR_DATA(conn->local_entry->nickname,
653                                      strlen(conn->local_entry->nickname)),
654                        SILC_STR_UI_SHORT(id_len),
655                        SILC_STR_DATA(id, id_len),
656                        SILC_STR_UI_INT(conn->local_entry->mode),
657                        SILC_STR_UI_INT(ch_count),
658                        SILC_STR_END);
659   if (ret < 0) {
660     silc_buffer_free(detach);
661     return NULL;
662   }
663
664   /* Save all joined channels */
665   silc_hash_table_list(conn->local_entry->channels, &htl);
666   while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
667     unsigned char chid[32];
668     SilcUInt32 chid_len;
669
670     silc_id_id2str(&chu->channel->id, SILC_ID_CHANNEL, chid, sizeof(chid),
671                    &chid_len);
672     silc_buffer_format(detach,
673                        SILC_STR_ADVANCE,
674                        SILC_STR_UI_SHORT(strlen(chu->channel->channel_name)),
675                        SILC_STR_DATA(chu->channel->channel_name,
676                                      strlen(chu->channel->channel_name)),
677                        SILC_STR_UI_SHORT(chid_len),
678                        SILC_STR_DATA(chid, chid_len),
679                        SILC_STR_UI_INT(chu->channel->mode),
680                        SILC_STR_END);
681   }
682   silc_hash_table_list_reset(&htl);
683
684   silc_buffer_start(detach);
685   SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(detach),
686                    silc_buffer_len(detach));
687
688   return detach;
689 }