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