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