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