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