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 checks whether the relayed packet came from router. If it did
537    then we'll need to encrypt it with the channel key. This is called
538    from the silc_server_packet_relay_to_channel. */
539
540 static void
541 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
542                                             SilcSocketConnection sock,
543                                             SilcChannelEntry channel,
544                                             unsigned char *data,
545                                             unsigned int data_len)
546 {
547   /* If we are router and the packet came from router and private key
548      has not been set for the channel then we must encrypt the packet
549      as it was decrypted with the session key shared between us and the
550      router which sent it. This is so, because cells does not share the
551      same channel key. */
552   if (server->server_type == SILC_ROUTER &&
553       sock->type == SILC_SOCKET_TYPE_ROUTER &&
554       !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
555       channel->channel_key) {
556     SilcBuffer chp;
557     uint32 iv_len, i;
558     uint16 data_len, flags;
559
560     iv_len = silc_cipher_get_block_len(channel->channel_key);
561     if (channel->iv[0] == '\0')
562       for (i = 0; i < iv_len; i++) channel->iv[i] = 
563                                      silc_rng_get_byte(server->rng);
564     else
565       silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
566     
567     /* Encode new payload. This encrypts it also. */
568     SILC_GET16_MSB(flags, data);
569     SILC_GET16_MSB(data_len, data + 2);
570     chp = silc_channel_message_payload_encode(flags, data_len, 
571                                               data + 4,
572                                               iv_len, channel->iv,
573                                               channel->channel_key,
574                                               channel->hmac);
575     memcpy(data, chp->data, chp->len);
576     silc_buffer_free(chp);
577   }
578 }
579
580 /* This routine is explicitly used to relay messages to some channel.
581    Packets sent with this function we have received earlier and are
582    totally encrypted. This just sends the packet to all clients on
583    the channel. If the sender of the packet is someone on the channel 
584    the message will not be sent to that client. The SILC Packet header
585    is encrypted with the session key shared between us and the client.
586    MAC is also computed before encrypting the header. Rest of the
587    packet will be untouched. */
588
589 void silc_server_packet_relay_to_channel(SilcServer server,
590                                          SilcSocketConnection sender_sock,
591                                          SilcChannelEntry channel,
592                                          void *sender, 
593                                          SilcIdType sender_type,
594                                          unsigned char *data,
595                                          uint32 data_len,
596                                          int force_send)
597 {
598   bool found = FALSE;
599   SilcSocketConnection sock = NULL;
600   SilcPacketContext packetdata;
601   SilcClientEntry client = NULL;
602   SilcServerEntry *routed = NULL;
603   SilcChannelClientEntry chl;
604   uint32 routed_count = 0;
605   SilcIDListData idata;
606
607   SILC_LOG_DEBUG(("Relaying packet to channel"));
608
609   /* Set the packet context pointers. */
610   packetdata.flags = 0;
611   packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
612   packetdata.src_id = silc_id_id2str(sender, sender_type);
613   packetdata.src_id_len = silc_id_get_len(sender_type);
614   packetdata.src_id_type = sender_type;
615   packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
616   packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
617   packetdata.dst_id_type = SILC_ID_CHANNEL;
618   packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
619                                           packetdata.src_id_len +
620                                           packetdata.dst_id_len));
621
622   /* This encrypts the packet, if needed. It will be encrypted if
623      it came from the router thus it needs to be encrypted with the
624      channel key. If the channel key does not exist, then we know we
625      don't have a single local user on the channel. */
626   silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
627                                               channel, data,
628                                               data_len);
629
630   /* If there are global users in the channel we will send the message
631      first to our router for further routing. */
632   if (server->server_type == SILC_SERVER && !server->standalone &&
633       channel->global_users) {
634     SilcServerEntry router;
635
636     router = server->router;
637
638     /* Check that the sender is not our router. */
639     if (sender_sock != (SilcSocketConnection)router->connection) {
640
641       /* Get data used in packet header encryption, keys and stuff. */
642       sock = (SilcSocketConnection)router->connection;
643       idata = (SilcIDListData)router;
644
645       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
646
647       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
648                                               idata->send_key, 
649                                               idata->hmac_send, 
650                                               data, data_len, TRUE, 
651                                               force_send);
652     }
653   }
654
655   /* Send the message to clients on the channel's client list. */
656   silc_list_start(channel->user_list);
657   while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
658     client = chl->client;
659
660     if (client) {
661
662       /* If sender is one on the channel do not send it the packet. */
663       if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
664         found = TRUE;
665         continue;
666       }
667
668       /* If the client has set router it means that it is not locally
669          connected client and we will route the packet further. */
670       if (server->server_type == SILC_ROUTER && client->router) {
671         int k;
672
673         /* Sender maybe server as well so we want to make sure that
674            we won't send the message to the server it came from. */
675         if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
676           found = TRUE;
677           continue;
678         }
679
680         /* Check if we have sent the packet to this route already */
681         for (k = 0; k < routed_count; k++)
682           if (routed[k] == client->router)
683             break;
684         if (k < routed_count)
685           continue;
686         
687         /* Get data used in packet header encryption, keys and stuff. */
688         sock = (SilcSocketConnection)client->router->connection;
689         idata = (SilcIDListData)client->router;
690
691         if (sender_sock && sock == sender_sock)
692           continue;
693
694         SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)", 
695                         silc_id_render(client->id, SILC_ID_CLIENT),
696                         sock->hostname, sock->ip));
697
698         /* We want to make sure that the packet is routed to same router
699            only once. Mark this route as sent route. */
700         k = routed_count;
701         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
702         routed[k] = client->router;
703         routed_count++;
704         
705         /* If the remote connection is router then we'll decrypt the
706            channel message and re-encrypt it with the session key shared
707            between us and the remote router. This is done because the
708            channel keys are cell specific and we have different channel
709            key than the remote router has. */
710         if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
711
712           /* If private key mode is not set then decrypt the packet
713              and re-encrypt it */
714           if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
715             unsigned char *tmp = silc_calloc(data_len, sizeof(*data));
716             memcpy(tmp, data, data_len);
717
718             /* Decrypt the channel message (we don't check the MAC) */
719             if (channel->channel_key &&
720                 !silc_channel_message_payload_decrypt(tmp, data_len, 
721                                                       channel->channel_key,
722                                                       NULL)) {
723               memset(tmp, 0, data_len);
724               silc_free(tmp);
725               continue;
726             }
727
728             /* Now re-encrypt and send it to the router */
729             silc_server_packet_send_srcdest(server, sock, 
730                                             SILC_PACKET_CHANNEL_MESSAGE, 0,
731                                             sender, sender_type,
732                                             channel->id, SILC_ID_CHANNEL,
733                                             tmp, data_len, force_send);
734             
735             /* Free the copy of the channel message */
736             memset(tmp, 0, data_len);
737             silc_free(tmp);
738           } else {
739             /* Private key mode is set, we don't have the channel key, so
740                just re-encrypt the entire packet and send it to the router. */
741             silc_server_packet_send_srcdest(server, sock, 
742                                             SILC_PACKET_CHANNEL_MESSAGE, 0,
743                                             sender, sender_type,
744                                             channel->id, SILC_ID_CHANNEL,
745                                             data, data_len, force_send);
746           }
747           continue;
748         }
749
750         /* Send the packet (to normal server) */
751         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
752                                                 idata->send_key, 
753                                                 idata->hmac_send, 
754                                                 data, data_len, TRUE, 
755                                                 force_send);
756
757         continue;
758       }
759
760       if (client && client->router)
761         continue;
762
763       /* Get data used in packet header encryption, keys and stuff. */
764       sock = (SilcSocketConnection)client->connection;
765       idata = (SilcIDListData)client;
766
767       if (sender_sock && sock == sender_sock)
768         continue;
769
770       SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)", 
771                       silc_id_render(client->id, SILC_ID_CLIENT),
772                       sock->hostname, sock->ip));
773
774       /* Send the packet */
775       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
776                                               idata->send_key, 
777                                               idata->hmac_send, 
778                                               data, data_len, TRUE, 
779                                               force_send);
780     }
781   }
782
783   silc_free(packetdata.src_id);
784   silc_free(packetdata.dst_id);
785 }
786
787 /* This function is used to send packets strictly to all local clients
788    on a particular channel.  This is used for example to distribute new
789    channel key to all our locally connected clients on the channel. 
790    The packets are always encrypted with the session key shared between
791    the client, this means these are not _to the channel_ but _to the client_
792    on the channel. */
793
794 void silc_server_packet_send_local_channel(SilcServer server,
795                                            SilcChannelEntry channel,
796                                            SilcPacketType type,
797                                            SilcPacketFlags flags,
798                                            unsigned char *data,
799                                            uint32 data_len,
800                                            int force_send)
801 {
802   SilcChannelClientEntry chl;
803   SilcSocketConnection sock = NULL;
804
805   SILC_LOG_DEBUG(("Start"));
806
807   /* Send the message to clients on the channel's client list. */
808   silc_list_start(channel->user_list);
809   while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
810     if (chl->client && !chl->client->router) {
811       sock = (SilcSocketConnection)chl->client->connection;
812
813       /* Send the packet to the client */
814       silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
815                                    SILC_ID_CLIENT, data, data_len,
816                                    force_send);
817     }
818   }
819 }
820
821 /* Routine used to send (relay, route) private messages to some destination.
822    If the private message key does not exist then the message is re-encrypted,
823    otherwise we just pass it along. This really is not used to send new
824    private messages (as server does not send them) but to relay received
825    private messages. */
826
827 void silc_server_send_private_message(SilcServer server,
828                                       SilcSocketConnection dst_sock,
829                                       SilcCipher cipher,
830                                       SilcHmac hmac,
831                                       SilcPacketContext *packet)
832 {
833   SilcBuffer buffer = packet->buffer;
834
835   /* Re-encrypt and send if private messge key does not exist */
836   if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
837
838     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
839                      + packet->dst_id_len + packet->padlen);
840     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
841     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
842     
843     /* Re-encrypt packet */
844     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
845     
846     /* Send the packet */
847     silc_server_packet_send_real(server, dst_sock, FALSE);
848
849   } else {
850     /* Key exist so encrypt just header and send it */
851     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
852                      + packet->dst_id_len + packet->padlen);
853     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
854     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
855
856     /* Encrypt header */
857     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, 
858                         SILC_PACKET_HEADER_LEN + packet->src_id_len + 
859                         packet->dst_id_len + packet->padlen);
860
861     silc_server_packet_send_real(server, dst_sock, FALSE);
862   }
863 }
864
865 /* Sends current motd to client */
866
867 void silc_server_send_motd(SilcServer server,
868                            SilcSocketConnection sock)
869 {
870   char *motd;
871   uint32 motd_len;
872
873   if (server->config && server->config->motd && 
874       server->config->motd->motd_file) {
875
876     motd = silc_file_read(server->config->motd->motd_file, &motd_len);
877     if (!motd)
878       return;
879
880     silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
881                             motd, motd_len);
882     silc_free(motd);
883   }
884 }
885
886 /* Sends error message. Error messages may or may not have any 
887    implications. */
888
889 void silc_server_send_error(SilcServer server,
890                             SilcSocketConnection sock,
891                             const char *fmt, ...)
892 {
893   va_list ap;
894   unsigned char buf[4096];
895
896   memset(buf, 0, sizeof(buf));
897   va_start(ap, fmt);
898   vsprintf(buf, fmt, ap);
899   va_end(ap);
900
901   silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0, 
902                           buf, strlen(buf), FALSE);
903 }
904
905 /* Sends notify message. If format is TRUE the variable arguments are
906    formatted and the formatted string is sent as argument payload. If it is
907    FALSE then each argument is sent as separate argument and their format
908    in the argument list must be { argument data, argument length }. */
909
910 void silc_server_send_notify(SilcServer server,
911                              SilcSocketConnection sock,
912                              int broadcast,
913                              SilcNotifyType type,
914                              uint32 argc, ...)
915 {
916   va_list ap;
917   SilcBuffer packet;
918
919   va_start(ap, argc);
920
921   packet = silc_notify_payload_encode(type, argc, ap);
922   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 
923                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
924                           packet->data, packet->len, FALSE);
925   silc_buffer_free(packet);
926 }
927
928 /* Sends notify message and gets the arguments from the `args' Argument
929    Payloads. */
930
931 void silc_server_send_notify_args(SilcServer server,
932                                   SilcSocketConnection sock,
933                                   int broadcast,
934                                   SilcNotifyType type,
935                                   uint32 argc,
936                                   SilcBuffer args)
937 {
938   SilcBuffer packet;
939
940   packet = silc_notify_payload_encode_args(type, argc, args);
941   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 
942                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
943                           packet->data, packet->len, FALSE);
944   silc_buffer_free(packet);
945 }
946
947 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
948    `old_id' with the `new_id'. */
949
950 void silc_server_send_notify_channel_change(SilcServer server,
951                                             SilcSocketConnection sock,
952                                             int broadcast,
953                                             SilcChannelID *old_id,
954                                             SilcChannelID *new_id,
955                                             uint32 id_len)
956 {
957   SilcBuffer idp1, idp2;
958
959   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
960   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
961
962   silc_server_send_notify(server, sock, broadcast,
963                           SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
964                           2, idp1->data, idp1->len, idp2->data, idp2->len);
965   silc_buffer_free(idp1);
966   silc_buffer_free(idp2);
967 }
968
969 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
970    `old_id' with the `new_id'. */
971
972 void silc_server_send_notify_nick_change(SilcServer server,
973                                          SilcSocketConnection sock,
974                                          int broadcast,
975                                          SilcClientID *old_id,
976                                          SilcClientID *new_id,
977                                          uint32 id_len)
978 {
979   SilcBuffer idp1, idp2;
980
981   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
982   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
983
984   silc_server_send_notify(server, sock, broadcast, 
985                           SILC_NOTIFY_TYPE_NICK_CHANGE,
986                           2, idp1->data, idp1->len, idp2->data, idp2->len);
987   silc_buffer_free(idp1);
988   silc_buffer_free(idp2);
989 }
990
991 /* Sends JOIN notify type. This tells that new client by `client_id' ID
992    has joined to the `channel'. */
993
994 void silc_server_send_notify_join(SilcServer server,
995                                   SilcSocketConnection sock,
996                                   int broadcast,
997                                   SilcChannelEntry channel,
998                                   SilcClientID *client_id,
999                                   uint32 client_id_len)
1000 {
1001   SilcBuffer idp1, idp2;
1002
1003   idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1004   idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1005   silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1006                           2, idp1->data, idp1->len,
1007                           idp2->data, idp2->len);
1008   silc_buffer_free(idp1);
1009   silc_buffer_free(idp2);
1010 }
1011
1012 /* Sends LEAVE notify type. This tells that `client_id' has left the
1013    `channel'. The Notify packet is always destined to the channel. */
1014
1015 void silc_server_send_notify_leave(SilcServer server,
1016                                    SilcSocketConnection sock,
1017                                    int broadcast,
1018                                    SilcChannelEntry channel,
1019                                    SilcClientID *client_id,
1020                                    uint32 client_id_len)
1021 {
1022   SilcBuffer idp;
1023
1024   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1025   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1026                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1027                                1, idp->data, idp->len);
1028   silc_buffer_free(idp);
1029 }
1030
1031 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1032    `channel' mode to `mode. The Notify packet is always destined to
1033    the channel. */
1034
1035 void silc_server_send_notify_cmode(SilcServer server,
1036                                    SilcSocketConnection sock,
1037                                    int broadcast,
1038                                    SilcChannelEntry channel,
1039                                    uint32 mode_mask,
1040                                    void *id, SilcIdType id_type,
1041                                    uint32 id_len,
1042                                    char *cipher, char *hmac)
1043 {
1044   SilcBuffer idp;
1045   unsigned char mode[4];
1046
1047   idp = silc_id_payload_encode((void *)id, id_type);
1048   SILC_PUT32_MSB(mode_mask, mode);
1049
1050   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1051                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1052                                4, idp->data, idp->len,
1053                                mode, 4,
1054                                cipher, cipher ? strlen(cipher) : 0,
1055                                hmac, hmac ? strlen(hmac) : 0);
1056   silc_buffer_free(idp);
1057 }
1058
1059 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
1060    `target' client's mode on `channel'. The Notify packet is always
1061    destined to the channel. */
1062
1063 void silc_server_send_notify_cumode(SilcServer server,
1064                                     SilcSocketConnection sock,
1065                                     int broadcast,
1066                                     SilcChannelEntry channel,
1067                                     uint32 mode_mask,
1068                                     SilcClientID *client_id,
1069                                     uint32 client_id_len,
1070                                     SilcClientID *target,
1071                                     uint32 target_len)
1072 {
1073   SilcBuffer idp1, idp2;
1074   unsigned char mode[4];
1075
1076   idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1077   idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1078   SILC_PUT32_MSB(mode_mask, mode);
1079
1080   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1081                                SILC_ID_CHANNEL, 
1082                                SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3, 
1083                                idp1->data, idp1->len,
1084                                mode, 4,
1085                                idp2->data, idp2->len);
1086   silc_buffer_free(idp1);
1087   silc_buffer_free(idp2);
1088 }
1089
1090 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1091    left SILC network. This function is used only between server and router
1092    traffic. This is not used to send the notify to the channel for
1093    client. The `message may be NULL. */
1094
1095 void silc_server_send_notify_signoff(SilcServer server,
1096                                      SilcSocketConnection sock,
1097                                      int broadcast,
1098                                      SilcClientID *client_id,
1099                                      uint32 client_id_len,
1100                                      char *message)
1101 {
1102   SilcBuffer idp;
1103
1104   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1105   silc_server_send_notify(server, sock, broadcast,
1106                           SILC_NOTIFY_TYPE_SIGNOFF,
1107                           message ? 2 : 1, idp->data, idp->len,
1108                           message, message ? strlen(message): 0);
1109   silc_buffer_free(idp);
1110 }
1111
1112 /* Sends TOPIC_SET notify type. This tells that `client_id' changed
1113    the `channel's topic to `topic'. The Notify packet is always destined
1114    to the channel. This function is used to send the topic set notifies
1115    between routers. */
1116
1117 void silc_server_send_notify_topic_set(SilcServer server,
1118                                        SilcSocketConnection sock,
1119                                        int broadcast,
1120                                        SilcChannelEntry channel,
1121                                        SilcClientID *client_id,
1122                                        uint32 client_id_len,
1123                                        char *topic)
1124 {
1125   SilcBuffer idp;
1126
1127   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1128   silc_server_send_notify(server, sock, broadcast,
1129                           SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
1130                           topic ? 2 : 1, 
1131                           idp->data, idp->len, 
1132                           topic, topic ? strlen(topic) : 0);
1133   silc_buffer_free(idp);
1134 }
1135
1136 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1137    was kicked off the channel.  The `comment' may indicate the reason
1138    for the kicking. This function is used only between server and router
1139    traffic. */
1140
1141 void silc_server_send_notify_kicked(SilcServer server,
1142                                     SilcSocketConnection sock,
1143                                     int broadcast,
1144                                     SilcChannelEntry channel,
1145                                     SilcClientID *client_id,
1146                                     uint32 client_id_len,
1147                                     char *comment)
1148 {
1149   SilcBuffer idp;
1150
1151   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1152   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1153                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED,
1154                                comment ? 2 : 1, idp->data, idp->len,
1155                                comment, comment ? strlen(comment) : 0);
1156   silc_buffer_free(idp);
1157 }
1158
1159 /* Send KILLED notify type. This tells that the `client_id' client was
1160    killed from the network.  The `comment' may indicate the reason
1161    for the killing. */
1162
1163 void silc_server_send_notify_killed(SilcServer server,
1164                                     SilcSocketConnection sock,
1165                                     int broadcast,
1166                                     SilcClientID *client_id,
1167                                     uint32 client_id_len,
1168                                     char *comment)
1169 {
1170   SilcBuffer idp;
1171
1172   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1173   silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1174                                SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1175                                comment ? 2 : 1, idp->data, idp->len,
1176                                comment, comment ? strlen(comment) : 0);
1177   silc_buffer_free(idp);
1178 }
1179
1180 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1181    user mode in the SILC Network was changed. This function is used to
1182    send the packet between routers as broadcast packet. */
1183
1184 void silc_server_send_notify_umode(SilcServer server,
1185                                    SilcSocketConnection sock,
1186                                    int broadcast,
1187                                    SilcClientID *client_id,
1188                                    uint32 client_id_len,
1189                                    uint32 mode_mask)
1190 {
1191   SilcBuffer idp;
1192   unsigned char mode[4];
1193
1194   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1195   SILC_PUT32_MSB(mode_mask, mode);
1196
1197   silc_server_send_notify(server, sock, broadcast,
1198                           SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1199                           idp->data, idp->len, 
1200                           mode, 4);
1201   silc_buffer_free(idp);
1202 }
1203
1204 /* Sends BAN notify type. This tells that ban has been either `add'ed
1205    or `del'eted on the `channel. This function is used to send the packet
1206    between routers as broadcast packet. */
1207
1208 void silc_server_send_notify_ban(SilcServer server,
1209                                  SilcSocketConnection sock,
1210                                  int broadcast,
1211                                  SilcChannelEntry channel,
1212                                  char *add, char *del)
1213 {
1214   SilcBuffer idp;
1215
1216   idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1217   silc_server_send_notify(server, sock, broadcast,
1218                           SILC_NOTIFY_TYPE_BAN, 3,
1219                           idp->data, idp->len,
1220                           add, add ? strlen(add) : 0,
1221                           del, del ? strlen(del) : 0);
1222   silc_buffer_free(idp);
1223 }
1224
1225 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1226    or `del'eted on the `channel.  The sender of the invite is the `client_id'.
1227    This function is used to send the packet between routers as broadcast
1228    packet. */
1229
1230 void silc_server_send_notify_invite(SilcServer server,
1231                                     SilcSocketConnection sock,
1232                                     int broadcast,
1233                                     SilcChannelEntry channel,
1234                                     SilcClientID *client_id,
1235                                     uint32 client_id_len,
1236                                     char *add, char *del)
1237 {
1238   SilcBuffer idp, idp2;
1239
1240   idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1241   idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1242   silc_server_send_notify(server, sock, broadcast,
1243                           SILC_NOTIFY_TYPE_INVITE, 5,
1244                           idp->data, idp->len,
1245                           channel->channel_name, strlen(channel->channel_name),
1246                           idp2->data, idp2->len,
1247                           add, add ? strlen(add) : 0,
1248                           del, del ? strlen(del) : 0);
1249   silc_buffer_free(idp);
1250   silc_buffer_free(idp2);
1251 }
1252
1253 /* Sends notify message destined to specific entity. */
1254
1255 void silc_server_send_notify_dest(SilcServer server,
1256                                   SilcSocketConnection sock,
1257                                   int broadcast,
1258                                   void *dest_id,
1259                                   SilcIdType dest_id_type,
1260                                   SilcNotifyType type,
1261                                   uint32 argc, ...)
1262 {
1263   va_list ap;
1264   SilcBuffer packet;
1265
1266   va_start(ap, argc);
1267
1268   packet = silc_notify_payload_encode(type, argc, ap);
1269   silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0, 
1270                                dest_id, dest_id_type,
1271                                packet->data, packet->len, FALSE);
1272   silc_buffer_free(packet);
1273 }
1274
1275 /* Sends notify message to a channel. The notify message sent is 
1276    distributed to all clients on the channel. If `route_notify' is TRUE
1277    then the notify may be routed to primary route or to some other routers.
1278    If FALSE it is assured that the notify is sent only locally. If `sender'
1279    is provided then the packet is not sent to that connection since it
1280    originally came from it. */
1281
1282 void silc_server_send_notify_to_channel(SilcServer server,
1283                                         SilcSocketConnection sender,
1284                                         SilcChannelEntry channel,
1285                                         unsigned char route_notify,
1286                                         SilcNotifyType type,
1287                                         uint32 argc, ...)
1288 {
1289   va_list ap;
1290   SilcBuffer packet;
1291
1292   va_start(ap, argc);
1293
1294   packet = silc_notify_payload_encode(type, argc, ap);
1295   silc_server_packet_send_to_channel(server, sender, channel, 
1296                                      SILC_PACKET_NOTIFY, route_notify,
1297                                      packet->data, packet->len, FALSE);
1298   silc_buffer_free(packet);
1299 }
1300
1301 /* Send notify message to all channels the client has joined. It is quaranteed
1302    that the message is sent only once to a client (ie. if a client is joined
1303    on two same channel it will receive only one notify message). Also, this
1304    sends only to local clients (locally connected if we are server, and to
1305    local servers if we are router). If `sender' is provided the packet is
1306    not sent to that client at all. */
1307
1308 void silc_server_send_notify_on_channels(SilcServer server,
1309                                          SilcClientEntry sender,
1310                                          SilcClientEntry client,
1311                                          SilcNotifyType type,
1312                                          uint32 argc, ...)
1313 {
1314   int k;
1315   SilcSocketConnection sock = NULL;
1316   SilcPacketContext packetdata;
1317   SilcClientEntry c;
1318   SilcClientEntry *sent_clients = NULL;
1319   uint32 sent_clients_count = 0;
1320   SilcServerEntry *routed = NULL;
1321   uint32 routed_count = 0;
1322   SilcChannelEntry channel;
1323   SilcChannelClientEntry chl, chl2;
1324   SilcIDListData idata;
1325   SilcBuffer packet;
1326   unsigned char *data;
1327   uint32 data_len;
1328   int force_send = FALSE;
1329   va_list ap;
1330
1331   SILC_LOG_DEBUG(("Start"));
1332
1333   if (!silc_list_count(client->channels))
1334     return;
1335
1336   va_start(ap, argc);
1337   packet = silc_notify_payload_encode(type, argc, ap);
1338   data = packet->data;
1339   data_len = packet->len;
1340
1341   /* Set the packet context pointers. */
1342   packetdata.flags = 0;
1343   packetdata.type = SILC_PACKET_NOTIFY;
1344   packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1345   packetdata.src_id_len = SILC_ID_SERVER_LEN;
1346   packetdata.src_id_type = SILC_ID_SERVER;
1347
1348   silc_list_start(client->channels);
1349   while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
1350     channel = chl->channel;
1351
1352     /* Send the message to all clients on the channel's client list. */
1353     silc_list_start(channel->user_list);
1354     while ((chl2 = silc_list_get(channel->user_list)) != SILC_LIST_END) {
1355       c = chl2->client;
1356       
1357       if (sender && c == sender)
1358         continue;
1359
1360       /* Check if we have sent the packet to this client already */
1361       for (k = 0; k < sent_clients_count; k++)
1362         if (sent_clients[k] == c)
1363           break;
1364       if (k < sent_clients_count)
1365         continue;
1366
1367       /* If we are router and if this client has router set it is not
1368          locally connected client and we will route the message to the
1369          router set in the client. */
1370       if (c && c->router && server->server_type == SILC_ROUTER) {
1371         /* Check if we have sent the packet to this route already */
1372         for (k = 0; k < routed_count; k++)
1373           if (routed[k] == c->router)
1374             break;
1375         if (k < routed_count)
1376           continue;
1377         
1378         /* Get data used in packet header encryption, keys and stuff. */
1379         sock = (SilcSocketConnection)c->router->connection;
1380         idata = (SilcIDListData)c->router;
1381         
1382         packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1383         packetdata.dst_id_len = SILC_ID_SERVER_LEN;
1384         packetdata.dst_id_type = SILC_ID_SERVER;
1385         packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
1386           packetdata.src_id_len + packetdata.dst_id_len;
1387         packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1388
1389         /* Send the packet */
1390         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1391                                                 idata->send_key, 
1392                                                 idata->hmac_send, 
1393                                                 data, data_len, FALSE, 
1394                                                 force_send);
1395         
1396         silc_free(packetdata.dst_id);
1397
1398         /* We want to make sure that the packet is routed to same router
1399            only once. Mark this route as sent route. */
1400         k = routed_count;
1401         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1402         routed[k] = c->router;
1403         routed_count++;
1404
1405         continue;
1406       }
1407
1408       if (c && c->router)
1409         continue;
1410
1411       /* Send to locally connected client */
1412       if (c) {
1413         
1414         /* Get data used in packet header encryption, keys and stuff. */
1415         sock = (SilcSocketConnection)c->connection;
1416         idata = (SilcIDListData)c;
1417         
1418         packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1419         packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
1420         packetdata.dst_id_type = SILC_ID_CLIENT;
1421         packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
1422           packetdata.src_id_len + packetdata.dst_id_len;
1423         packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1424
1425         /* Send the packet */
1426         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1427                                                 idata->send_key, 
1428                                                 idata->hmac_send, 
1429                                                 data, data_len, FALSE, 
1430                                                 force_send);
1431
1432         silc_free(packetdata.dst_id);
1433
1434         /* Make sure that we send the notify only once per client. */
1435         sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) * 
1436                                     (sent_clients_count + 1));
1437         sent_clients[sent_clients_count] = c;
1438         sent_clients_count++;
1439       }
1440     }
1441   }
1442
1443   if (routed_count)
1444     silc_free(routed);
1445   if (sent_clients_count)
1446     silc_free(sent_clients);
1447   silc_free(packetdata.src_id);
1448 }
1449
1450 /* Sends New ID Payload to remote end. The packet is used to distribute
1451    information about new registered clients, servers, channel etc. usually
1452    to routers so that they can keep these information up to date. 
1453    If the argument `broadcast' is TRUE then the packet is sent as
1454    broadcast packet. */
1455
1456 void silc_server_send_new_id(SilcServer server,
1457                              SilcSocketConnection sock,
1458                              int broadcast,
1459                              void *id, SilcIdType id_type, 
1460                              uint32 id_len)
1461 {
1462   SilcBuffer idp;
1463
1464   SILC_LOG_DEBUG(("Start"));
1465
1466   idp = silc_id_payload_encode(id, id_type);
1467   silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 
1468                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1469                           idp->data, idp->len, FALSE);
1470   silc_buffer_free(idp);
1471 }
1472
1473 /* Send New Channel Payload to notify about newly created channel in the
1474    SILC network. Normal server nevers sends this packet. Router uses this
1475    to notify other routers in the network about new channel. This packet
1476    is broadcasted. */
1477
1478 void silc_server_send_new_channel(SilcServer server,
1479                                   SilcSocketConnection sock,
1480                                   int broadcast,
1481                                   char *channel_name,
1482                                   void *channel_id, 
1483                                   uint32 channel_id_len,
1484                                   uint32 mode)
1485 {
1486   SilcBuffer packet;
1487   unsigned char *cid;
1488   uint32 name_len = strlen(channel_name);
1489
1490   SILC_LOG_DEBUG(("Start"));
1491
1492   cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1493   if (!cid)
1494     return;
1495
1496   /* Encode the channel payload */
1497   packet = silc_channel_payload_encode(channel_name, name_len,
1498                                        cid, channel_id_len, mode);
1499
1500   silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL, 
1501                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1502                           packet->data, packet->len, FALSE);
1503
1504   silc_free(cid);
1505   silc_buffer_free(packet);
1506 }
1507
1508 /* Send Channel Key payload to distribute the new channel key. Normal server
1509    sends this to router when new client joins to existing channel. Router
1510    sends this to the local server who sent the join command in case where
1511    the channel did not exist yet. Both normal and router servers uses this
1512    also to send this to locally connected clients on the channel. This
1513    must not be broadcasted packet. Routers do not send this to each other. 
1514    If `sender is provided then the packet is not sent to that connection since
1515    it originally came from it. */
1516
1517 void silc_server_send_channel_key(SilcServer server,
1518                                   SilcSocketConnection sender,
1519                                   SilcChannelEntry channel,
1520                                   unsigned char route)
1521 {
1522   SilcBuffer packet;
1523   unsigned char *chid;
1524   uint32 tmp_len;
1525  
1526   SILC_LOG_DEBUG(("Start"));
1527  
1528   chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1529   if (!chid)
1530     return;
1531  
1532   /* Encode channel key packet */
1533   tmp_len = strlen(channel->channel_key->cipher->name);
1534   packet = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, chid, tmp_len,
1535                                            channel->channel_key->cipher->name,
1536                                            channel->key_len / 8, channel->key);
1537  
1538   silc_server_packet_send_to_channel(server, sender, channel, 
1539                                      SILC_PACKET_CHANNEL_KEY,
1540                                      route, packet->data, packet->len, FALSE);
1541   silc_buffer_free(packet);
1542   silc_free(chid);
1543 }
1544
1545 /* Generic function to send any command. The arguments must be sent already
1546    encoded into correct form in correct order. */
1547
1548 void silc_server_send_command(SilcServer server, 
1549                               SilcSocketConnection sock,
1550                               SilcCommand command, 
1551                               uint32 argc, ...)
1552 {
1553   SilcBuffer packet;
1554   va_list ap;
1555
1556   va_start(ap, argc);
1557
1558   packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1559   silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1560                           packet->data, packet->len, TRUE);
1561   silc_buffer_free(packet);
1562 }
1563
1564 /* Send the heartbeat packet. */
1565
1566 void silc_server_send_heartbeat(SilcServer server,
1567                                 SilcSocketConnection sock)
1568 {
1569   silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1570                           NULL, 0, FALSE);
1571 }
1572
1573 /* Generic function to relay packet we've received. This is used to relay
1574    packets to a client but generally can be used to other purposes as well. */
1575
1576 void silc_server_relay_packet(SilcServer server,
1577                               SilcSocketConnection dst_sock,
1578                               SilcCipher cipher,
1579                               SilcHmac hmac,
1580                               SilcPacketContext *packet,
1581                               int force_send)
1582 {
1583   silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1584                    + packet->dst_id_len + packet->padlen);
1585
1586   silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len);
1587   silc_buffer_put(dst_sock->outbuf, packet->buffer->data, packet->buffer->len);
1588   
1589   /* Re-encrypt packet */
1590   silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, packet->buffer->len);
1591   
1592   /* Send the packet */
1593   silc_server_packet_send_real(server, dst_sock, force_send);
1594
1595   silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1596                    + packet->dst_id_len + packet->padlen);
1597 }
1598
1599 /* Routine used to send the connection authentication packet. */
1600
1601 void silc_server_send_connection_auth_request(SilcServer server,
1602                                               SilcSocketConnection sock,
1603                                               uint16 conn_type,
1604                                               SilcAuthMethod auth_meth)
1605 {
1606   SilcBuffer packet;
1607
1608   packet = silc_buffer_alloc(4);
1609   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1610   silc_buffer_format(packet,
1611                      SILC_STR_UI_SHORT(conn_type),
1612                      SILC_STR_UI_SHORT(auth_meth),
1613                      SILC_STR_END);
1614
1615   silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1616                           0, packet->data, packet->len, FALSE);
1617   silc_buffer_free(packet);
1618 }