updates.
[silc.git] / apps / silcd / packet_receive.c
1 /*
2
3   packet_receive.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /*
21  * Server packet routines to handle received packets.
22  */
23 /* $Id$ */
24
25 #include "serverincludes.h"
26 #include "server_internal.h"
27
28 extern char *server_version;
29
30 /* Received notify packet. Server can receive notify packets from router. 
31    Server then relays the notify messages to clients if needed. */
32
33 void silc_server_notify(SilcServer server,
34                         SilcSocketConnection sock,
35                         SilcPacketContext *packet)
36 {
37   SilcNotifyPayload payload;
38   SilcNotifyType type;
39   SilcArgumentPayload args;
40   SilcChannelID *channel_id;
41   SilcClientID *client_id, *client_id2;
42   SilcChannelEntry channel;
43   SilcClientEntry client;
44   SilcChannelClientEntry chl;
45   unsigned int mode;
46   unsigned char *tmp;
47   unsigned int tmp_len;
48
49   SILC_LOG_DEBUG(("Start"));
50
51   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
52       packet->src_id_type != SILC_ID_SERVER)
53     return;
54
55   /* If we are router and this packet is not already broadcast packet
56      we will broadcast it. The sending socket really cannot be router or
57      the router is buggy. If this packet is coming from router then it must
58      have the broadcast flag set already and we won't do anything. */
59   if (!server->standalone && server->server_type == SILC_ROUTER &&
60       sock->type == SILC_SOCKET_TYPE_SERVER &&
61       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
62     SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
63     silc_server_packet_send(server, server->router->connection, packet->type,
64                             packet->flags | SILC_PACKET_FLAG_BROADCAST, 
65                             packet->buffer->data, packet->buffer->len, FALSE);
66   }
67
68   payload = silc_notify_payload_parse(packet->buffer);
69   if (!payload)
70     return;
71
72   type = silc_notify_get_type(payload);
73   args = silc_notify_get_args(payload);
74   if (!args)
75     goto out;
76
77   switch(type) {
78   case SILC_NOTIFY_TYPE_JOIN:
79     /* 
80      * Distribute the notify to local clients on the channel
81      */
82     SILC_LOG_DEBUG(("JOIN notify"));
83
84     /* Get Channel ID */
85     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
86     if (!tmp)
87       goto out;
88     channel_id = silc_id_payload_parse_id(tmp, tmp_len);
89     if (!channel_id)
90       goto out;
91
92     /* Get channel entry */
93     channel = silc_idlist_find_channel_by_id(server->local_list, 
94                                              channel_id, NULL);
95     if (!channel) {
96       channel = silc_idlist_find_channel_by_id(server->global_list, 
97                                                channel_id, NULL);
98       if (!channel) {
99         silc_free(channel_id);
100         goto out;
101       }
102     }
103     silc_free(channel_id);
104
105     /* Get client ID */
106     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
107     if (!tmp)
108       goto out;
109     client_id = silc_id_payload_parse_id(tmp, tmp_len);
110     if (!client_id)
111       goto out;
112
113     /* Send to channel */
114     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
115                                        FALSE, packet->buffer->data, 
116                                        packet->buffer->len, FALSE);
117
118     /* If the the client is not in local list we check global list (ie. the
119        channel will be global channel) and if it does not exist then create
120        entry for the client. */
121     client = silc_idlist_find_client_by_id(server->local_list, 
122                                            client_id, NULL);
123     if (!client) {
124       client = silc_idlist_find_client_by_id(server->global_list, 
125                                              client_id, NULL);
126       if (!client) {
127         /* If router did not find the client the it is bogus */
128         if (server->server_type == SILC_ROUTER)
129           goto out;
130
131         client = 
132           silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
133                                  silc_id_dup(client_id, SILC_ID_CLIENT), 
134                                  sock->user_data, NULL);
135         if (!client) {
136           silc_free(client_id);
137           goto out;
138         }
139       }
140     }
141
142     /* Do not add client to channel if it is there already */
143     if (silc_server_client_on_channel(client, channel))
144       break;
145
146     if (server->server_type == SILC_SERVER && 
147         sock->type == SILC_SOCKET_TYPE_ROUTER)
148       /* The channel is global now */
149       channel->global_users = TRUE;
150
151     /* JOIN the global client to the channel (local clients (if router 
152        created the channel) is joined in the pending JOIN command). */
153     chl = silc_calloc(1, sizeof(*chl));
154     chl->client = client;
155     chl->channel = channel;
156     silc_list_add(channel->user_list, chl);
157     silc_list_add(client->channels, chl);
158     silc_free(client_id);
159
160     break;
161
162   case SILC_NOTIFY_TYPE_LEAVE:
163     /* 
164      * Distribute the notify to local clients on the channel
165      */
166     SILC_LOG_DEBUG(("LEAVE notify"));
167
168     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
169                                 packet->dst_id_type);
170     if (!channel_id)
171       goto out;
172
173     /* Get channel entry */
174     channel = silc_idlist_find_channel_by_id(server->local_list, 
175                                              channel_id, NULL);
176     if (!channel) { 
177       silc_free(channel_id);
178       goto out;
179     }
180
181     /* Get client ID */
182     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
183     if (!tmp) {
184       silc_free(channel_id);
185       goto out;
186     }
187     client_id = silc_id_payload_parse_id(tmp, tmp_len);
188     if (!client_id) {
189       silc_free(channel_id);
190       goto out;
191     }
192
193     /* Send to channel */
194     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
195                                        FALSE, packet->buffer->data, 
196                                        packet->buffer->len, FALSE);
197
198     /* Get client entry */
199     client = silc_idlist_find_client_by_id(server->global_list, 
200                                            client_id, NULL);
201     if (!client) {
202       client = silc_idlist_find_client_by_id(server->local_list, 
203                                              client_id, NULL);
204       if (!client) {
205         silc_free(client_id);
206         silc_free(channel_id);
207         goto out;
208       }
209     }
210     silc_free(client_id);
211
212     /* Remove the user from channel */
213     silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
214     break;
215
216   case SILC_NOTIFY_TYPE_SIGNOFF:
217     /* 
218      * Distribute the notify to local clients on the channel
219      */
220     SILC_LOG_DEBUG(("SIGNOFF notify"));
221
222     /* Get client ID */
223     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
224     if (!tmp)
225       goto out;
226     client_id = silc_id_payload_parse_id(tmp, tmp_len);
227     if (!client_id)
228       goto out;
229
230     /* Get client entry */
231     client = silc_idlist_find_client_by_id(server->global_list, 
232                                            client_id, NULL);
233     if (!client) {
234       client = silc_idlist_find_client_by_id(server->local_list, 
235                                              client_id, NULL);
236       if (!client) {
237         silc_free(client_id);
238         goto out;
239       }
240     }
241     silc_free(client_id);
242
243     /* Remove the client from all channels */
244     silc_server_remove_from_channels(server, NULL, client);
245
246     /* Remove the client entry */
247     if (!silc_idlist_del_client(server->global_list, client))
248       silc_idlist_del_client(server->local_list, client);
249     break;
250
251   case SILC_NOTIFY_TYPE_TOPIC_SET:
252     /* 
253      * Distribute the notify to local clients on the channel
254      */
255
256     SILC_LOG_DEBUG(("TOPIC SET notify"));
257
258     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
259                                 packet->dst_id_type);
260     if (!channel_id)
261       goto out;
262
263     /* Get channel entry */
264     channel = silc_idlist_find_channel_by_id(server->local_list, 
265                                              channel_id, NULL);
266     if (!channel) {
267       channel = silc_idlist_find_channel_by_id(server->global_list, 
268                                                channel_id, NULL);
269       if (!channel) {
270         silc_free(channel_id);
271         goto out;
272       }
273     }
274
275     /* Get the topic */
276     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
277     if (!tmp) {
278       silc_free(channel_id);
279       goto out;
280     }
281
282     if (channel->topic)
283       silc_free(channel->topic);
284     channel->topic = silc_calloc(tmp_len, sizeof(*channel->topic));
285     memcpy(channel->topic, tmp, tmp_len);
286
287     /* Send the same notify to the channel */
288     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
289                                        FALSE, packet->buffer->data, 
290                                        packet->buffer->len, FALSE);
291     silc_free(channel_id);
292     break;
293
294   case SILC_NOTIFY_TYPE_NICK_CHANGE:
295     {
296       /* 
297        * Distribute the notify to local clients on the channel
298        */
299       unsigned char *id, *id2;
300
301       SILC_LOG_DEBUG(("NICK CHANGE notify"));
302       
303       /* Get old client ID */
304       id = silc_argument_get_arg_type(args, 1, &tmp_len);
305       if (!id)
306         goto out;
307       client_id = silc_id_payload_parse_id(id, tmp_len);
308       if (!client_id)
309         goto out;
310       
311       /* Get new client ID */
312       id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
313       if (!id2)
314         goto out;
315       client_id2 = silc_id_payload_parse_id(id2, tmp_len);
316       if (!client_id2)
317         goto out;
318       
319       SILC_LOG_DEBUG(("Old Client ID id(%s)", 
320                       silc_id_render(client_id, SILC_ID_CLIENT)));
321       SILC_LOG_DEBUG(("New Client ID id(%s)", 
322                       silc_id_render(client_id2, SILC_ID_CLIENT)));
323
324       /* Replace the Client ID */
325       client = silc_idlist_replace_client_id(server->global_list, client_id,
326                                              client_id2);
327       if (!client)
328         client = silc_idlist_replace_client_id(server->local_list, client_id, 
329                                                client_id2);
330
331       if (client) {
332         /* The nickname is not valid anymore, set it NULL. This causes that
333            the nickname will be queried if someone wants to know it. */
334         if (client->nickname)
335           silc_free(client->nickname);
336         client->nickname = NULL;
337
338         /* Send the NICK_CHANGE notify type to local clients on the channels
339            this client is joined to. */
340         silc_server_send_notify_on_channels(server, client, 
341                                             SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
342                                             id, tmp_len, 
343                                             id2, tmp_len);
344       }
345
346       silc_free(client_id);
347       if (!client)
348         silc_free(client_id2);
349       break;
350     }
351
352   case SILC_NOTIFY_TYPE_CMODE_CHANGE:
353     /* 
354      * Distribute the notify to local clients on the channel
355      */
356     
357     SILC_LOG_DEBUG(("CMODE CHANGE notify"));
358       
359     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
360                                 packet->dst_id_type);
361     if (!channel_id)
362       goto out;
363
364     /* Get channel entry */
365     channel = silc_idlist_find_channel_by_id(server->local_list, 
366                                              channel_id, NULL);
367     if (!channel) {
368       channel = silc_idlist_find_channel_by_id(server->global_list, 
369                                                channel_id, NULL);
370       if (!channel) {
371         silc_free(channel_id);
372         goto out;
373       }
374     }
375
376     /* Send the same notify to the channel */
377     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
378                                        FALSE, packet->buffer->data, 
379                                        packet->buffer->len, FALSE);
380
381     /* Get the mode */
382     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
383     if (!tmp) {
384       silc_free(channel_id);
385       goto out;
386     }
387
388     SILC_GET32_MSB(mode, tmp);
389
390     /* Change mode */
391     channel->mode = mode;
392     silc_free(channel_id);
393     break;
394
395   case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
396     /* 
397      * Distribute the notify to local clients on the channel
398      */
399
400     SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
401
402     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
403                                 packet->dst_id_type);
404     if (!channel_id)
405       goto out;
406
407     /* Get channel entry */
408     channel = silc_idlist_find_channel_by_id(server->local_list, 
409                                              channel_id, NULL);
410     if (!channel) {
411       channel = silc_idlist_find_channel_by_id(server->global_list, 
412                                                channel_id, NULL);
413       if (!channel) {
414         silc_free(channel_id);
415         goto out;
416       }
417     }
418
419     /* Get the mode */
420     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
421     if (!tmp) {
422       silc_free(channel_id);
423       goto out;
424     }
425       
426     SILC_GET32_MSB(mode, tmp);
427
428     /* Get target client */
429     tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
430     if (!tmp)
431       goto out;
432     client_id = silc_id_payload_parse_id(tmp, tmp_len);
433     if (!client_id)
434       goto out;
435     
436     /* Get client entry */
437     client = silc_idlist_find_client_by_id(server->global_list, 
438                                            client_id, NULL);
439     if (!client) {
440       client = silc_idlist_find_client_by_id(server->local_list, 
441                                              client_id, NULL);
442       if (!client) {
443         silc_free(client_id);
444         goto out;
445       }
446     }
447     silc_free(client_id);
448
449     /* Get entry to the channel user list */
450     silc_list_start(channel->user_list);
451     while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
452       if (chl->client == client) {
453         /* Change the mode */
454         chl->mode = mode;
455         break;
456       }
457
458     /* Send the same notify to the channel */
459     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
460                                        FALSE, packet->buffer->data, 
461                                        packet->buffer->len, FALSE);
462     silc_free(channel_id);
463     break;
464
465   case SILC_NOTIFY_TYPE_INVITE:
466     SILC_LOG_DEBUG(("INVITE notify (not-impl XXX)"));
467     break;
468
469   case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
470     SILC_LOG_DEBUG(("CHANNEL CHANGE notify (not-impl XXX)"));
471     break;
472
473   case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
474     SILC_LOG_DEBUG(("SERVER SIGNOFF notify (not-impl XXX)"));
475     break;
476
477     /* Ignore rest of the notify types for now */
478   case SILC_NOTIFY_TYPE_NONE:
479   case SILC_NOTIFY_TYPE_MOTD:
480     break;
481   default:
482     break;
483   }
484
485  out:
486   silc_notify_payload_free(payload);
487 }
488
489 void silc_server_notify_list(SilcServer server,
490                              SilcSocketConnection sock,
491                              SilcPacketContext *packet)
492 {
493   SilcPacketContext *new;
494   SilcBuffer buffer;
495   unsigned short len;
496
497   SILC_LOG_DEBUG(("Processing New Notify List"));
498
499   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
500       packet->src_id_type != SILC_ID_SERVER)
501     return;
502
503   /* Make copy of the original packet context, except for the actual
504      data buffer, which we will here now fetch from the original buffer. */
505   new = silc_packet_context_alloc();
506   new->type = SILC_PACKET_NOTIFY;
507   new->flags = packet->flags;
508   new->src_id = packet->src_id;
509   new->src_id_len = packet->src_id_len;
510   new->src_id_type = packet->src_id_type;
511   new->dst_id = packet->dst_id;
512   new->dst_id_len = packet->dst_id_len;
513   new->dst_id_type = packet->dst_id_type;
514
515   buffer = silc_buffer_alloc(1024);
516   new->buffer = buffer;
517
518   while (packet->buffer->len) {
519     SILC_GET16_MSB(len, packet->buffer->data + 2);
520     if (len > packet->buffer->len)
521       break;
522
523     if (len > buffer->truelen) {
524       silc_buffer_free(buffer);
525       buffer = silc_buffer_alloc(1024 + len);
526     }
527
528     silc_buffer_pull_tail(buffer, len);
529     silc_buffer_put(buffer, packet->buffer->data, len);
530
531     /* Process the Notify */
532     silc_server_notify(server, sock, new);
533
534     silc_buffer_push_tail(buffer, len);
535     silc_buffer_pull(packet->buffer, len);
536   }
537
538   silc_buffer_free(buffer);
539   silc_free(new);
540 }
541
542 /* Received private message. This resolves the destination of the message 
543    and sends the packet. This is used by both server and router.  If the
544    destination is our locally connected client this sends the packet to
545    the client. This may also send the message for further routing if
546    the destination is not in our server (or router). */
547
548 void silc_server_private_message(SilcServer server,
549                                  SilcSocketConnection sock,
550                                  SilcPacketContext *packet)
551 {
552   SilcClientID *id;
553   SilcServerEntry router;
554   SilcSocketConnection dst_sock;
555   SilcClientEntry client;
556   SilcIDListData idata;
557
558   SILC_LOG_DEBUG(("Start"));
559
560   if (!packet->dst_id)
561     goto err;
562
563   /* Decode destination Client ID */
564   id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
565   if (!id) {
566     SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
567     goto err;
568   }
569
570   /* If the destination belongs to our server we don't have to route
571      the message anywhere but to send it to the local destination. */
572   client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
573   if (client) {
574     /* It exists, now deliver the message to the destination */
575     dst_sock = (SilcSocketConnection)client->connection;
576
577     /* If we are router and the client has router then the client is in
578        our cell but not directly connected to us. */
579     if (server->server_type == SILC_ROUTER && client->router) {
580       /* We are of course in this case the client's router thus the real
581          "router" of the client is the server who owns the client. Thus
582          we will send the packet to that server. */
583       router = (SilcServerEntry)client->router;
584       idata = (SilcIDListData)router;
585
586       silc_server_send_private_message(server, router->connection,
587                                        idata->send_key,
588                                        idata->hmac,
589                                        packet);
590       return;
591     }
592
593     /* Seems that client really is directly connected to us */
594     idata = (SilcIDListData)client;
595     silc_server_send_private_message(server, dst_sock, 
596                                      idata->send_key,
597                                      idata->hmac, packet);
598     return;
599   }
600
601   /* Destination belongs to someone not in this server. If we are normal
602      server our action is to send the packet to our router. */
603   if (server->server_type == SILC_SERVER && !server->standalone) {
604     router = server->router;
605
606     /* Send to primary route */
607     if (router) {
608       dst_sock = (SilcSocketConnection)router->connection;
609       idata = (SilcIDListData)router;
610       silc_server_send_private_message(server, dst_sock, 
611                                        idata->send_key,
612                                        idata->hmac, packet);
613     }
614     return;
615   }
616
617   /* We are router and we will perform route lookup for the destination 
618      and send the message to fastest route. */
619   if (server->server_type == SILC_ROUTER && !server->standalone) {
620     /* Check first that the ID is valid */
621     client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
622     if (client) {
623       dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
624       router = (SilcServerEntry)dst_sock->user_data;
625       idata = (SilcIDListData)router;
626
627       /* Get fastest route and send packet. */
628       if (router)
629         silc_server_send_private_message(server, dst_sock, 
630                                          idata->send_key,
631                                          idata->hmac, packet);
632       return;
633     }
634   }
635
636  err:
637   silc_server_send_error(server, sock, 
638                          "No such nickname: Private message not sent");
639 }
640
641 /* Processes incoming command reply packet. The command reply packet may
642    be destined to one of our clients or it may directly for us. We will 
643    call the command reply routine after processing the packet. */
644
645 void silc_server_command_reply(SilcServer server,
646                                SilcSocketConnection sock,
647                                SilcPacketContext *packet)
648 {
649   SilcBuffer buffer = packet->buffer;
650   SilcClientEntry client = NULL;
651   SilcSocketConnection dst_sock;
652   SilcIDListData idata;
653   SilcClientID *id = NULL;
654
655   SILC_LOG_DEBUG(("Start"));
656
657   /* Source must be server or router */
658   if (packet->src_id_type != SILC_ID_SERVER &&
659       sock->type != SILC_SOCKET_TYPE_ROUTER)
660     return;
661
662   if (packet->dst_id_type == SILC_ID_CHANNEL)
663     return;
664
665   if (packet->dst_id_type == SILC_ID_CLIENT) {
666     /* Destination must be one of ours */
667     id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
668     if (!id)
669       return;
670     client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
671     if (!client) {
672       SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
673       silc_free(id);
674       return;
675     }
676   }
677
678   if (packet->dst_id_type == SILC_ID_SERVER) {
679     /* For now this must be for us */
680     if (SILC_ID_SERVER_COMPARE(packet->dst_id, server->id_string)) {
681       SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
682       return;
683     }
684   }
685
686   /* Execute command reply locally for the command */
687   silc_server_command_reply_process(server, sock, buffer);
688
689   if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
690     /* Relay the packet to the client */
691     
692     dst_sock = (SilcSocketConnection)client->connection;
693     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
694                      + packet->dst_id_len + packet->padlen);
695     
696     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
697     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
698     
699     idata = (SilcIDListData)client;
700     
701     /* Encrypt packet */
702     silc_packet_encrypt(idata->send_key, idata->hmac, dst_sock->outbuf, 
703                         buffer->len);
704     
705     /* Send the packet */
706     silc_server_packet_send_real(server, dst_sock, TRUE);
707
708     silc_free(id);
709   }
710 }
711
712 /* Process received channel message. The message can be originated from
713    client or server. */
714
715 void silc_server_channel_message(SilcServer server,
716                                  SilcSocketConnection sock,
717                                  SilcPacketContext *packet)
718 {
719   SilcChannelEntry channel = NULL;
720   SilcChannelClientEntry chl;
721   SilcChannelID *id = NULL;
722   void *sender = NULL;
723
724   SILC_LOG_DEBUG(("Processing channel message"));
725
726   /* Sanity checks */
727   if (packet->dst_id_type != SILC_ID_CHANNEL) {
728     SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
729     goto out;
730   }
731
732   /* Find channel entry */
733   id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
734   if (!id)
735     goto out;
736   channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
737   if (!channel) {
738     channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
739     if (!channel) {
740       SILC_LOG_DEBUG(("Could not find channel"));
741       goto out;
742     }
743   }
744
745   /* See that this client is on the channel. If the message is coming
746      from router we won't do the check as the message is from client that
747      we don't know about. Also, if the original sender is not client
748      (as it can be server as well) we don't do the check. */
749   sender = silc_id_str2id(packet->src_id, packet->src_id_len, 
750                           packet->src_id_type);
751   if (!sender)
752     goto out;
753   if (sock->type != SILC_SOCKET_TYPE_ROUTER && 
754       packet->src_id_type == SILC_ID_CLIENT) {
755     silc_list_start(channel->user_list);
756     while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
757       if (chl->client && !SILC_ID_CLIENT_COMPARE(chl->client->id, sender))
758         break;
759     }
760     if (chl == SILC_LIST_END) {
761       SILC_LOG_DEBUG(("Client not on channel"));
762       goto out;
763     }
764   }
765
766   /* Distribute the packet to our local clients. This will send the
767      packet for further routing as well, if needed. */
768   silc_server_packet_relay_to_channel(server, sock, channel, sender,
769                                       packet->src_id_type,
770                                       packet->buffer->data,
771                                       packet->buffer->len, FALSE);
772
773  out:
774   if (sender)
775     silc_free(sender);
776   if (id)
777     silc_free(id);
778 }
779
780 /* Received channel key packet. We distribute the key to all of our locally
781    connected clients on the channel. */
782
783 void silc_server_channel_key(SilcServer server,
784                              SilcSocketConnection sock,
785                              SilcPacketContext *packet)
786 {
787   SilcBuffer buffer = packet->buffer;
788   SilcChannelEntry channel;
789
790   if (packet->src_id_type != SILC_ID_SERVER)
791     return;
792
793   /* Save the channel key */
794   channel = silc_server_save_channel_key(server, buffer, NULL);
795   if (!channel)
796     return;
797
798   /* Distribute the key to everybody who is on the channel. If we are router
799      we will also send it to locally connected servers. */
800   silc_server_send_channel_key(server, sock, channel, FALSE);
801 }
802
803 /* Received New Client packet and processes it.  Creates Client ID for the
804    client. Client becomes registered after calling this functions. */
805
806 SilcClientEntry silc_server_new_client(SilcServer server,
807                                        SilcSocketConnection sock,
808                                        SilcPacketContext *packet)
809 {
810   SilcBuffer buffer = packet->buffer;
811   SilcClientEntry client;
812   SilcIDCacheEntry cache;
813   SilcClientID *client_id;
814   SilcBuffer reply;
815   SilcIDListData idata;
816   char *username = NULL, *realname = NULL, *id_string;
817   int ret;
818
819   SILC_LOG_DEBUG(("Creating new client"));
820
821   if (sock->type != SILC_SOCKET_TYPE_CLIENT)
822     return NULL;
823
824   /* Take client entry */
825   client = (SilcClientEntry)sock->user_data;
826   idata = (SilcIDListData)client;
827
828   /* Fetch the old client cache entry so that we can update it. */
829   if (!silc_idcache_find_by_context(server->local_list->clients,
830                                     sock->user_data, &cache)) {
831     SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
832     return NULL;
833   }
834
835   /* Parse incoming packet */
836   ret = silc_buffer_unformat(buffer,
837                              SILC_STR_UI16_STRING_ALLOC(&username),
838                              SILC_STR_UI16_STRING_ALLOC(&realname),
839                              SILC_STR_END);
840   if (ret == -1) {
841     if (username)
842       silc_free(username);
843     if (realname)
844       silc_free(realname);
845     return NULL;
846   }
847
848   if (!username) {
849     silc_free(username);
850     if (realname)
851       silc_free(realname);
852     silc_server_disconnect_remote(server, sock, "Server closed connection: "
853                                   "Incomplete client information");
854     return NULL;
855   }
856
857   /* Create Client ID */
858   silc_id_create_client_id(server->id, server->rng, server->md5hash,
859                            username, &client_id);
860
861   /* Update client entry */
862   idata->registered = TRUE;
863   client->nickname = strdup(username);
864   client->username = username;
865   client->userinfo = realname ? realname : strdup("");
866   client->id = client_id;
867
868   /* Update the cache entry */
869   cache->id = (void *)client_id;
870   cache->type = SILC_ID_CLIENT;
871   cache->data = username;
872   silc_idcache_sort_by_data(server->local_list->clients);
873
874   /* Notify our router about new client on the SILC network */
875   if (!server->standalone)
876     silc_server_send_new_id(server, (SilcSocketConnection) 
877                             server->router->connection, 
878                             server->server_type == SILC_ROUTER ? TRUE : FALSE,
879                             client->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
880   
881   /* Send the new client ID to the client. */
882   id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
883   reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
884   silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
885   silc_buffer_format(reply,
886                      SILC_STR_UI_SHORT(SILC_ID_CLIENT),
887                      SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
888                      SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
889                      SILC_STR_END);
890   silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0, 
891                           reply->data, reply->len, FALSE);
892   silc_free(id_string);
893   silc_buffer_free(reply);
894
895   /* Send some nice info to the client */
896   SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
897                           ("Welcome to the SILC Network %s@%s",
898                            username, sock->hostname));
899   SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
900                           ("Your host is %s, running version %s",
901                            server->config->server_info->server_name,
902                            server_version));
903   if (server->server_type == SILC_ROUTER) {
904     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
905                             ("There are %d clients on %d servers in SILC "
906                              "Network", server->stat.clients,
907                              server->stat.servers + 1));
908     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
909                             ("There are %d clients on %d server in our cell",
910                              server->stat.cell_clients,
911                              server->stat.cell_servers + 1));
912     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
913                             ("I have %d clients, %d channels, %d servers and "
914                              "%d routers",
915                              server->stat.my_clients, 
916                              server->stat.my_channels,
917                              server->stat.my_servers,
918                              server->stat.my_routers));
919     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
920                             ("%d server operators and %d router operators "
921                              "online",
922                              server->stat.my_server_ops,
923                              server->stat.my_router_ops));
924   } else {
925     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
926                             ("I have %d clients and %d channels formed",
927                              server->stat.my_clients,
928                              server->stat.my_channels));
929     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
930                             ("%d operators online",
931                              server->stat.my_server_ops));
932   }
933   SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
934                           ("Your connection is secured with %s cipher, "
935                            "key length %d bits",
936                            idata->send_key->cipher->name,
937                            idata->send_key->cipher->key_len));
938   SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
939                           ("Your current nickname is %s",
940                            client->nickname));
941
942   /* Send motd */
943   silc_server_send_motd(server, sock);
944
945   return client;
946 }
947
948 /* Create new server. This processes received New Server packet and
949    saves the received Server ID. The server is our locally connected
950    server thus we save all the information and save it to local list. 
951    This funtion can be used by both normal server and router server.
952    If normal server uses this it means that its router has connected
953    to the server. If router uses this it means that one of the cell's
954    servers is connected to the router. */
955
956 SilcServerEntry silc_server_new_server(SilcServer server,
957                                        SilcSocketConnection sock,
958                                        SilcPacketContext *packet)
959 {
960   SilcBuffer buffer = packet->buffer;
961   SilcServerEntry new_server;
962   SilcIDCacheEntry cache;
963   SilcServerID *server_id;
964   SilcIDListData idata;
965   unsigned char *server_name, *id_string;
966   unsigned short id_len;
967   int ret;
968
969   SILC_LOG_DEBUG(("Creating new server"));
970
971   if (sock->type != SILC_SOCKET_TYPE_SERVER &&
972       sock->type != SILC_SOCKET_TYPE_ROUTER)
973     return NULL;
974
975   /* Take server entry */
976   new_server = (SilcServerEntry)sock->user_data;
977   idata = (SilcIDListData)new_server;
978
979   /* Fetch the old server cache entry so that we can update it. */
980   if (!silc_idcache_find_by_context(server->local_list->servers,
981                                     sock->user_data, &cache)) {
982     SILC_LOG_ERROR(("Lost server's cache entry - bad thing"));
983     return NULL;
984   }
985
986   /* Parse the incoming packet */
987   ret = silc_buffer_unformat(buffer,
988                              SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
989                              SILC_STR_UI16_STRING_ALLOC(&server_name),
990                              SILC_STR_END);
991   if (ret == -1) {
992     if (id_string)
993       silc_free(id_string);
994     if (server_name)
995       silc_free(server_name);
996     return NULL;
997   }
998
999   if (id_len > buffer->len) {
1000     silc_free(id_string);
1001     silc_free(server_name);
1002     return NULL;
1003   }
1004
1005   /* Get Server ID */
1006   server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1007   if (!server_id) {
1008     silc_free(id_string);
1009     silc_free(server_name);
1010     return NULL;
1011   }
1012   silc_free(id_string);
1013
1014   /* Update client entry */
1015   idata->registered = TRUE;
1016   new_server->server_name = server_name;
1017   new_server->id = server_id;
1018
1019   /* Update the cache entry */
1020   cache->id = (void *)server_id;
1021   cache->type = SILC_ID_SERVER;
1022   cache->data = server_name;
1023   silc_idcache_sort_by_data(server->local_list->servers);
1024
1025   /* Distribute the information about new server in the SILC network
1026      to our router. If we are normal server we won't send anything
1027      since this connection must be our router connection. */
1028   if (server->server_type == SILC_ROUTER && !server->standalone &&
1029       server->router->connection != sock)
1030     silc_server_send_new_id(server, server->router->connection,
1031                             TRUE, new_server->id, SILC_ID_SERVER, 
1032                             SILC_ID_SERVER_LEN);
1033
1034   if (server->server_type == SILC_ROUTER)
1035     server->stat.cell_servers++;
1036
1037   return new_server;
1038 }
1039
1040 /* Processes incoming New ID packet. New ID Payload is used to distribute
1041    information about newly registered clients and servers. */
1042
1043 static void silc_server_new_id_real(SilcServer server, 
1044                                     SilcSocketConnection sock,
1045                                     SilcPacketContext *packet,
1046                                     int broadcast)
1047 {
1048   SilcBuffer buffer = packet->buffer;
1049   SilcIDList id_list;
1050   SilcServerEntry router;
1051   SilcSocketConnection router_sock;
1052   SilcIDPayload idp;
1053   SilcIdType id_type;
1054   unsigned char *hash = NULL;
1055   void *id;
1056
1057   SILC_LOG_DEBUG(("Processing new ID"));
1058
1059   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1060       server->server_type == SILC_SERVER ||
1061       packet->src_id_type != SILC_ID_SERVER)
1062     return;
1063
1064   idp = silc_id_payload_parse(buffer);
1065   if (!idp)
1066     return;
1067
1068   id_type = silc_id_payload_get_type(idp);
1069
1070   /* Normal server cannot have other normal server connections */
1071   if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
1072     goto out;
1073
1074   id = silc_id_payload_get_id(idp);
1075   if (!id)
1076     goto out;
1077
1078   /* If the sender of this packet is server and we are router we need to
1079      broadcast this packet to other routers in the network. */
1080   if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1081       sock->type == SILC_SOCKET_TYPE_SERVER &&
1082       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1083     SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
1084     silc_server_packet_send(server, server->router->connection,
1085                             packet->type, 
1086                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
1087                             buffer->data, buffer->len, FALSE);
1088   }
1089
1090   if (sock->type == SILC_SOCKET_TYPE_SERVER)
1091     id_list = server->local_list;
1092   else
1093     id_list = server->global_list;
1094
1095   router_sock = sock;
1096   router = sock->user_data;
1097
1098   switch(id_type) {
1099   case SILC_ID_CLIENT:
1100     {
1101       SilcClientEntry entry;
1102
1103       SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1104                       silc_id_render(id, SILC_ID_CLIENT),
1105                       sock->type == SILC_SOCKET_TYPE_SERVER ?
1106                       "Server" : "Router", sock->hostname));
1107     
1108       /* As a router we keep information of all global information in our
1109          global list. Cell wide information however is kept in the local
1110          list. The client is put to global list and we will take the hash
1111          value of the Client ID and save it to the ID Cache system for fast
1112          searching in the future. */
1113       hash = silc_calloc(sizeof(((SilcClientID *)id)->hash), 
1114                          sizeof(unsigned char));
1115       memcpy(hash, ((SilcClientID *)id)->hash, 
1116              sizeof(((SilcClientID *)id)->hash));
1117       entry = silc_idlist_add_client(id_list, hash, NULL, NULL, id, 
1118                                      router, NULL);
1119       entry->nickname = NULL;
1120
1121       if (sock->type == SILC_SOCKET_TYPE_SERVER)
1122         server->stat.cell_clients++;
1123       server->stat.clients++;
1124
1125 #if 0
1126       /* XXX Adding two ID's with same IP number replaces the old entry thus
1127          gives wrong route. Thus, now disabled until figured out a better way
1128          to do this or when removed the whole thing. This could be removed
1129          because entry->router->connection gives always the most optimal route
1130          for the ID anyway (unless new routes (faster perhaps) are established
1131          after receiving this ID, this we don't know however). */
1132       /* Add route cache for this ID */
1133       silc_server_route_add(silc_server_route_hash(
1134                             ((SilcClientID *)id)->ip.s_addr,
1135                             server->id->port), ((SilcClientID *)id)->ip.s_addr,
1136                             router);
1137 #endif
1138     }
1139     break;
1140
1141   case SILC_ID_SERVER:
1142     SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1143                     silc_id_render(id, SILC_ID_SERVER),
1144                     sock->type == SILC_SOCKET_TYPE_SERVER ?
1145                     "Server" : "Router", sock->hostname));
1146     
1147     /* As a router we keep information of all global information in our global
1148        list. Cell wide information however is kept in the local list. */
1149     silc_idlist_add_server(id_list, NULL, 0, id, router, router_sock);
1150
1151     if (sock->type == SILC_SOCKET_TYPE_SERVER)
1152       server->stat.cell_servers++;
1153     server->stat.servers++;
1154
1155 #if 0
1156     /* Add route cache for this ID */
1157     silc_server_route_add(silc_server_route_hash(
1158                           ((SilcServerID *)id)->ip.s_addr,
1159                           ((SilcServerID *)id)->port), 
1160                           ((SilcServerID *)id)->ip.s_addr,
1161                           router);
1162 #endif
1163     break;
1164
1165   case SILC_ID_CHANNEL:
1166     SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
1167     break;
1168
1169   default:
1170     break;
1171   }
1172
1173  out:
1174   silc_id_payload_free(idp);
1175 }
1176
1177
1178 /* Processes incoming New ID packet. New ID Payload is used to distribute
1179    information about newly registered clients and servers. */
1180
1181 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
1182                         SilcPacketContext *packet)
1183 {
1184   silc_server_new_id_real(server, sock, packet, TRUE);
1185 }
1186
1187 /* Receoved New Id List packet, list of New ID payloads inside one
1188    packet. Process the New ID payloads one by one. */
1189
1190 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
1191                              SilcPacketContext *packet)
1192 {
1193   SilcPacketContext *new_id;
1194   SilcBuffer idp;
1195   unsigned short id_len;
1196
1197   SILC_LOG_DEBUG(("Processing New ID List"));
1198
1199   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1200       packet->src_id_type != SILC_ID_SERVER)
1201     return;
1202
1203   /* If the sender of this packet is server and we are router we need to
1204      broadcast this packet to other routers in the network. Broadcast
1205      this list packet instead of multiple New ID packets. */
1206   if (!server->standalone && server->server_type == SILC_ROUTER &&
1207       sock->type == SILC_SOCKET_TYPE_SERVER &&
1208       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1209     SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
1210     silc_server_packet_send(server, server->router->connection,
1211                             packet->type, 
1212                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
1213                             packet->buffer->data, packet->buffer->len, FALSE);
1214   }
1215
1216   /* Make copy of the original packet context, except for the actual
1217      data buffer, which we will here now fetch from the original buffer. */
1218   new_id = silc_packet_context_alloc();
1219   new_id->type = SILC_PACKET_NEW_ID;
1220   new_id->flags = packet->flags;
1221   new_id->src_id = packet->src_id;
1222   new_id->src_id_len = packet->src_id_len;
1223   new_id->src_id_type = packet->src_id_type;
1224   new_id->dst_id = packet->dst_id;
1225   new_id->dst_id_len = packet->dst_id_len;
1226   new_id->dst_id_type = packet->dst_id_type;
1227
1228   idp = silc_buffer_alloc(256);
1229   new_id->buffer = idp;
1230
1231   while (packet->buffer->len) {
1232     SILC_GET16_MSB(id_len, packet->buffer->data + 2);
1233     if ((id_len > packet->buffer->len) ||
1234         (id_len > idp->truelen))
1235       break;
1236
1237     silc_buffer_pull_tail(idp, 4 + id_len);
1238     silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
1239
1240     /* Process the New ID */
1241     silc_server_new_id_real(server, sock, new_id, FALSE);
1242
1243     silc_buffer_push_tail(idp, 4 + id_len);
1244     silc_buffer_pull(packet->buffer, 4 + id_len);
1245   }
1246
1247   silc_buffer_free(idp);
1248   silc_free(new_id);
1249 }
1250
1251 /* Received New Channel packet. Information about new channels in the 
1252    network are distributed using this packet. Save the information about
1253    the new channel. This usually comes from router but also normal server
1254    can send this to notify channels it has when it connects to us. */
1255
1256 void silc_server_new_channel(SilcServer server,
1257                              SilcSocketConnection sock,
1258                              SilcPacketContext *packet)
1259 {
1260   unsigned char *id;
1261   SilcChannelID *channel_id;
1262   unsigned short channel_id_len;
1263   char *channel_name;
1264   int ret;
1265
1266   SILC_LOG_DEBUG(("Processing New Channel"));
1267
1268   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1269       packet->src_id_type != SILC_ID_SERVER ||
1270       server->server_type == SILC_SERVER)
1271     return;
1272
1273   /* Parse payload */
1274   ret = silc_buffer_unformat(packet->buffer, 
1275                              SILC_STR_UI16_STRING_ALLOC(&channel_name),
1276                              SILC_STR_UI16_NSTRING_ALLOC(&id, &channel_id_len),
1277                              SILC_STR_END);
1278   if (ret == -1) {
1279     if (channel_name)
1280       silc_free(channel_name);
1281     if (id)
1282       silc_free(id);
1283     return;
1284   }
1285     
1286   /* Decode the channel ID */
1287   channel_id = silc_id_str2id(id, channel_id_len, SILC_ID_CHANNEL);
1288   if (!channel_id)
1289     return;
1290
1291   if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
1292     /* Add the server to global list as it is coming from router. It 
1293        cannot be our own channel as it is coming from router. */
1294
1295     SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
1296                     silc_id_render(channel_id, SILC_ID_CHANNEL), 
1297                     sock->hostname));
1298     
1299     silc_idlist_add_channel(server->global_list, channel_name, 0, channel_id, 
1300                             server->router->connection, NULL);
1301
1302     server->stat.channels++;
1303   } else {
1304     /* The channel is coming from our server, thus it is in our cell
1305        we will add it to our local list. */
1306     SilcChannelEntry channel;
1307     SilcBuffer chk;
1308
1309     SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
1310                     silc_id_render(channel_id, SILC_ID_CHANNEL), 
1311                     sock->hostname));
1312     
1313     /* Check that we don't already have this channel */
1314     channel = silc_idlist_find_channel_by_name(server->local_list, 
1315                                                channel_name, NULL);
1316     if (!channel)
1317       channel = silc_idlist_find_channel_by_name(server->global_list, 
1318                                                  channel_name, NULL);
1319
1320     /* If the channel does not exist, then create it. We create the channel
1321        with the channel ID provided by the server. This creates a new
1322        key to the channel as well that we will send to the server. */
1323     if (!channel) {
1324       channel = silc_server_create_new_channel_with_id(server, NULL,
1325                                                        channel_name,
1326                                                        channel_id, FALSE);
1327       if (!channel)
1328         return;
1329
1330       /* Send the new channel key to the server */
1331       chk = silc_channel_key_payload_encode(channel_id_len, id,
1332                                             strlen(channel->channel_key->
1333                                                    cipher->name),
1334                                             channel->channel_key->cipher->name,
1335                                             channel->key_len / 8, 
1336                                             channel->key);
1337       silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0, 
1338                               chk->data, chk->len, FALSE);
1339       silc_buffer_free(chk);
1340
1341     } else {
1342       /* The channel exist by that name, check whether the ID's match.
1343          If they don't then we'll force the server to use the ID we have.
1344          We also create a new key for the channel. */
1345
1346       if (SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
1347         /* They don't match, send CHANNEL_CHANGE notify to the server to
1348            force the ID change. */
1349         SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
1350         silc_server_send_notify_channel_change(server, sock, FALSE, 
1351                                                channel_id,
1352                                                channel->id, 
1353                                                SILC_ID_CHANNEL_LEN);
1354       }
1355
1356       /* Create new key for the channel and send it to the server and
1357          everybody else possibly on the channel. */
1358
1359       silc_server_create_channel_key(server, channel, 0);
1360
1361       /* Send to the channel */
1362       silc_server_send_channel_key(server, sock, channel, FALSE);
1363
1364       /* Send to the server */
1365       chk = silc_channel_key_payload_encode(channel_id_len, id,
1366                                             strlen(channel->channel_key->
1367                                                    cipher->name),
1368                                             channel->channel_key->cipher->name,
1369                                             channel->key_len / 8, 
1370                                             channel->key);
1371       silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0, 
1372                               chk->data, chk->len, FALSE);
1373       silc_buffer_free(chk);
1374
1375       /* Since the channel is coming from server and we also know about it
1376          then send the JOIN notify to the server so that it see's our
1377          users on the channel "joining" the channel. */
1378       /* XXX TODO **/
1379     }
1380   }
1381
1382   silc_free(id);
1383 }
1384
1385 /* Received New Channel List packet, list of New Channel List payloads inside
1386    one packet. Process the New Channel payloads one by one. */
1387
1388 void silc_server_new_channel_list(SilcServer server,
1389                                   SilcSocketConnection sock,
1390                                   SilcPacketContext *packet)
1391 {
1392   SilcPacketContext *new;
1393   SilcBuffer buffer;
1394   unsigned short len1, len2;
1395
1396   SILC_LOG_DEBUG(("Processing New Channel List"));
1397
1398   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1399       packet->src_id_type != SILC_ID_SERVER ||
1400       server->server_type == SILC_SERVER)
1401     return;
1402
1403   /* If the sender of this packet is server and we are router we need to
1404      broadcast this packet to other routers in the network. Broadcast
1405      this list packet instead of multiple New Channel packets. */
1406   if (!server->standalone && server->server_type == SILC_ROUTER &&
1407       sock->type == SILC_SOCKET_TYPE_SERVER &&
1408       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1409     SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
1410     silc_server_packet_send(server, server->router->connection,
1411                             packet->type, 
1412                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
1413                             packet->buffer->data, packet->buffer->len, FALSE);
1414   }
1415
1416   /* Make copy of the original packet context, except for the actual
1417      data buffer, which we will here now fetch from the original buffer. */
1418   new = silc_packet_context_alloc();
1419   new->type = SILC_PACKET_NEW_CHANNEL;
1420   new->flags = packet->flags;
1421   new->src_id = packet->src_id;
1422   new->src_id_len = packet->src_id_len;
1423   new->src_id_type = packet->src_id_type;
1424   new->dst_id = packet->dst_id;
1425   new->dst_id_len = packet->dst_id_len;
1426   new->dst_id_type = packet->dst_id_type;
1427
1428   buffer = silc_buffer_alloc(512);
1429   new->buffer = buffer;
1430
1431   while (packet->buffer->len) {
1432     SILC_GET16_MSB(len1, packet->buffer->data);
1433     if ((len1 > packet->buffer->len) ||
1434         (len1 > buffer->truelen))
1435       break;
1436
1437     SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
1438     if ((len2 > packet->buffer->len) ||
1439         (len2 > buffer->truelen))
1440       break;
1441
1442     silc_buffer_pull_tail(buffer, 4 + len1 + len2);
1443     silc_buffer_put(buffer, packet->buffer->data, 4 + len1 + len2);
1444
1445     /* Process the New Channel */
1446     silc_server_new_channel(server, sock, new);
1447
1448     silc_buffer_push_tail(buffer, 4 + len1 + len2);
1449     silc_buffer_pull(packet->buffer, 4 + len1 + len2);
1450   }
1451
1452   silc_buffer_free(buffer);
1453   silc_free(new);
1454 }