updates.
[silc.git] / apps / silcd / packet_send.c
1 /*
2
3   packet_send.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 send packets. 
22  */
23 /* $Id$ */
24
25 #include "serverincludes.h"
26 #include "server_internal.h"
27
28 /* Internal context that holds the packet data and packet sending function
29    callbacks when the packet is sent with timeout.  This is used when
30    the server is performing re-key protocol.  During re-key we will prevent
31    sending of any other than re-key packets so that the packets would not
32    be encrypted with wrong keys.  Other than that, this is not used at all. */
33 typedef struct {
34   
35 } *SilcServerSendPacket;
36
37 /* Routine that sends packet or marks packet to be sent. This is used
38    directly only in special cases. Normal cases should use
39    silc_server_packet_send. Returns < 0 error. */
40
41 int silc_server_packet_send_real(SilcServer server,
42                                  SilcSocketConnection sock,
43                                  int force_send)
44 {
45   int ret;
46
47   /* Send the packet */
48   ret = silc_packet_send(sock, force_send);
49   if (ret != -2)
50     return ret;
51
52   /* Mark that there is some outgoing data available for this connection. 
53      This call sets the connection both for input and output (the input
54      is set always and this call keeps the input setting, actually). 
55      Actual data sending is performed by silc_server_packet_process. */
56   SILC_SET_CONNECTION_FOR_OUTPUT(sock->sock);
57
58   /* Mark to socket that data is pending in outgoing buffer. This flag
59      is needed if new data is added to the buffer before the earlier
60      put data is sent to the network. */
61   SILC_SET_OUTBUF_PENDING(sock);
62
63   return 0;
64 }
65
66 /* Assembles a new packet to be sent out to network. This doesn't actually
67    send the packet but creates the packet and fills the outgoing data
68    buffer and marks the packet ready to be sent to network. However, If 
69    argument force_send is TRUE the packet is sent immediately and not put 
70    to queue. Normal case is that the packet is not sent immediately. */
71
72 void silc_server_packet_send(SilcServer server,
73                              SilcSocketConnection sock, 
74                              SilcPacketType type, 
75                              SilcPacketFlags flags,
76                              unsigned char *data, 
77                              uint32 data_len,
78                              int force_send)
79 {
80   void *dst_id = NULL;
81   SilcIdType dst_id_type = SILC_ID_NONE;
82
83   if (!sock)
84     return;
85
86   /* Get data used in the packet sending, keys and stuff */
87   switch(sock->type) {
88   case SILC_SOCKET_TYPE_CLIENT:
89     dst_id = ((SilcClientEntry)sock->user_data)->id;
90     dst_id_type = SILC_ID_CLIENT;
91     break;
92   case SILC_SOCKET_TYPE_SERVER:
93   case SILC_SOCKET_TYPE_ROUTER:
94     dst_id = ((SilcServerEntry)sock->user_data)->id;
95     dst_id_type = SILC_ID_SERVER;
96     break;
97   default:
98     break;
99   }
100
101   silc_server_packet_send_dest(server, sock, type, flags, dst_id,
102                                dst_id_type, data, data_len, force_send);
103 }
104
105 /* Assembles a new packet to be sent out to network. This doesn't actually
106    send the packet but creates the packet and fills the outgoing data
107    buffer and marks the packet ready to be sent to network. However, If 
108    argument force_send is TRUE the packet is sent immediately and not put 
109    to queue. Normal case is that the packet is not sent immediately. 
110    Destination information is sent as argument for this function. */
111
112 void silc_server_packet_send_dest(SilcServer server,
113                                   SilcSocketConnection sock, 
114                                   SilcPacketType type, 
115                                   SilcPacketFlags flags,
116                                   void *dst_id,
117                                   SilcIdType dst_id_type,
118                                   unsigned char *data, 
119                                   uint32 data_len,
120                                   int force_send)
121 {
122   SilcPacketContext packetdata;
123   SilcIDListData idata;
124   SilcCipher cipher = NULL;
125   SilcHmac hmac = NULL;
126   unsigned char *dst_id_data = NULL;
127   uint32 dst_id_len = 0;
128
129   SILC_LOG_DEBUG(("Sending packet, type %d", type));
130
131   /* Get data used in the packet sending, keys and stuff */
132   idata = (SilcIDListData)sock->user_data;
133
134   if (dst_id) {
135     dst_id_data = silc_id_id2str(dst_id, dst_id_type);
136     dst_id_len = silc_id_get_len(dst_id_type);
137   }
138
139   /* Set the packet context pointers */
140   packetdata.type = type;
141   packetdata.flags = flags;
142   packetdata.src_id = silc_id_id2str(server->id, server->id_type);
143   packetdata.src_id_len = SILC_ID_SERVER_LEN;
144   packetdata.src_id_type = server->id_type;
145   packetdata.dst_id = dst_id_data;
146   packetdata.dst_id_len = dst_id_len;
147   packetdata.dst_id_type = dst_id_type;
148   packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
149     packetdata.src_id_len + dst_id_len;
150   packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
151
152   /* Prepare outgoing data buffer for packet sending */
153   silc_packet_send_prepare(sock, 
154                            SILC_PACKET_HEADER_LEN +
155                            packetdata.src_id_len + 
156                            packetdata.dst_id_len,
157                            packetdata.padlen,
158                            data_len);
159
160   SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
161
162   packetdata.buffer = sock->outbuf;
163
164   /* Put the data to the buffer */
165   if (data && data_len)
166     silc_buffer_put(sock->outbuf, data, data_len);
167
168   /* Create the outgoing packet */
169   silc_packet_assemble(&packetdata);
170
171   if (idata) {
172     cipher = idata->send_key;
173     hmac = idata->hmac;
174   }
175
176   /* Encrypt the packet */
177   silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
178
179   SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
180                    sock->outbuf->data, sock->outbuf->len);
181
182   /* Now actually send the packet */
183   silc_server_packet_send_real(server, sock, force_send);
184
185   if (packetdata.src_id)
186     silc_free(packetdata.src_id);
187   if (packetdata.dst_id)
188     silc_free(packetdata.dst_id);
189 }
190
191 /* Assembles a new packet to be sent out to network. This doesn't actually
192    send the packet but creates the packet and fills the outgoing data
193    buffer and marks the packet ready to be sent to network. However, If 
194    argument force_send is TRUE the packet is sent immediately and not put 
195    to queue. Normal case is that the packet is not sent immediately. 
196    The source and destination information is sent as argument for this
197    function. */
198
199 void silc_server_packet_send_srcdest(SilcServer server,
200                                      SilcSocketConnection sock, 
201                                      SilcPacketType type, 
202                                      SilcPacketFlags flags,
203                                      void *src_id,
204                                      SilcIdType src_id_type,
205                                      void *dst_id,
206                                      SilcIdType dst_id_type,
207                                      unsigned char *data, 
208                                      uint32 data_len,
209                                      int force_send)
210 {
211   SilcPacketContext packetdata;
212   SilcIDListData idata;
213   SilcCipher cipher = NULL;
214   SilcHmac hmac = NULL;
215   unsigned char *dst_id_data = NULL;
216   uint32 dst_id_len = 0;
217   unsigned char *src_id_data = NULL;
218   uint32 src_id_len = 0;
219
220   SILC_LOG_DEBUG(("Sending packet, type %d", type));
221
222   /* Get data used in the packet sending, keys and stuff */
223   idata = (SilcIDListData)sock->user_data;
224
225   if (dst_id) {
226     dst_id_data = silc_id_id2str(dst_id, dst_id_type);
227     dst_id_len = silc_id_get_len(dst_id_type);
228   }
229
230   if (src_id) {
231     src_id_data = silc_id_id2str(src_id, src_id_type);
232     src_id_len = silc_id_get_len(src_id_type);
233   }
234
235   /* Set the packet context pointers */
236   packetdata.type = type;
237   packetdata.flags = flags;
238   packetdata.src_id = src_id_data;
239   packetdata.src_id_len = src_id_len;
240   packetdata.src_id_type = src_id_type;
241   packetdata.dst_id = dst_id_data;
242   packetdata.dst_id_len = dst_id_len;
243   packetdata.dst_id_type = dst_id_type;
244   packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
245     packetdata.src_id_len + dst_id_len;
246   packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
247
248   /* Prepare outgoing data buffer for packet sending */
249   silc_packet_send_prepare(sock, 
250                            SILC_PACKET_HEADER_LEN +
251                            packetdata.src_id_len + 
252                            packetdata.dst_id_len,
253                            packetdata.padlen,
254                            data_len);
255
256   SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
257
258   packetdata.buffer = sock->outbuf;
259
260   /* Put the data to the buffer */
261   if (data && data_len)
262     silc_buffer_put(sock->outbuf, data, data_len);
263
264   /* Create the outgoing packet */
265   silc_packet_assemble(&packetdata);
266
267   if (idata) {
268     cipher = idata->send_key;
269     hmac = idata->hmac;
270   }
271
272   /* Encrypt the packet */
273   silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
274
275   SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
276                    sock->outbuf->data, sock->outbuf->len);
277
278   /* Now actually send the packet */
279   silc_server_packet_send_real(server, sock, force_send);
280
281   if (packetdata.src_id)
282     silc_free(packetdata.src_id);
283   if (packetdata.dst_id)
284     silc_free(packetdata.dst_id);
285 }
286
287 /* Broadcast received packet to our primary route. This function is used
288    by router to further route received broadcast packet. It is expected
289    that the broadcast flag from the packet is checked before calling this
290    function. This does not test or set the broadcast flag. */
291
292 void silc_server_packet_broadcast(SilcServer server,
293                                   SilcSocketConnection sock,
294                                   SilcPacketContext *packet)
295 {
296   SilcBuffer buffer = packet->buffer;
297   SilcIDListData idata;
298   void *id;
299
300   SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
301
302   /* If the packet is originated from our primary route we are
303      not allowed to send the packet. */
304   id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
305   if (id && SILC_ID_SERVER_COMPARE(id, server->router->id)) {
306     idata = (SilcIDListData)sock->user_data;
307
308     silc_buffer_push(buffer, buffer->data - buffer->head);
309     silc_packet_send_prepare(sock, 0, 0, buffer->len); 
310     silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
311     silc_packet_encrypt(idata->send_key, idata->hmac, 
312                         sock->outbuf, sock->outbuf->len);
313
314     SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", sock->outbuf->len),
315                      sock->outbuf->data, sock->outbuf->len);
316
317     /* Now actually send the packet */
318     silc_server_packet_send_real(server, sock, TRUE);
319     silc_free(id);
320     return;
321   }
322
323   SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
324                   "original sender of this packet"));
325   silc_free(id);
326 }
327
328 /* Routes received packet to `sock'. This is used to route the packets that
329    router receives but are not destined to it. */
330
331 void silc_server_packet_route(SilcServer server,
332                               SilcSocketConnection sock,
333                               SilcPacketContext *packet)
334 {
335   SilcBuffer buffer = packet->buffer;
336   SilcIDListData idata;
337
338   SILC_LOG_DEBUG(("Routing received packet"));
339
340   idata = (SilcIDListData)sock->user_data;
341
342   silc_buffer_push(buffer, buffer->data - buffer->head);
343   silc_packet_send_prepare(sock, 0, 0, buffer->len); 
344   silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
345   silc_packet_encrypt(idata->send_key, idata->hmac, 
346                       sock->outbuf, sock->outbuf->len);
347
348   SILC_LOG_HEXDUMP(("Routed packet, len %d", sock->outbuf->len),
349                    sock->outbuf->data, sock->outbuf->len);
350
351   /* Now actually send the packet */
352   silc_server_packet_send_real(server, sock, TRUE);
353 }
354
355 /* Internal routine to actually create the channel packet and send it
356    to network. This is common function in channel message sending. If
357    `channel_message' is TRUE this encrypts the message as it is strictly
358    a channel message. If FALSE normal encryption process is used. */
359
360 static void
361 silc_server_packet_send_to_channel_real(SilcServer server,
362                                         SilcSocketConnection sock,
363                                         SilcPacketContext *packet,
364                                         SilcCipher cipher,
365                                         SilcHmac hmac,
366                                         unsigned char *data,
367                                         uint32 data_len,
368                                         int channel_message,
369                                         int force_send)
370 {
371   packet->truelen = data_len + SILC_PACKET_HEADER_LEN + 
372     packet->src_id_len + packet->dst_id_len;
373
374   /* Prepare outgoing data buffer for packet sending */
375   silc_packet_send_prepare(sock, 
376                            SILC_PACKET_HEADER_LEN +
377                            packet->src_id_len + 
378                            packet->dst_id_len,
379                            packet->padlen,
380                            data_len);
381
382   packet->buffer = sock->outbuf;
383
384   /* Put the data to buffer, assemble and encrypt the packet. The packet
385      is encrypted with normal session key shared with the client. */
386   silc_buffer_put(sock->outbuf, data, data_len);
387   silc_packet_assemble(packet);
388   if (channel_message)
389     silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN + 
390                         packet->src_id_len + packet->dst_id_len +
391                         packet->padlen);
392   else
393     silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
394     
395   SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
396                    sock->outbuf->data, sock->outbuf->len);
397
398   /* Now actually send the packet */
399   silc_server_packet_send_real(server, sock, force_send);
400 }
401
402 /* This routine is used by the server to send packets to channel. The 
403    packet sent with this function is distributed to all clients on
404    the channel. Usually this is used to send notify messages to the
405    channel, things like notify about new user joining to the channel. 
406    If `route' is FALSE then the packet is sent only locally and will not
407    be routed anywhere (for router locally means cell wide). If `sender'
408    is provided then the packet is not sent to that connection since it
409    originally came from it. */
410
411 void silc_server_packet_send_to_channel(SilcServer server,
412                                         SilcSocketConnection sender,
413                                         SilcChannelEntry channel,
414                                         SilcPacketType type,
415                                         unsigned char route,
416                                         unsigned char *data,
417                                         uint32 data_len,
418                                         int force_send)
419 {
420   SilcSocketConnection sock = NULL;
421   SilcPacketContext packetdata;
422   SilcClientEntry client = NULL;
423   SilcServerEntry *routed = NULL;
424   SilcChannelClientEntry chl;
425   SilcIDListData idata;
426   uint32 routed_count = 0;
427
428   /* This doesn't send channel message packets */
429   if (type == SILC_PACKET_CHANNEL_MESSAGE)
430     return;
431   
432   SILC_LOG_DEBUG(("Sending packet to channel"));
433
434   /* Set the packet context pointers. */
435   packetdata.flags = 0;
436   packetdata.type = type;
437   packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
438   packetdata.src_id_len = SILC_ID_SERVER_LEN;
439   packetdata.src_id_type = SILC_ID_SERVER;
440   packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
441   packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
442   packetdata.dst_id_type = SILC_ID_CHANNEL;
443   packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
444     packetdata.src_id_len + packetdata.dst_id_len;
445   packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
446
447   /* If there are global users in the channel we will send the message
448      first to our router for further routing. */
449   if (route && server->server_type == SILC_SERVER && !server->standalone &&
450       channel->global_users) {
451     SilcServerEntry router;
452
453     /* Get data used in packet header encryption, keys and stuff. */
454     router = server->router;
455     sock = (SilcSocketConnection)router->connection;
456     idata = (SilcIDListData)router;
457     
458     if (sock != sender) {
459       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
460       
461       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
462                                               idata->send_key, idata->hmac, 
463                                               data, data_len, FALSE, 
464                                               force_send);
465     }
466   }
467
468   /* Send the message to clients on the channel's client list. */
469   silc_list_start(channel->user_list);
470   while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
471     client = chl->client;
472
473     /* If client has router set it is not locally connected client and
474        we will route the message to the router set in the client. Though,
475        send locally connected server in all cases. */
476     if (server->server_type == SILC_ROUTER && client && client->router && 
477         ((!route && client->router->router == server->id_entry) || route)) {
478       int k;
479
480       /* Check if we have sent the packet to this route already */
481       for (k = 0; k < routed_count; k++)
482         if (routed[k] == client->router)
483           break;
484       if (k < routed_count)
485         continue;
486
487       /* Get data used in packet header encryption, keys and stuff. */
488       sock = (SilcSocketConnection)client->router->connection;
489       idata = (SilcIDListData)client->router;
490
491       if (sender && sock == sender)
492         continue;
493
494       /* Send the packet */
495       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
496                                               idata->send_key, idata->hmac, 
497                                               data, data_len, FALSE, 
498                                               force_send);
499
500       /* We want to make sure that the packet is routed to same router
501          only once. Mark this route as sent route. */
502       k = routed_count;
503       routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
504       routed[k] = client->router;
505       routed_count++;
506
507       continue;
508     }
509
510     if (client && client->router)
511       continue;
512
513     /* Send to locally connected client */
514     if (client) {
515
516       /* Get data used in packet header encryption, keys and stuff. */
517       sock = (SilcSocketConnection)client->connection;
518       idata = (SilcIDListData)client;
519
520       if (sender && sock == sender)
521         continue;
522
523       /* Send the packet */
524       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
525                                               idata->send_key, idata->hmac, 
526                                               data, data_len, FALSE, 
527                                               force_send);
528     }
529   }
530
531   if (routed_count)
532     silc_free(routed);
533   silc_free(packetdata.src_id);
534   silc_free(packetdata.dst_id);
535 }
536
537 /* This routine is explicitly used to relay messages to some channel.
538    Packets sent with this function we have received earlier and are
539    totally encrypted. This just sends the packet to all clients on
540    the channel. If the sender of the packet is someone on the channel 
541    the message will not be sent to that client. The SILC Packet header
542    is encrypted with the session key shared between us and the client.
543    MAC is also computed before encrypting the header. Rest of the
544    packet will be untouched. */
545
546 void silc_server_packet_relay_to_channel(SilcServer server,
547                                          SilcSocketConnection sender_sock,
548                                          SilcChannelEntry channel,
549                                          void *sender, 
550                                          SilcIdType sender_type,
551                                          unsigned char *data,
552                                          uint32 data_len,
553                                          int force_send)
554 {
555   int found = FALSE;
556   SilcSocketConnection sock = NULL;
557   SilcPacketContext packetdata;
558   SilcClientEntry client = NULL;
559   SilcServerEntry *routed = NULL;
560   SilcChannelClientEntry chl;
561   uint32 routed_count = 0;
562   SilcIDListData idata;
563
564   SILC_LOG_DEBUG(("Relaying packet to channel"));
565
566   /* Set the packet context pointers. */
567   packetdata.flags = 0;
568   packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
569   packetdata.src_id = silc_id_id2str(sender, sender_type);
570   packetdata.src_id_len = silc_id_get_len(sender_type);
571   packetdata.src_id_type = sender_type;
572   packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
573   packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
574   packetdata.dst_id_type = SILC_ID_CHANNEL;
575   packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
576                                           packetdata.src_id_len +
577                                           packetdata.dst_id_len));
578
579   /* If there are global users in the channel we will send the message
580      first to our router for further routing. */
581   if (server->server_type == SILC_SERVER && !server->standalone &&
582       channel->global_users) {
583     SilcServerEntry router;
584
585     router = server->router;
586
587     /* Check that the sender is not our router. */
588     if (sender_sock != (SilcSocketConnection)router->connection) {
589
590       /* Get data used in packet header encryption, keys and stuff. */
591       sock = (SilcSocketConnection)router->connection;
592       idata = (SilcIDListData)router;
593
594       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
595
596       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
597                                               idata->send_key, idata->hmac, 
598                                               data, data_len, TRUE, 
599                                               force_send);
600     }
601   }
602
603   /* Send the message to clients on the channel's client list. */
604   silc_list_start(channel->user_list);
605   while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
606     client = chl->client;
607
608     if (client) {
609
610       /* If sender is one on the channel do not send it the packet. */
611       if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
612         found = TRUE;
613         continue;
614       }
615
616       /* If the client has set router it means that it is not locally
617          connected client and we will route the packet further. */
618       if (server->server_type == SILC_ROUTER && client->router) {
619         int k;
620
621         /* Sender maybe server as well so we want to make sure that
622            we won't send the message to the server it came from. */
623         if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
624           found = TRUE;
625           continue;
626         }
627
628         /* Check if we have sent the packet to this route already */
629         for (k = 0; k < routed_count; k++)
630           if (routed[k] == client->router)
631             break;
632         if (k < routed_count)
633           continue;
634         
635         /* Get data used in packet header encryption, keys and stuff. */
636         sock = (SilcSocketConnection)client->router->connection;
637         idata = (SilcIDListData)client->router;
638
639         if (sender_sock && sock == sender_sock)
640           continue;
641
642         SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)", 
643                         silc_id_render(client->id, SILC_ID_CLIENT),
644                         sock->hostname, sock->ip));
645
646         /* We want to make sure that the packet is routed to same router
647            only once. Mark this route as sent route. */
648         k = routed_count;
649         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
650         routed[k] = client->router;
651         routed_count++;
652         
653         /* If the remote connection is router then we'll decrypt the
654            channel message and re-encrypt it with the session key shared
655            between us and the remote router. This is done because the
656            channel keys are cell specific and we have different channel
657            key than the remote router has. */
658         if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
659
660           /* If private key mode is not set then decrypt the packet
661              and re-encrypt it */
662           if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
663             unsigned char *tmp = silc_calloc(data_len, sizeof(*data));
664             memcpy(tmp, data, data_len);
665
666             /* Decrypt the channel message (we don't check the MAC) */
667             if (!silc_channel_message_payload_decrypt(tmp, data_len, 
668                                                       channel->channel_key,
669                                                       NULL)) {
670               memset(tmp, 0, data_len);
671               silc_free(tmp);
672               continue;
673             }
674
675             /* Now re-encrypt and send it to the router */
676             silc_server_packet_send_srcdest(server, sock, 
677                                             SILC_PACKET_CHANNEL_MESSAGE, 0,
678                                             sender, sender_type,
679                                             channel->id, SILC_ID_CHANNEL,
680                                             tmp, data_len, force_send);
681             
682             /* Free the copy of the channel message */
683             memset(tmp, 0, data_len);
684             silc_free(tmp);
685           } else {
686             /* Private key mode is set, we don't have the channel key, so
687                just re-encrypt the entire packet and send it to the router. */
688             silc_server_packet_send_srcdest(server, sock, 
689                                             SILC_PACKET_CHANNEL_MESSAGE, 0,
690                                             sender, sender_type,
691                                             channel->id, SILC_ID_CHANNEL,
692                                             data, data_len, force_send);
693           }
694           continue;
695         }
696   
697         /* Send the packet (to normal server) */
698         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
699                                                 idata->send_key, idata->hmac, 
700                                                 data, data_len, TRUE, 
701                                                 force_send);
702
703         continue;
704       }
705
706       if (client && client->router)
707         continue;
708
709       /* Get data used in packet header encryption, keys and stuff. */
710       sock = (SilcSocketConnection)client->connection;
711       idata = (SilcIDListData)client;
712
713       if (sender_sock && sock == sender_sock)
714         continue;
715
716       SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)", 
717                       silc_id_render(client->id, SILC_ID_CLIENT),
718                       sock->hostname, sock->ip));
719
720       /* Send the packet */
721       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
722                                               idata->send_key, idata->hmac, 
723                                               data, data_len, TRUE, 
724                                               force_send);
725     }
726   }
727
728   silc_free(packetdata.src_id);
729   silc_free(packetdata.dst_id);
730 }
731
732 /* This function is used to send packets strictly to all local clients
733    on a particular channel.  This is used for example to distribute new
734    channel key to all our locally connected clients on the channel. 
735    The packets are always encrypted with the session key shared between
736    the client, this means these are not _to the channel_ but _to the client_
737    on the channel. */
738
739 void silc_server_packet_send_local_channel(SilcServer server,
740                                            SilcChannelEntry channel,
741                                            SilcPacketType type,
742                                            SilcPacketFlags flags,
743                                            unsigned char *data,
744                                            uint32 data_len,
745                                            int force_send)
746 {
747   SilcChannelClientEntry chl;
748   SilcSocketConnection sock = NULL;
749
750   SILC_LOG_DEBUG(("Start"));
751
752   /* Send the message to clients on the channel's client list. */
753   silc_list_start(channel->user_list);
754   while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
755     if (chl->client && !chl->client->router) {
756       sock = (SilcSocketConnection)chl->client->connection;
757
758       /* Send the packet to the client */
759       silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
760                                    SILC_ID_CLIENT, data, data_len,
761                                    force_send);
762     }
763   }
764 }
765
766 /* Routine used to send (relay, route) private messages to some destination.
767    If the private message key does not exist then the message is re-encrypted,
768    otherwise we just pass it along. This really is not used to send new
769    private messages (as server does not send them) but to relay received
770    private messages. */
771
772 void silc_server_send_private_message(SilcServer server,
773                                       SilcSocketConnection dst_sock,
774                                       SilcCipher cipher,
775                                       SilcHmac hmac,
776                                       SilcPacketContext *packet)
777 {
778   SilcBuffer buffer = packet->buffer;
779
780   /* Re-encrypt and send if private messge key does not exist */
781   if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
782
783     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
784                      + packet->dst_id_len + packet->padlen);
785     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
786     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
787     
788     /* Re-encrypt packet */
789     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
790     
791     /* Send the packet */
792     silc_server_packet_send_real(server, dst_sock, FALSE);
793
794   } else {
795     /* Key exist so encrypt just header and send it */
796     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
797                      + packet->dst_id_len + packet->padlen);
798     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
799     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
800
801     /* Encrypt header */
802     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, 
803                         SILC_PACKET_HEADER_LEN + packet->src_id_len + 
804                         packet->dst_id_len + packet->padlen);
805
806     silc_server_packet_send_real(server, dst_sock, FALSE);
807   }
808 }
809
810 /* Sends current motd to client */
811
812 void silc_server_send_motd(SilcServer server,
813                            SilcSocketConnection sock)
814 {
815   char *motd;
816   uint32 motd_len;
817
818   if (server->config && server->config->motd && 
819       server->config->motd->motd_file) {
820
821     motd = silc_file_read(server->config->motd->motd_file, &motd_len);
822     if (!motd)
823       return;
824
825     silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
826                             motd, motd_len);
827     silc_free(motd);
828   }
829 }
830
831 /* Sends error message. Error messages may or may not have any 
832    implications. */
833
834 void silc_server_send_error(SilcServer server,
835                             SilcSocketConnection sock,
836                             const char *fmt, ...)
837 {
838   va_list ap;
839   unsigned char buf[4096];
840
841   memset(buf, 0, sizeof(buf));
842   va_start(ap, fmt);
843   vsprintf(buf, fmt, ap);
844   va_end(ap);
845
846   silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0, 
847                           buf, strlen(buf), FALSE);
848 }
849
850 /* Sends notify message. If format is TRUE the variable arguments are
851    formatted and the formatted string is sent as argument payload. If it is
852    FALSE then each argument is sent as separate argument and their format
853    in the argument list must be { argument data, argument length }. */
854
855 void silc_server_send_notify(SilcServer server,
856                              SilcSocketConnection sock,
857                              int broadcast,
858                              SilcNotifyType type,
859                              uint32 argc, ...)
860 {
861   va_list ap;
862   SilcBuffer packet;
863
864   va_start(ap, argc);
865
866   packet = silc_notify_payload_encode(type, argc, ap);
867   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0, 
868                           packet->data, packet->len, FALSE);
869   silc_buffer_free(packet);
870 }
871
872 /* Sends notify message and gets the arguments from the `args' Argument
873    Payloads. */
874
875 void silc_server_send_notify_args(SilcServer server,
876                                   SilcSocketConnection sock,
877                                   int broadcast,
878                                   SilcNotifyType type,
879                                   uint32 argc,
880                                   SilcBuffer args)
881 {
882   SilcBuffer packet;
883
884   packet = silc_notify_payload_encode_args(type, argc, args);
885   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0, 
886                           packet->data, packet->len, FALSE);
887   silc_buffer_free(packet);
888 }
889
890 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
891    `old_id' with the `new_id'. */
892
893 void silc_server_send_notify_channel_change(SilcServer server,
894                                             SilcSocketConnection sock,
895                                             int broadcast,
896                                             SilcChannelID *old_id,
897                                             SilcChannelID *new_id,
898                                             uint32 id_len)
899 {
900   SilcBuffer idp1, idp2;
901
902   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
903   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
904
905   silc_server_send_notify(server, sock, broadcast,
906                           SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
907                           2, idp1->data, idp1->len, idp2->data, idp2->len);
908   silc_buffer_free(idp1);
909   silc_buffer_free(idp2);
910 }
911
912 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
913    `old_id' with the `new_id'. */
914
915 void silc_server_send_notify_nick_change(SilcServer server,
916                                          SilcSocketConnection sock,
917                                          int broadcast,
918                                          SilcClientID *old_id,
919                                          SilcClientID *new_id,
920                                          uint32 id_len)
921 {
922   SilcBuffer idp1, idp2;
923
924   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
925   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
926
927   silc_server_send_notify(server, sock, broadcast, 
928                           SILC_NOTIFY_TYPE_NICK_CHANGE,
929                           2, idp1->data, idp1->len, idp2->data, idp2->len);
930   silc_buffer_free(idp1);
931   silc_buffer_free(idp2);
932 }
933
934 /* Sends JOIN notify type. This tells that new client by `client_id' ID
935    has joined to the `channel'. */
936
937 void silc_server_send_notify_join(SilcServer server,
938                                   SilcSocketConnection sock,
939                                   int broadcast,
940                                   SilcChannelEntry channel,
941                                   SilcClientID *client_id,
942                                   uint32 client_id_len)
943 {
944   SilcBuffer idp1, idp2;
945
946   idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
947   idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
948   silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
949                           2, idp1->data, idp1->len,
950                           idp2->data, idp2->len);
951   silc_buffer_free(idp1);
952   silc_buffer_free(idp2);
953 }
954
955 /* Sends LEAVE notify type. This tells that `client_id' has left the
956    `channel'. The Notify packet is always destined to the channel. */
957
958 void silc_server_send_notify_leave(SilcServer server,
959                                    SilcSocketConnection sock,
960                                    int broadcast,
961                                    SilcChannelEntry channel,
962                                    SilcClientID *client_id,
963                                    uint32 client_id_len)
964 {
965   SilcBuffer idp;
966
967   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
968   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
969                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
970                                1, idp->data, idp->len);
971   silc_buffer_free(idp);
972 }
973
974 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
975    `channel' mode to `mode. The Notify packet is always destined to
976    the channel. */
977
978 void silc_server_send_notify_cmode(SilcServer server,
979                                    SilcSocketConnection sock,
980                                    int broadcast,
981                                    SilcChannelEntry channel,
982                                    uint32 mode_mask,
983                                    void *id, SilcIdType id_type,
984                                    uint32 id_len,
985                                    char *cipher, char *hmac)
986 {
987   SilcBuffer idp;
988   unsigned char mode[4];
989
990   idp = silc_id_payload_encode((void *)id, id_type);
991   SILC_PUT32_MSB(mode_mask, mode);
992
993   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
994                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
995                                4, idp->data, idp->len,
996                                mode, 4,
997                                cipher, cipher ? strlen(cipher) : 0,
998                                hmac, hmac ? strlen(hmac) : 0);
999   silc_buffer_free(idp);
1000 }
1001
1002 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
1003    `target' client's mode on `channel'. The Notify packet is always
1004    destined to the channel. */
1005
1006 void silc_server_send_notify_cumode(SilcServer server,
1007                                     SilcSocketConnection sock,
1008                                     int broadcast,
1009                                     SilcChannelEntry channel,
1010                                     uint32 mode_mask,
1011                                     SilcClientID *client_id,
1012                                     uint32 client_id_len,
1013                                     SilcClientID *target,
1014                                     uint32 target_len)
1015 {
1016   SilcBuffer idp1, idp2;
1017   unsigned char mode[4];
1018
1019   idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1020   idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1021   SILC_PUT32_MSB(mode_mask, mode);
1022
1023   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1024                                SILC_ID_CHANNEL, 
1025                                SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3, 
1026                                idp1->data, idp1->len,
1027                                mode, 4,
1028                                idp2->data, idp2->len);
1029   silc_buffer_free(idp1);
1030   silc_buffer_free(idp2);
1031 }
1032
1033 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1034    left SILC network. This function is used only between server and router
1035    traffic. This is not used to send the notify to the channel for
1036    client. The `message may be NULL. */
1037
1038 void silc_server_send_notify_signoff(SilcServer server,
1039                                      SilcSocketConnection sock,
1040                                      int broadcast,
1041                                      SilcClientID *client_id,
1042                                      uint32 client_id_len,
1043                                      char *message)
1044 {
1045   SilcBuffer idp;
1046
1047   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1048   silc_server_send_notify(server, sock, broadcast,
1049                           SILC_NOTIFY_TYPE_SIGNOFF,
1050                           message ? 2 : 1, idp->data, idp->len,
1051                           message, message ? strlen(message): 0);
1052   silc_buffer_free(idp);
1053 }
1054
1055 /* Sends TOPIC_SET notify type. This tells that `client_id' changed
1056    the `channel's topic to `topic'. The Notify packet is always destined
1057    to the channel. This function is used to send the topic set notifies
1058    between routers. */
1059
1060 void silc_server_send_notify_topic_set(SilcServer server,
1061                                        SilcSocketConnection sock,
1062                                        int broadcast,
1063                                        SilcChannelEntry channel,
1064                                        SilcClientID *client_id,
1065                                        uint32 client_id_len,
1066                                        char *topic)
1067 {
1068   SilcBuffer idp;
1069
1070   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1071   silc_server_send_notify(server, sock, broadcast,
1072                           SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
1073                           topic ? 2 : 1, 
1074                           idp->data, idp->len, 
1075                           topic, topic ? strlen(topic) : 0);
1076   silc_buffer_free(idp);
1077 }
1078
1079 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1080    was kicked off the channel.  The `comment' may indicate the reason
1081    for the kicking. This function is used only between server and router
1082    traffic. */
1083
1084 void silc_server_send_notify_kicked(SilcServer server,
1085                                     SilcSocketConnection sock,
1086                                     int broadcast,
1087                                     SilcChannelEntry channel,
1088                                     SilcClientID *client_id,
1089                                     uint32 client_id_len,
1090                                     char *comment)
1091 {
1092   SilcBuffer idp;
1093
1094   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1095   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1096                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED,
1097                                comment ? 2 : 1, idp->data, idp->len,
1098                                comment, comment ? strlen(comment) : 0);
1099   silc_buffer_free(idp);
1100 }
1101
1102 /* Send KILLED notify type. This tells that the `client_id' client was
1103    killed from the network.  The `comment' may indicate the reason
1104    for the killing. */
1105
1106 void silc_server_send_notify_killed(SilcServer server,
1107                                     SilcSocketConnection sock,
1108                                     int broadcast,
1109                                     SilcClientID *client_id,
1110                                     uint32 client_id_len,
1111                                     char *comment)
1112 {
1113   SilcBuffer idp;
1114
1115   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1116   silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1117                                SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1118                                comment ? 2 : 1, idp->data, idp->len,
1119                                comment, comment ? strlen(comment) : 0);
1120   silc_buffer_free(idp);
1121 }
1122
1123 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1124    user mode in the SILC Network was changed. This function is used to
1125    send the packet between routers as broadcast packet. */
1126
1127 void silc_server_send_notify_umode(SilcServer server,
1128                                    SilcSocketConnection sock,
1129                                    int broadcast,
1130                                    SilcClientID *client_id,
1131                                    uint32 client_id_len,
1132                                    uint32 mode_mask)
1133 {
1134   SilcBuffer idp;
1135   unsigned char mode[4];
1136
1137   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1138   SILC_PUT32_MSB(mode_mask, mode);
1139
1140   silc_server_send_notify(server, sock, broadcast,
1141                           SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1142                           idp->data, idp->len, 
1143                           mode, 4);
1144   silc_buffer_free(idp);
1145 }
1146
1147 /* Sends BAN notify type. This tells that ban has been either `add'ed
1148    or `del'eted on the `channel. This function is used to send the packet
1149    between routers as broadcast packet. */
1150
1151 void silc_server_send_notify_ban(SilcServer server,
1152                                  SilcSocketConnection sock,
1153                                  int broadcast,
1154                                  SilcChannelEntry channel,
1155                                  char *add, char *del)
1156 {
1157   SilcBuffer idp;
1158
1159   idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1160   silc_server_send_notify(server, sock, broadcast,
1161                           SILC_NOTIFY_TYPE_BAN, 3,
1162                           idp->data, idp->len,
1163                           add, add ? strlen(add) : 0,
1164                           del, del ? strlen(del) : 0);
1165   silc_buffer_free(idp);
1166 }
1167
1168 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1169    or `del'eted on the `channel.  The sender of the invite is the `client_id'.
1170    This function is used to send the packet between routers as broadcast
1171    packet. */
1172
1173 void silc_server_send_notify_invite(SilcServer server,
1174                                     SilcSocketConnection sock,
1175                                     int broadcast,
1176                                     SilcChannelEntry channel,
1177                                     SilcClientID *client_id,
1178                                     uint32 client_id_len,
1179                                     char *add, char *del)
1180 {
1181   SilcBuffer idp, idp2;
1182
1183   idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1184   idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1185   silc_server_send_notify(server, sock, broadcast,
1186                           SILC_NOTIFY_TYPE_INVITE, 5,
1187                           idp->data, idp->len,
1188                           channel->channel_name, strlen(channel->channel_name),
1189                           idp2->data, idp2->len,
1190                           add, add ? strlen(add) : 0,
1191                           del, del ? strlen(del) : 0);
1192   silc_buffer_free(idp);
1193   silc_buffer_free(idp2);
1194 }
1195
1196 /* Sends notify message destined to specific entity. */
1197
1198 void silc_server_send_notify_dest(SilcServer server,
1199                                   SilcSocketConnection sock,
1200                                   int broadcast,
1201                                   void *dest_id,
1202                                   SilcIdType dest_id_type,
1203                                   SilcNotifyType type,
1204                                   uint32 argc, ...)
1205 {
1206   va_list ap;
1207   SilcBuffer packet;
1208
1209   va_start(ap, argc);
1210
1211   packet = silc_notify_payload_encode(type, argc, ap);
1212   silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0, 
1213                                dest_id, dest_id_type,
1214                                packet->data, packet->len, FALSE);
1215   silc_buffer_free(packet);
1216 }
1217
1218 /* Sends notify message to a channel. The notify message sent is 
1219    distributed to all clients on the channel. If `route_notify' is TRUE
1220    then the notify may be routed to primary route or to some other routers.
1221    If FALSE it is assured that the notify is sent only locally. If `sender'
1222    is provided then the packet is not sent to that connection since it
1223    originally came from it. */
1224
1225 void silc_server_send_notify_to_channel(SilcServer server,
1226                                         SilcSocketConnection sender,
1227                                         SilcChannelEntry channel,
1228                                         unsigned char route_notify,
1229                                         SilcNotifyType type,
1230                                         uint32 argc, ...)
1231 {
1232   va_list ap;
1233   SilcBuffer packet;
1234
1235   va_start(ap, argc);
1236
1237   packet = silc_notify_payload_encode(type, argc, ap);
1238   silc_server_packet_send_to_channel(server, sender, channel, 
1239                                      SILC_PACKET_NOTIFY, route_notify,
1240                                      packet->data, packet->len, FALSE);
1241   silc_buffer_free(packet);
1242 }
1243
1244 /* Send notify message to all channels the client has joined. It is quaranteed
1245    that the message is sent only once to a client (ie. if a client is joined
1246    on two same channel it will receive only one notify message). Also, this
1247    sends only to local clients (locally connected if we are server, and to
1248    local servers if we are router). If `sender' is provided the packet is
1249    not sent to that client at all. */
1250
1251 void silc_server_send_notify_on_channels(SilcServer server,
1252                                          SilcClientEntry sender,
1253                                          SilcClientEntry client,
1254                                          SilcNotifyType type,
1255                                          uint32 argc, ...)
1256 {
1257   int k;
1258   SilcSocketConnection sock = NULL;
1259   SilcPacketContext packetdata;
1260   SilcClientEntry c;
1261   SilcClientEntry *sent_clients = NULL;
1262   uint32 sent_clients_count = 0;
1263   SilcServerEntry *routed = NULL;
1264   uint32 routed_count = 0;
1265   SilcChannelEntry channel;
1266   SilcChannelClientEntry chl, chl2;
1267   SilcIDListData idata;
1268   SilcBuffer packet;
1269   unsigned char *data;
1270   uint32 data_len;
1271   int force_send = FALSE;
1272   va_list ap;
1273
1274   SILC_LOG_DEBUG(("Start"));
1275
1276   if (!silc_list_count(client->channels))
1277     return;
1278
1279   va_start(ap, argc);
1280   packet = silc_notify_payload_encode(type, argc, ap);
1281   data = packet->data;
1282   data_len = packet->len;
1283
1284   /* Set the packet context pointers. */
1285   packetdata.flags = 0;
1286   packetdata.type = SILC_PACKET_NOTIFY;
1287   packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1288   packetdata.src_id_len = SILC_ID_SERVER_LEN;
1289   packetdata.src_id_type = SILC_ID_SERVER;
1290
1291   silc_list_start(client->channels);
1292   while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
1293     channel = chl->channel;
1294
1295     /* Send the message to all clients on the channel's client list. */
1296     silc_list_start(channel->user_list);
1297     while ((chl2 = silc_list_get(channel->user_list)) != SILC_LIST_END) {
1298       c = chl2->client;
1299       
1300       if (sender && c == sender)
1301         continue;
1302
1303       /* Check if we have sent the packet to this client already */
1304       for (k = 0; k < sent_clients_count; k++)
1305         if (sent_clients[k] == c)
1306           break;
1307       if (k < sent_clients_count)
1308         continue;
1309
1310       /* If we are router and if this client has router set it is not
1311          locally connected client and we will route the message to the
1312          router set in the client. */
1313       if (c && c->router && server->server_type == SILC_ROUTER) {
1314         /* Check if we have sent the packet to this route already */
1315         for (k = 0; k < routed_count; k++)
1316           if (routed[k] == c->router)
1317             break;
1318         if (k < routed_count)
1319           continue;
1320         
1321         /* Get data used in packet header encryption, keys and stuff. */
1322         sock = (SilcSocketConnection)c->router->connection;
1323         idata = (SilcIDListData)c->router;
1324         
1325         packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1326         packetdata.dst_id_len = SILC_ID_SERVER_LEN;
1327         packetdata.dst_id_type = SILC_ID_SERVER;
1328         packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
1329           packetdata.src_id_len + packetdata.dst_id_len;
1330         packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1331
1332         /* Send the packet */
1333         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1334                                                 idata->send_key, idata->hmac, 
1335                                                 data, data_len, FALSE, 
1336                                                 force_send);
1337         
1338         silc_free(packetdata.dst_id);
1339
1340         /* We want to make sure that the packet is routed to same router
1341            only once. Mark this route as sent route. */
1342         k = routed_count;
1343         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1344         routed[k] = c->router;
1345         routed_count++;
1346
1347         continue;
1348       }
1349
1350       if (c && c->router)
1351         continue;
1352
1353       /* Send to locally connected client */
1354       if (c) {
1355         
1356         /* Get data used in packet header encryption, keys and stuff. */
1357         sock = (SilcSocketConnection)c->connection;
1358         idata = (SilcIDListData)c;
1359         
1360         packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1361         packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
1362         packetdata.dst_id_type = SILC_ID_CLIENT;
1363         packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
1364           packetdata.src_id_len + packetdata.dst_id_len;
1365         packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1366
1367         /* Send the packet */
1368         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1369                                                 idata->send_key, idata->hmac, 
1370                                                 data, data_len, FALSE, 
1371                                                 force_send);
1372
1373         silc_free(packetdata.dst_id);
1374
1375         /* Make sure that we send the notify only once per client. */
1376         sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) * 
1377                                     (sent_clients_count + 1));
1378         sent_clients[sent_clients_count] = c;
1379         sent_clients_count++;
1380       }
1381     }
1382   }
1383
1384   if (routed_count)
1385     silc_free(routed);
1386   if (sent_clients_count)
1387     silc_free(sent_clients);
1388   silc_free(packetdata.src_id);
1389 }
1390
1391 /* Sends New ID Payload to remote end. The packet is used to distribute
1392    information about new registered clients, servers, channel etc. usually
1393    to routers so that they can keep these information up to date. 
1394    If the argument `broadcast' is TRUE then the packet is sent as
1395    broadcast packet. */
1396
1397 void silc_server_send_new_id(SilcServer server,
1398                              SilcSocketConnection sock,
1399                              int broadcast,
1400                              void *id, SilcIdType id_type, 
1401                              uint32 id_len)
1402 {
1403   SilcBuffer idp;
1404
1405   SILC_LOG_DEBUG(("Start"));
1406
1407   idp = silc_id_payload_encode(id, id_type);
1408   silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 
1409                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1410                           idp->data, idp->len, FALSE);
1411   silc_buffer_free(idp);
1412 }
1413
1414 /* Send New Channel Payload to notify about newly created channel in the
1415    SILC network. Normal server nevers sends this packet. Router uses this
1416    to notify other routers in the network about new channel. This packet
1417    is broadcasted. */
1418
1419 void silc_server_send_new_channel(SilcServer server,
1420                                   SilcSocketConnection sock,
1421                                   int broadcast,
1422                                   char *channel_name,
1423                                   void *channel_id, 
1424                                   uint32 channel_id_len,
1425                                   uint32 mode)
1426 {
1427   SilcBuffer packet;
1428   unsigned char *cid;
1429   uint32 name_len = strlen(channel_name);
1430
1431   SILC_LOG_DEBUG(("Start"));
1432
1433   cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1434   if (!cid)
1435     return;
1436
1437   /* Encode the channel payload */
1438   packet = silc_channel_payload_encode(channel_name, name_len,
1439                                        cid, channel_id_len, mode);
1440
1441   silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL, 
1442                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1443                           packet->data, packet->len, FALSE);
1444
1445   silc_free(cid);
1446   silc_buffer_free(packet);
1447 }
1448
1449 /* Send Channel Key payload to distribute the new channel key. Normal server
1450    sends this to router when new client joins to existing channel. Router
1451    sends this to the local server who sent the join command in case where
1452    the channel did not exist yet. Both normal and router servers uses this
1453    also to send this to locally connected clients on the channel. This
1454    must not be broadcasted packet. Routers do not send this to each other. 
1455    If `sender is provided then the packet is not sent to that connection since
1456    it originally came from it. */
1457
1458 void silc_server_send_channel_key(SilcServer server,
1459                                   SilcSocketConnection sender,
1460                                   SilcChannelEntry channel,
1461                                   unsigned char route)
1462 {
1463   SilcBuffer packet;
1464   unsigned char *chid;
1465   uint32 tmp_len;
1466  
1467   SILC_LOG_DEBUG(("Start"));
1468  
1469   chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1470   if (!chid)
1471     return;
1472  
1473   /* Encode channel key packet */
1474   tmp_len = strlen(channel->channel_key->cipher->name);
1475   packet = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, chid, tmp_len,
1476                                            channel->channel_key->cipher->name,
1477                                            channel->key_len / 8, channel->key);
1478  
1479   silc_server_packet_send_to_channel(server, sender, channel, 
1480                                      SILC_PACKET_CHANNEL_KEY,
1481                                      route, packet->data, packet->len, FALSE);
1482   silc_buffer_free(packet);
1483   silc_free(chid);
1484 }
1485
1486 /* Generic function to send any command. The arguments must be sent already
1487    encoded into correct form in correct order. */
1488
1489 void silc_server_send_command(SilcServer server, 
1490                               SilcSocketConnection sock,
1491                               SilcCommand command, 
1492                               uint32 argc, ...)
1493 {
1494   SilcBuffer packet;
1495   va_list ap;
1496
1497   va_start(ap, argc);
1498
1499   packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1500   silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1501                           packet->data, packet->len, TRUE);
1502   silc_buffer_free(packet);
1503 }
1504
1505 /* Send the heartbeat packet. */
1506
1507 void silc_server_send_heartbeat(SilcServer server,
1508                                 SilcSocketConnection sock)
1509 {
1510   silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1511                           NULL, 0, FALSE);
1512 }
1513
1514 /* Generic function to relay packet we've received. This is used to relay
1515    packets to a client but generally can be used to other purposes as well. */
1516
1517 void silc_server_relay_packet(SilcServer server,
1518                               SilcSocketConnection dst_sock,
1519                               SilcCipher cipher,
1520                               SilcHmac hmac,
1521                               SilcPacketContext *packet,
1522                               int force_send)
1523 {
1524   silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1525                    + packet->dst_id_len + packet->padlen);
1526
1527   silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len);
1528   silc_buffer_put(dst_sock->outbuf, packet->buffer->data, packet->buffer->len);
1529   
1530   /* Re-encrypt packet */
1531   silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, packet->buffer->len);
1532   
1533   /* Send the packet */
1534   silc_server_packet_send_real(server, dst_sock, force_send);
1535
1536   silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1537                    + packet->dst_id_len + packet->padlen);
1538 }
1539
1540 /* Routine used to send the connection authentication packet. */
1541
1542 void silc_server_send_connection_auth_request(SilcServer server,
1543                                               SilcSocketConnection sock,
1544                                               uint16 conn_type,
1545                                               SilcAuthMethod auth_meth)
1546 {
1547   SilcBuffer packet;
1548
1549   packet = silc_buffer_alloc(4);
1550   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1551   silc_buffer_format(packet,
1552                      SILC_STR_UI_SHORT(conn_type),
1553                      SILC_STR_UI_SHORT(auth_meth),
1554                      SILC_STR_END);
1555
1556   silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1557                           0, packet->data, packet->len, FALSE);
1558   silc_buffer_free(packet);
1559 }